4 The input from a photo resistor dictates the pitch of a piezo.
9 // These constants won't change:
10 const int sensorPin = A0; // pin that the sensor is attached to
11 const int ledPin = 9; // pin that the LED is attached to
14 // calibration variables:
15 int sensorValue = 0; // the sensor value
16 int sensorMin = 1023; // minimum sensor value
17 int sensorMax = 0; // maximum sensor value
21 // turn on LED to signal the start of the calibration period:
23 digitalWrite(13, HIGH);
25 // calibrate during the first five seconds
26 while (millis() < 5000) {
27 sensorValue = analogRead(sensorPin);
29 // record the maximum sensor value
30 if (sensorValue > sensorMax) {
31 sensorMax = sensorValue;
34 // record the minimum sensor value
35 if (sensorValue < sensorMin) {
36 sensorMin = sensorValue;
40 // signal the end of the calibration period
41 digitalWrite(13, LOW);
46 sensorValue = analogRead(sensorPin);
48 // apply the calibration to the sensor reading
49 thisPitch = map(sensorValue, sensorMin, sensorMax, 120, 1500);
50 // map the analog input range (in this case, min - max from the photoresistor)
51 // to the output pitch range (120 - 1500Hz)
52 // change the minimum and maximum input numbers below
53 // depending on the range your sensor's giving:
55 // in case the sensor value is outside the range seen during calibration
56 thisPitch = constrain(sensorValue, 120, 1500);
61 tone(ledPin, thisPitch, 10); // Tone is built in function
62 delay(1); // delay in between reads for stability