4 Plays a pitch that changes based on a changing analog input
7 * 8-ohm speaker on digital pin 8
8 * photoresistor on analog 0 to 5V
9 * 4.7K resistor on analog 0 to ground
13 by Tom Igoe, with suggestion from Michael Flynn
15 This example code is in the public domain.
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
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
29 // initialize serial communications (for debugging only):
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);
45 tone(ledPin, thisPitch, 10);
46 delay(1); // delay in between reads for stability
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).
83 // These constants won't change:
84 const int sensorPin = A0; // pin that the sensor is attached to
85 const int ledPin = 9; // pin that the LED is attached to
88 int sensorValue = 0; // the sensor value
89 int sensorMin = 1023; // minimum sensor value
90 int sensorMax = 0; // maximum sensor value
93 // initialize serial communications (for debugging only):
96 digitalWrite(13, HIGH);
98 // calibrate during the first five seconds
99 while (millis() < 5000) {
100 sensorValue = analogRead(sensorPin);
102 // record the maximum sensor value
103 if (sensorValue > sensorMax) {
104 sensorMax = sensorValue;
107 // record the minimum sensor value
108 if (sensorValue < sensorMin) {
109 sensorMin = sensorValue;
113 // signal the end of the calibration period
114 digitalWrite(13, LOW);
119 int sensorReading = analogRead(sensorPin);
120 // print the sensor reading so you know its range
121 Serial.println(sensorReading);
122 // map the analog input range (in this case, 400 - 1000 from the photoresistor)
123 // to the output pitch range (120 - 1500Hz)
124 // change the minimum and maximum input numbers below
125 // depending on the range your sensor's giving:
126 int thisPitch = map(sensorReading, sensorMin, sensorMax, 220, 3500);
129 if (sensorReading < sensorMax -50) {
130 tone(ledPin, thisPitch, 10);
132 delay(1); // delay in between reads for stability