]> git.piffa.net Git - sketchbook_andrea/blob - piezo/piezo_1_tonePitchFollower/piezo_1_tonePitchFollower.ino
5ae3099928d38078368e9335485f64da745cfc6a
[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 /* Domande:
44 1. Modificare l'estensione del range di frequenza del piezo
45 tra i valori 20hz e 20.000hz.
46
47 2. Modificare lo script in modo che vengano emessi suoni solo quando
48 una mano offusca leggermente la luce (il piezo non deve suonare
49 se non c'e' un operatore a interagire con la fotoresistenza).
50
51 */
52
53
54