#define timeSeconds 3
// Set GPIOs for LED and PIR Motion Sensor
const int led = 33;
const int motionSensor = 32;
int32_t contador = 0;
// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
boolean motion = 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 port for debugging purposes
Serial.begin(115200);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Ajuste del pin motionSensor como interrupción, estableciendo la función y el modo
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
// Set LED to LOW
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
// Tiempo Actual
now = millis();
if((digitalRead(led) == HIGH) && (motion == false)) {
contador++;
Serial.print(String(contador) + " MOTION DETECTED!!! ");
motion = true;
}
// ApagueSe apaga el LED después del número de segundos definido en la variable timeSeconds
// que contiene el tiempo en milisegundos.
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("-> Motion stopped... in " + String(timeSeconds*1000) + "[s]");
digitalWrite(led, LOW);
startTimer = false;
motion = false;
}
delay(10);
}