Advertisement
celem

ldrtest.ino

Dec 3rd, 2012
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. /*
  2. ** Plot photoresistor(LDR) response curve
  3. ** Edward I. Comer December 2012
  4.  */
  5. // Defines
  6. #define DELAYTIME 10000     // Delay 10 seconds
  7. #define MAXLIGHTLEVEL 25    // Max brightness level
  8. #define STARTLIGHTLEVEL 9      // Starting point
  9. #define INCREMENT 1     // increment to boost light
  10.  
  11. // constants won't change. Used here to
  12. // set pin numbers:
  13. const int localLEDPin =  13;         // the number of the LED pin
  14. const int ldrPin = 1;                // pin number with LDR
  15. const int lightPin = 3;              // pin for flashlight
  16. int LightLevel = STARTLIGHTLEVEL;    // light brightness 0-to-255
  17. int ldrValue = 0;                    // Value read
  18.  
  19. void setup() {
  20.   // set the digital pin as output:
  21.   pinMode(localLEDPin, OUTPUT); // set pin 13 as output
  22.   Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  23. //  Serial.println("ldrtest.ino running");
  24. //  Serial.println("waiting 60 seconds");
  25.   analogWrite(lightPin,0);    // settle at starting light level
  26.   delay(10000);     // wait 10 secs for LDR to settle
  27. //  delay(30000);       // wait 30 secs for LDR to settle
  28.   //Serial.println("\"Light value\",\"LDR value\"");
  29. }
  30.  
  31. void shutdown()
  32. {
  33.     analogWrite(lightPin,0);    // Turn light off
  34.     Serial.print("END");
  35.     Serial.write(0x0a);         // newline character for Linux
  36.     // Rapid blink indicates completion of test
  37.     while(1){  // endless loop
  38.         digitalWrite(localLEDPin, HIGH);   // sets the LED on
  39.     delay(500);                  // waits for a second
  40.     digitalWrite(localLEDPin, LOW);    // sets the LED off
  41.     delay(500);                  // waits for a second
  42.   }
  43. }
  44.  
  45. void loop()
  46. {
  47.     // Turn on local LED to indicate test start
  48.     digitalWrite(localLEDPin, HIGH);        // local LED on
  49.     analogWrite(lightPin,LightLevel);   // set light brightness
  50.     delay(DELAYTIME);                   // wait for LDR to settle
  51.     ldrValue = analogRead(ldrPin);      // read LDR value
  52.     // Turn on local LED to indicate test stopped
  53.     digitalWrite(localLEDPin, LOW);     // local LED off
  54.  
  55.         Serial.print(LightLevel, DEC);      // Print CSV format X-axis
  56.         //Serial.print(", ");
  57.         Serial.print(" ");
  58.         Serial.print(ldrValue, DEC);        // Print CSV format Y-axis
  59.         Serial.write(0x0a);                 // newline character for Linux
  60.    
  61.     LightLevel += INCREMENT;            // increment light brightness
  62.     if(LightLevel > MAXLIGHTLEVEL){
  63.         shutdown();
  64.     }
  65.     LightLevel &= 255;                  // limit variable to 1-byte
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement