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://arduino.cc/en/Tutorial/Tone2
22 // These constants won't change:
23 const int sensorPin = A0; // pin that the sensor is attached to
24 const int ledPin = 9; // pin that the LED is attached to
28 // initialize serial communications (for debugging only):
34 int sensorReading = analogRead(sensorPin);
35 // print the sensor reading so you know its range
36 Serial.println(sensorReading);
37 // map the analog input range (in this case, 400 - 1000 from the photoresistor)
38 // to the output pitch range (120 - 1500Hz)
39 // change the minimum and maximum input numbers below
40 // depending on the range your sensor's giving:
41 int thisPitch = map(sensorReading, sensorMin, sensorMax, 220, 3500);
44 tone(ledPin, thisPitch, 10);
45 delay(1); // delay in between reads for stability
49 1. Modificare l'estensione del range di frequenza del piezo
50 tra i valori 20hz e 20.000hz.
51 2. Inserire l'auto calibrazione
52 3. Modificare lo script in modo che vengano emessi suoni solo quando
53 una mano offusca leggermente la luce (il piezo non deve suonare
54 se non c'e' un operatore a interagire con la fotoresistenza).
82 // These constants won't change:
83 const int sensorPin = A0; // pin that the sensor is attached to
84 const int ledPin = 9; // pin that the LED is attached to
87 int sensorValue = 0; // the sensor value
88 int sensorMin = 1023; // minimum sensor value
89 int sensorMax = 0; // maximum sensor value
92 // initialize serial communications (for debugging only):
95 digitalWrite(13, HIGH);
97 // calibrate during the first five seconds
98 while (millis() < 5000) {
99 sensorValue = analogRead(sensorPin);
101 // record the maximum sensor value
102 if (sensorValue > sensorMax) {
103 sensorMax = sensorValue;
106 // record the minimum sensor value
107 if (sensorValue < sensorMin) {
108 sensorMin = sensorValue;
112 // signal the end of the calibration period
113 digitalWrite(13, LOW);
118 int sensorReading = analogRead(sensorPin);
119 // print the sensor reading so you know its range
120 Serial.println(sensorReading);
121 // map the analog input range (in this case, 400 - 1000 from the photoresistor)
122 // to the output pitch range (120 - 1500Hz)
123 // change the minimum and maximum input numbers below
124 // depending on the range your sensor's giving:
125 int thisPitch = map(sensorReading, sensorMin, sensorMax, 220, 3500);
128 if (sensorReading < sensorMax -50) {
129 tone(ledPin, thisPitch, 10);
131 delay(1); // delay in between reads for stability