//Sensor de temperatura y humedad
#
//Pograma editado por:BLANCO CERON JOSUE ANDRE, CASTILLO LOPEZ DONALDO, RAFAEL ZAMUDIO
#
// Xalapa, Veracruz, México
#
//2023 - Septiembre
#
#define timeSeconds 10
// Configuracion GPIO para LED y sensor de movimiento PIR
const int led = 26;
const int motionSensor = 27;
// Temporizador: Variables auxiliares
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
boolean motion = false;
// comprobacion de que el led funcione con el movimiento
void IRAM_ATTR detectsMovement() {
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}
void setup() {
// Puerto serie para fines de depuración
Serial.begin(115200);
// Modo sensor de movimiento PIR INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Establece el pin del sensor de movimiento como interrupción,
// se asigna la función de interrupción y configura el modo RISING
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
// se establece el LED en BAJO
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
// Tiempo actual
now = millis();
if((digitalRead(led) == HIGH) && (motion == false)) {
Serial.println("MOTION DETECTED!!!");
motion = true;
}
//Se apaga el LED después del número de segundos definido en la variable timeSeconds
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
startTimer = false;
motion = false;
}
}