| 1 |
/* |
| 2 |
Pitch follower |
| 3 |
|
| 4 |
Plays a pitch that changes based on a changing analog input |
| 5 |
|
| 6 |
circuit: |
| 7 |
- 8 ohm speaker on digital pin 9 |
| 8 |
- photoresistor on analog 0 to 5V |
| 9 |
- 4.7 kilohm resistor on analog 0 to ground |
| 10 |
|
| 11 |
created 21 Jan 2010 |
| 12 |
modified 31 May 2012 |
| 13 |
by Tom Igoe, with suggestion from Michael Flynn |
| 14 |
|
| 15 |
This example code is in the public domain. |
| 16 |
|
| 17 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/tonePitchFollower |
| 18 |
*/ |
| 19 |
|
| 20 |
void setup() { |
| 21 |
// initialize serial communications (for debugging only): |
| 22 |
Serial.begin(9600); |
| 23 |
} |
| 24 |
|
| 25 |
void loop() { |
| 26 |
// read the sensor: |
| 27 |
int sensorReading = analogRead(A0); |
| 28 |
// print the sensor reading so you know its range |
| 29 |
Serial.println(sensorReading); |
| 30 |
// map the analog input range (in this case, 400 - 1000 from the photoresistor) |
| 31 |
// to the output pitch range (120 - 1500Hz) |
| 32 |
// change the minimum and maximum input numbers below depending on the range |
| 33 |
// your sensor's giving: |
| 34 |
int thisPitch = map(sensorReading, 400, 1000, 120, 1500); |
| 35 |
|
| 36 |
// play the pitch: |
| 37 |
tone(9, thisPitch, 10); |
| 38 |
delay(1); // delay in between reads for stability |
| 39 |
} |