Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #define timeSeconds 10
- const int PIR = 25; // PIR is connected to GPIO 25
- const int LED = 16; // the number of the LED pin
- unsigned long now = millis();
- unsigned long lastTrigger = 0;
- boolean startTimer = false;
- boolean mtd = false;
- // Checks if motion was detected, sets LED HIGH and starts a timer
- void IRAM_ATTR detectsMovement() {
- digitalWrite(LED, HIGH);
- startTimer = true;
- lastTrigger = millis();
- }
- void setup() {
- Serial.begin(115200);
- pinMode(LED, OUTPUT);
- digitalWrite(LED, LOW);
- pinMode(PIR, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(PIR), detectsMovement, RISING);
- }
- void loop() {
- // Current time
- now = millis();
- if((digitalRead(LED) == HIGH) && (mtd == false)) {
- Serial.println("MOTION DETECTED!!!");
- mtd = true;
- }
- // Turn off the LED after the number of seconds defined in the timeSeconds variable
- if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
- Serial.println("Motion stopped...");
- digitalWrite(LED, LOW);
- startTimer = false;
- mtd = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement