Advertisement
3rdWard_Arduino

class3_sensorAutoCalibrate

Jun 17th, 2012
195
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Analog input from photocell to determine LED brightness
  3.  Auto theshold calibration
  4.  Matt Richard
  5.  http://idblab.blogspot.com
  6.  */
  7.  
  8. int sensorPin = A0; // select the input pin for the potentiometer
  9. int ledPin = 3; // select the pin for the LED
  10. int sensorValue = 0; // variable to store the value coming from the sensor
  11. int minThresh = 1023;// replace this number with a lowest number from the serial monitor
  12. int maxThresh = 0;// replace this number with a highest number from the serial monitor
  13.  
  14. void setup() {
  15.   Serial.begin(9600);
  16.   // use the LED attached to pin 13 as a signal when we are autocalibrating
  17.   pinMode(13, OUTPUT);
  18.   digitalWrite(13, HIGH);
  19.  
  20.   // begin auto calibration
  21.   for( int i = 0; i < 1000; i++){
  22.     // grab the sensor value
  23.     sensorValue = analogRead(sensorPin);
  24.  
  25.     // check to see if the sensor value is lower than the minimum threshold
  26.     if( sensorValue < minThresh){
  27.       // if it is, set the minimum threshold to the current sensorValue
  28.       minThresh = sensorValue;
  29.     }
  30.     // check to see if the sensor value is higher than the maximum threshold
  31.     if( sensorValue > maxThresh){
  32.       // if it is, set the maximum threshold to the current sensorValue
  33.       maxThresh = sensorValue;
  34.     }
  35.     delay(5);
  36.   }
  37.  
  38.   digitalWrite(13, LOW);
  39. }
  40.  
  41. void loop() {
  42.   // read the value from the sensor
  43.   sensorValue = analogRead(sensorPin);
  44.   // print the value from the sensor
  45.   Serial.println(sensorValue);
  46.   // constrain the sensor value so that it does not exceed the thresholds
  47.   sensorValue = constrain(sensorValue, minThresh, maxThresh);
  48.   // map the sensor value to the PWM range
  49.   pwmValue = map(sensorValue, minThresh, maxThresh, 0, 255);
  50.   // power the LED using the pwmValue
  51.   analogWrite(ledPin, pwmValue);
  52. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement