2011-08-30 21:33:32 +02:00
|
|
|
/*
|
|
|
|
Pitch follower
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
Plays a pitch that changes based on a changing analog input
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
circuit:
|
2013-06-11 14:47:15 +02:00
|
|
|
* 8-ohm speaker on digital pin 9
|
2011-08-30 21:33:32 +02:00
|
|
|
* photoresistor on analog 0 to 5V
|
|
|
|
* 4.7K resistor on analog 0 to ground
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
created 21 Jan 2010
|
2012-06-01 00:23:59 +02:00
|
|
|
modified 31 May 2012
|
|
|
|
by Tom Igoe, with suggestion from Michael Flynn
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/Tone2
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// initialize serial communications (for debugging only):
|
|
|
|
Serial.begin(9600);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// read the sensor:
|
|
|
|
int sensorReading = analogRead(A0);
|
|
|
|
// print the sensor reading so you know its range
|
|
|
|
Serial.println(sensorReading);
|
2012-06-01 00:23:59 +02:00
|
|
|
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
|
|
|
|
// to the output pitch range (120 - 1500Hz)
|
2011-08-30 21:33:32 +02:00
|
|
|
// change the minimum and maximum input numbers below
|
|
|
|
// depending on the range your sensor's giving:
|
2012-06-01 00:23:59 +02:00
|
|
|
int thisPitch = map(sensorReading, 400, 1000, 120, 1500);
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
// play the pitch:
|
|
|
|
tone(9, thisPitch, 10);
|
2012-04-09 16:48:11 +02:00
|
|
|
delay(1); // delay in between reads for stability
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|