Hide minor edits - Show changes to markup
Examples > Digital I/O
This example shows how to use the tone() command to generate a pitch that follows the values of an analog input
Ejemplos > E/S Digital
Este ejemplo muestra como usar la función tone() para generar un tono que sigue los valores de una entrada analógica.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic:
click the image to enlarge
image developed using Fritzing. For more circuit examples, see the Página del proyecto Fritzing
Esquemático:
Haz click aquí para ampliar la imagen.
The code for this example is very simple. Just take an analog input and map its values to a range of audible pitches. Humans can hear from 20 - 20,000Hz, but 100 - 1000 usually works pretty well for this sketch.
You'll need to get the actual range of your analog input for the mapping. In the circuit shown, the analog input value ranged from about 400 to about 1000. Change the values in the map() comand to match the range for your sensor.
The sketch is as follows:
El código de este ejemplo es muy simple. Simplemente lee una entrada analógica y lo convierte en un valor en un rango comprendido en el rango de frecuencia audible. Los humanos podemos oír frecuencias entre 20 y 20.000 Hz, pero 100 - 1000 funciona bien para este programa.
Necesitaras encontrar el rango de variación de tu entrada analógica para poder hacer la conversión. En el circuito que se muestra el rango de la entrada analógica va desde 400 hasta aproximadamente 1000.Cambia los valores en la función map() para que se ajusten al rango de tu sensor.
El programa es el siguiente:
/* Pitch follower Plays a pitch that changes based on a changing analog input circuit: * 8-ohm speaker on digital pin 8 * photoresistor on analog 0 to 5V * 4.7K resistor on analog 0 to ground created 21 Jan 2010 by Tom Igoe http://arduino.cc/en/Tutorial/Tone2 */
(:includeurl http://arduino.googlecode.com/svn/trunk/build/shared/examples/Digital/tonePitchFollower/tonePitchFollower.pde border=0:)
void setup() { // initialize serial communications (for debugging only): Serial.begin(9600); }
void loop() { // read the sensor: int sensorReading = analogRead(0); // print the sensor reading so you know its range Serial.println(sensorReading); // map the pitch to the range of the analog input. // change the minimum and maximum input numbers below // depending on the range your sensor's giving: int thisPitch = map(sensorReading, 400, 1000, 100, 1000);
// play the pitch: tone(8, thisPitch, 10);
}
Examples > Digital I/O
This example shows how to use the tone() command to generate a pitch that follows the values of an analog input
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic:
click the image to enlarge
The code for this example is very simple. Just take an analog input and map its values to a range of audible pitches. Humans can hear from 20 - 20,000Hz, but 100 - 1000 usually works pretty well for this sketch.
You'll need to get the actual range of your analog input for the mapping. In the circuit shown, the analog input value ranged from about 400 to about 1000. Change the values in the map() comand to match the range for your sensor.
The sketch is as follows: (:div class=code :)
/* Pitch follower Plays a pitch that changes based on a changing analog input circuit: * 8-ohm speaker on digital pin 8 * photoresistor on analog 0 to 5V * 4.7K resistor on analog 0 to ground created 21 Jan 2010 by Tom Igoe http://arduino.cc/en/Tutorial/Tone2 */
void setup() { // initialize serial communications (for debugging only): Serial.begin(9600); }
void loop() { // read the sensor: int sensorReading = analogRead(0); // print the sensor reading so you know its range Serial.println(sensorReading); // map the pitch to the range of the analog input. // change the minimum and maximum input numbers below // depending on the range your sensor's giving: int thisPitch = map(sensorReading, 400, 1000, 100, 1000);
// play the pitch: tone(8, thisPitch, 10);
}
(:divend:)