#define LED_PIN 26 // Pin del LED
#define BUZZER_PIN 27 // Pin del buzzer
#define PIR_PIN 34 // Pin del sensor PIR
int estadoAnterior = LOW; // Guarda el último estado del sensor
void setup() {
Serial.begin(115200); // Inicia comunicación serial
pinMode(LED_PIN, OUTPUT); // LED como salida
pinMode(BUZZER_PIN, OUTPUT); // Buzzer como salida
pinMode(PIR_PIN, INPUT); // Sensor como entrada
}
void loop() {
// Leer estado actual del sensor PIR
int estadoActual = digitalRead(PIR_PIN);
// Actualizar LED según el sensor
digitalWrite(LED_PIN, estadoActual);
// Activar buzzer solo al detectar movimiento (flanco ascendente)
if (estadoActual == HIGH && estadoAnterior == LOW) {
tone(BUZZER_PIN, 1000, 100); // 1000Hz durante 100ms
}
// Enviar estado por serial solo cuando cambia
if (estadoActual != estadoAnterior) {
Serial.print("ESTADO:");
Serial.println(estadoActual == HIGH ? "1" : "0");
estadoAnterior = estadoActual; // Actualizar estado guardado
}
delay(50); // Pequeña pausa para no saturar el procesador
}