Advertisement
learnelectronics

Arduino Dark Activated Motion Alarm

Sep 14th, 2017
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. /*
  2.  * Arduino Dark Activated Motion Alarm
  3.  *
  4.  * learnelectronics
  5.  * 12 SEPT 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  */
  9.  
  10.  
  11.  
  12.  
  13.  
  14. #define ldr   A0                        //ldr is on pin A0 (voltage divider with 1.5k)
  15. #define pir    7                        //pir is on pin 7
  16. #define alm    8                        //active buzzer is on pin 8
  17. #define led    6                        //armed indicator led is on pin 6
  18.  
  19.  
  20.  
  21. int dark = 200;                         //darkness threshold - adjust to suit
  22. bool armed = 0;                         //0 if light, 1 if dark
  23.  
  24.  
  25. void setup() {
  26. pinMode(ldr,INPUT);                    //ldr set as input
  27. pinMode(pir,INPUT);                    //pir set as input
  28. pinMode(alm,OUTPUT);                   //buzzer set as output
  29. pinMode(led,OUTPUT);                   //led set as output
  30. //Serial.begin(9600);
  31.  
  32.  
  33. }
  34.  
  35. void loop() {
  36.  
  37. if (analogRead(ldr)<dark){            //if it's darker than the threshold
  38.   armed = 1;                          //arm the system
  39.   digitalWrite(led,HIGH);             //turn on led
  40. }
  41. else{                                 //if it's not dark enough
  42.   armed = 0;                          //disarm the sytem
  43.   digitalWrite(led,LOW);              //turn of the led
  44. }
  45. //Serial.println(analogRead(ldr));    //for debugging
  46.  
  47. while (armed){                        //if the system is armed
  48.   if (digitalRead(pir) == 1){         //has the pir been triggered
  49.     digitalWrite(alm,HIGH);           //sound the alarm
  50.   }
  51.   else {                              //if the pir has not been triggered
  52.     digitalWrite(alm,LOW);            //silence the alarm
  53.   }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement