Advertisement
3rdWard_Arduino

Class3_photocell auto calibrate

Feb 8th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. int sensorPin = A0;    // select the input pin for the potentiometer
  2. int ledPin = 11;      // select the pin for the LED
  3. int sensorValue = 0;  // variable to store the value coming from the sensor
  4. int lowestVal = 1023;
  5. int highestVal = 0;
  6. int pwmVal = 0;
  7.  
  8. void setup() {
  9.   // declare the ledPin as an OUTPUT:
  10.   pinMode(ledPin, OUTPUT);  
  11.   Serial.begin(9600);
  12.   for(int i = 0; i < 3000; i++){
  13.     // read the current value from the photocell
  14.     sensorValue = analogRead(sensorPin);
  15.     //check to see if the value is greater than highestVal ...
  16.     if( sensorValue > highestVal){
  17.      highestVal = sensorValue;
  18.     }
  19.     // or less than lowestVal
  20.     if( sensorValue < lowestVal){
  21.      lowestVal = sensorValue;
  22.     }
  23.     delay(1);
  24.   }
  25. }
  26.  
  27. void loop() {
  28.   // read the value from the sensor:
  29.   sensorValue = analogRead(sensorPin);
  30.   Serial.println( sensorValue );
  31.   sensorValue = constrain(sensorValue, lowestVal, highestVal);
  32.   pwmVal = map( sensorValue, lowestVal, highestVal, 0, 255);
  33.   analogWrite(ledPin, pwmVal);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement