/*
Arduino Starter Kit example
Project 6 - Light Theremin
This sketch is written to accompany Project 6 in the Arduino Starter Kit
Parts required:
- photoresistor
- 10 kilohm resistor
- piezo
created 13 Sep 2012
by Scott Fitzgerald
https://store.arduino.cc/genuino-starter-kit
This example code is part of the public domain.
*/// variable to hold sensor value
intsensorValue;// variable to calibrate low value
intsensorLow=1023;// variable to calibrate high value
intsensorHigh=0;// LED pin
constintledPin=13;
voidsetup(){// 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){// record the maximum sensor value
sensorValue=analogRead(A0);if(sensorValue>sensorHigh){sensorHigh=sensorValue;}// record the minimum sensor value
if(sensorValue<sensorLow){sensorLow=sensorValue;}}// turn the LED off, signaling the end of the calibration period
digitalWrite(ledPin,LOW);}
voidloop(){//read the input from A0 and store it in a variable
sensorValue=analogRead(A0);// map the sensor values to a wide range of pitches
intpitch=map(sensorValue,sensorLow,sensorHigh,50,4000);// play the tone for 20 ms on pin 8
tone(8,pitch,20);// wait for a moment
delay(10);}