Advertisement
3rdWard_Arduino

class3_autoCalibrate_photocell_andSpeaker_tones

Mar 25th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. /*
  2. Analog input from photocell to determine LED brightness
  3.  Matt Richard
  4.  http://idblab.blogspot.com
  5.  */
  6. /*
  7. Pitch follower
  8.  Plays a pitch that changes based on a changing analog input
  9.  circuit:
  10.  * 8-ohm speaker on digital pin 11, speakerPin
  11.  * photoresistor on analog 0 to 5V
  12.  * 4.7K resistor on analog 0 to ground
  13.  created 21 Jan 2010
  14.  Modified 4 Sep 2010
  15.  by Tom Igoe
  16.  
  17.  This example code is in the public domain.
  18.  http://arduino.cc/en/Tutorial/Tone2
  19.  */
  20.  
  21.  
  22.  
  23. int speakerPin = 11;
  24. int sensorPin = A0; // select the input pin for the potentiometer
  25. int ledPin = 11; // select the pin for the LED
  26. int sensorValue = 0; // variable to store the value coming from the sensor
  27. int minThresh = 1000;// replace this number with a lowest number from the serial monitor
  28. int maxThresh = 0;// replace this number with a highest number from the serial monitor
  29.  
  30. void setup() {
  31.   Serial.begin(9600);
  32.  
  33.   // turn on pin 13 LED, to let us know we are in calibration mode
  34.   digitalWrite(13, HIGH);
  35.  
  36.   // create a for loop
  37.   for(int i = 0; i < 1000; i++){
  38.     // that repeatedly checks the value of the photocell
  39.     sensorValue = analogRead(sensorPin);
  40.     // and determines if the value is less than the lowest ever value
  41.     if(sensorValue < minThresh){
  42.       minThresh = sensorValue;
  43.       Serial.print("minThresh = ");
  44.       Serial.println(minThresh);// minThresh = 213                        
  45.     }
  46.     // or greater than the highest ever value.
  47.     if(sensorValue > maxThresh){
  48.       maxThresh = sensorValue;
  49.       Serial.print("maxThresh = ");
  50.       Serial.println(maxThresh);// maxThresh = 613
  51.     }
  52.     delay(5);
  53.   }
  54.   // turn off pin 13 led to let us know we are done calibrating
  55.   digitalWrite(13, LOW);
  56.  
  57.  
  58. }
  59.  
  60. void loop() {
  61.   // read the value from the sensor:
  62.   sensorValue = analogRead(sensorPin);
  63.   sensorValue = constrain(sensorValue, minThresh, maxThresh);
  64.   Serial.println(sensorValue);
  65.  
  66.   int thisPitch = map(sensorValue, minThresh, maxThresh, 100, 1000);
  67.   // play the pitch:
  68.   tone(speakerPin, thisPitch, 10);
  69.  
  70.   //analogWrite(ledPin, map(sensorValue, minThresh, maxThresh, 0, 255));
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement