/* Analog input from photocell to determine LED brightness Matt Richard http://idblab.blogspot.com */ /* Pitch follower Plays a pitch that changes based on a changing analog input circuit: * 8-ohm speaker on digital pin 11, speakerPin * photoresistor on analog 0 to 5V * 4.7K resistor on analog 0 to ground created 21 Jan 2010 Modified 4 Sep 2010 by Tom Igoe This example code is in the public domain. http://arduino.cc/en/Tutorial/Tone2 */ int speakerPin = 11; int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 11; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int minThresh = 1000;// replace this number with a lowest number from the serial monitor int maxThresh = 0;// replace this number with a highest number from the serial monitor void setup() { Serial.begin(9600); // turn on pin 13 LED, to let us know we are in calibration mode digitalWrite(13, HIGH); // create a for loop for(int i = 0; i < 1000; i++){ // that repeatedly checks the value of the photocell sensorValue = analogRead(sensorPin); // and determines if the value is less than the lowest ever value if(sensorValue < minThresh){ minThresh = sensorValue; Serial.print("minThresh = "); Serial.println(minThresh);// minThresh = 213 } // or greater than the highest ever value. if(sensorValue > maxThresh){ maxThresh = sensorValue; Serial.print("maxThresh = "); Serial.println(maxThresh);// maxThresh = 613 } delay(5); } // turn off pin 13 led to let us know we are done calibrating digitalWrite(13, LOW); } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); sensorValue = constrain(sensorValue, minThresh, maxThresh); Serial.println(sensorValue); int thisPitch = map(sensorValue, minThresh, maxThresh, 100, 1000); // play the pitch: tone(speakerPin, thisPitch, 10); //analogWrite(ledPin, map(sensorValue, minThresh, maxThresh, 0, 255)); }