Arduino Project Book #6: Light Theremin
Parts required:
Photoresistor
10 kilohm resistor
Small speaker connected to Pin 8 and ground
Photocell sensor Circuits
https://sites.google.com/view/acera-arduino/arduino/voltage-divider-and-photocell.
//Arduino Book Project #6 - Light Theremin
int sensorValue; // variable to hold sensor value
int sensorLow = 1023; // variable to calibrate low value
int sensorHigh = 0; // variable to calibrate high value
const int ledPin = 13; // LED pin
void setup() {
// Make the LED pin an output and turn it on
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// calibrate for the first five seconds after program runs
while (millis() < 5000) {
sensorValue = analogRead(A0); // record the maximum sensor value
if (sensorValue > sensorHigh) {
sensorHigh = sensorValue;
}
if (sensorValue < sensorLow) { // record the minimum sensor value
sensorLow = sensorValue;
}
}
digitalWrite(ledPin, LOW); // turn the LED off, signaling the end of the calibration period
}
void loop() {
sensorValue = analogRead(A0); //read the input from A0 and store it in a variable
// map the sensor values to a wide range of pitches
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
tone(8, pitch, 20); // play the tone for 20 ms on pin 8
delay(10); // wait for a moment
}