]> git.piffa.net Git - sketchbook_andrea/blob - piezo/piezo_1_tonePitchFollower/piezo_1_tonePitchFollower.ino
e74c7b343449a5c366c457ebb127c5887d99dcc4
[sketchbook_andrea] / piezo / piezo_1_tonePitchFollower / piezo_1_tonePitchFollower.ino
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 8
8  * photoresistor on analog 0 to 5V
9  * 4.7K 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  http://arduino.cc/en/Tutorial/Tone2
18  
19  */
20
21
22 void setup() {
23   // initialize serial communications (for debugging only):
24   Serial.begin(9600);
25 }
26
27 void loop() {
28   // read the sensor:
29   int sensorReading = analogRead(A0);
30   // print the sensor reading so you know its range
31   Serial.println(sensorReading);
32   // map the analog input range (in this case, 400 - 1000 from the photoresistor)
33   // to the output pitch range (120 - 1500Hz)
34   // change the minimum and maximum input numbers below
35   // depending on the range your sensor's giving:
36   int thisPitch = map(sensorReading, 400, 1000, 120, 1500);
37
38   // play the pitch:
39   tone(9, thisPitch, 10);
40   delay(1);        // delay in between reads for stability
41 }
42
43
44
45
46
47