]> git.piffa.net Git - sketchbook_andrea/blob - piezo/piezo_1_tonePitchFollower/piezo_1_tonePitchFollower.ino
9ceda24d32cdabfc4475e6f7f51be22477f55990
[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 // 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
25
26
27 void setup() {
28   // initialize serial communications (for debugging only):
29   Serial.begin(9600);
30 }
31
32 void loop() {
33   // read the sensor:
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);
42
43   // play the pitch:
44   tone(ledPin, thisPitch, 10);
45   delay(1);        // delay in between reads for stability
46 }
47
48 /* Domande:
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).
55
56 Soluzioni in fondo.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 Soluzioni:
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
85
86 // variables:
87 int sensorValue = 0;         // the sensor value
88 int sensorMin = 1023;        // minimum sensor value
89 int sensorMax = 0;           // maximum sensor value
90
91 void setup() {
92   // initialize serial communications (for debugging only):
93   Serial.begin(9600);
94     pinMode(13, OUTPUT);
95   digitalWrite(13, HIGH);
96
97   // calibrate during the first five seconds 
98   while (millis() < 5000) {
99     sensorValue = analogRead(sensorPin);
100
101     // record the maximum sensor value
102     if (sensorValue > sensorMax) {
103       sensorMax = sensorValue;
104     }
105
106     // record the minimum sensor value
107     if (sensorValue < sensorMin) {
108       sensorMin = sensorValue;
109     }
110   }
111
112   // signal the end of the calibration period
113   digitalWrite(13, LOW);
114 }
115
116 void loop() {
117   // read the sensor:
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);
126
127   // play the pitch:
128   if (sensorReading < sensorMax -50) {
129   tone(ledPin, thisPitch, 10);
130   }
131   delay(1);        // delay in between reads for stability
132 }
133 */
134
135
136