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