Advertisement
grist

MotionActivatedLightSwitch.ino

Jan 9th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. /*
  2.  * MotionActivatedLightSwitch.ino
  3.  *
  4.  * Created: 20/12/2012 4:38:29 PM
  5.  *  Author: grist.carrigafoyl
  6.  *  Target: Arduino Uno
  7.  
  8.  Activate a pin when motion is detected via a PIR sensor on another pin.
  9.  Checks ambient light level via a photoresistor and only activates if the light level is low
  10.  
  11.  
  12.  */
  13.  
  14. #define PHOTORESISTOR           1  // Analog in
  15. #define PIR_PIN         8  // Digital in
  16. #define SWITCH_PIN      13 // Digital out
  17.  
  18. // The light level below which the switch will trigger.
  19. #define LOW_LIGHT_LEVEL     300 // Based on experimentation.
  20.  
  21.  
  22. // Prototypes
  23. int read_adc(char pin);
  24.  
  25. void setup()
  26. {
  27.   pinMode(PIR_PIN, INPUT);
  28.   pinMode(SWITCH_PIN, OUTPUT);  
  29.    
  30. }
  31.  
  32. void loop()
  33. {
  34.       byte pir_state;
  35.       int curr_light_level;
  36.  
  37.       // How long the light stays on for. Simple constant for now
  38.       int delay_time = 5000;
  39.      
  40.       // Loop forever
  41.       for (;;) {
  42.             pir_state = digitalRead(PIR_PIN);
  43.             if (pir_state == LOW) {  // Movement detected (pin is pulled high normally)
  44.                 curr_light_level = read_adc(PHOTORESISTOR);
  45.                 if (curr_light_level < LOW_LIGHT_LEVEL) {
  46.                     digitalWrite(SWITCH_PIN, HIGH);  // On
  47.                     delay(delay_time);
  48.                 }
  49.             }
  50.             digitalWrite(SWITCH_PIN, LOW); // Off
  51.         }
  52. }
  53.  
  54. int read_adc(char pin)
  55. { // Read the analog value from the ADC input pin.
  56.     int ret_val;
  57.  
  58.     ret_val = analogRead(pin);
  59.     return(ret_val);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement