]> git.piffa.net Git - sketchbook_andrea/blob - piezo/piezo_1_tonePitchFollower/piezo_1_tonePitchFollower.ino
piezo
[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://www.dummies.com/how-to/content/how-to-make-an-instrument-with-the-arduino.html
18 - https://www.arduino.cc/en/Tutorial/tonePitchFollower
19  
20  */
21
22
23 // These constants won't change:
24 const int sensorPin = A0;    // pin that the sensor is attached to
25 const int ledPin = 9;        // pin that the LED is attached to
26
27
28 void setup() {
29   // initialize serial communications (for debugging only):
30   Serial.begin(9600);
31 }
32
33 void loop() {
34   // read the sensor:
35   int sensorReading = analogRead(sensorPin);
36   // print the sensor reading so you know its range
37   Serial.println(sensorReading);
38   // map the analog input range (in this case, 400 - 1000 from the photoresistor)
39   // to the output pitch range (120 - 1500Hz)
40   // change the minimum and maximum input numbers below
41   // depending on the range your sensor's giving:
42   int thisPitch = map(sensorReading, 300, 900, 220, 3500);
43
44   // play the pitch:
45   tone(ledPin, thisPitch, 10);
46   delay(1);        // delay in between reads for stability
47 }
48
49 /* Domande:
50 1. Modificare l'estensione del range di frequenza del piezo
51 tra i valori 20hz e 20.000hz.
52 2. Inserire l'auto calibrazione
53 3. Modificare lo script in modo che vengano emessi suoni solo quando
54 una mano offusca leggermente la luce (il piezo non deve suonare
55 se non c'e' un operatore a interagire con la fotoresistenza).
56 */