Advertisement
Guest User

Untitled

a guest
Feb 26th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. #define timeSeconds 10
  4.  
  5. const int PIR = 25;   // PIR is connected to GPIO 25
  6. const int LED =  16;  // the number of the LED pin
  7.  
  8. unsigned long now = millis();
  9. unsigned long lastTrigger = 0;
  10. boolean startTimer = false;
  11. boolean mtd = false;
  12.  
  13. // Checks if motion was detected, sets LED HIGH and starts a timer
  14. void IRAM_ATTR detectsMovement() {
  15.   digitalWrite(LED, HIGH);
  16.   startTimer = true;
  17.   lastTrigger = millis();
  18. }
  19.  
  20. void setup() {
  21.   Serial.begin(115200);
  22.     pinMode(LED, OUTPUT);
  23.     digitalWrite(LED, LOW);
  24.     pinMode(PIR, INPUT_PULLUP);
  25.     attachInterrupt(digitalPinToInterrupt(PIR), detectsMovement, RISING);
  26. }
  27.  
  28. void loop() {
  29.   // Current time
  30.   now = millis();
  31.   if((digitalRead(LED) == HIGH) && (mtd == false)) {
  32.     Serial.println("MOTION DETECTED!!!");
  33.     mtd = true;
  34.   }
  35.   // Turn off the LED after the number of seconds defined in the timeSeconds variable
  36.   if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
  37.     Serial.println("Motion stopped...");
  38.     digitalWrite(LED, LOW);
  39.     startTimer = false;
  40.     mtd = false;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement