const int pirPin = 2;
const int ledPin = 3;
const int buzzerPin = 4;
const int buttonPin = 5;
int buttonState = 0; // variable for reading the pushbutton status
unsigned long buttonPressTime = 0; // variable to store the time the button was
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
// If the button is pressed (LOW)
if (buttonState == LOW && digitalRead(pirPin) != LOW) {
// Record the time the button was pressed
buttonPressTime = millis();
// Wait for 5 seconds while keeping the button low
while (millis() - buttonPressTime < 5000) {
// Keep the button low
deactivateAlarm();
}
digitalWrite(buttonPin, HIGH);
}
// Check PIR sensor and button status
if (digitalRead(pirPin) == HIGH && buttonState == HIGH) {
activateAlarm();
}
}
void activateAlarm() {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
}
void deactivateAlarm() {
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}