#include "LiquidCrystal.h"
const int pirPin = 2;
const int buzzerPin = 7;
LiquidCrystal lcd(12, 11, 5, 4, 3, 10);
volatile bool motionDetected = false;
unsigned long detectionTime = 0;
const unsigned long displayDuration = 5000; // Durada del missatge en mil·lisegons
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pirPin), detectMotion, RISING);
}
void loop() {
if (motionDetected) {
// lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Intruder Alert!");
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
motionDetected = false;
detectionTime = millis(); // Registra el temps de detecció
}
// Esborra el missatge després de displayDuration mil·lisegons
if (millis() - detectionTime > displayDuration) {
lcd.clear();
}
}
void detectMotion() {
motionDetected = true;
}