const int pirPin = 3;
const int buzzerPin = 5;
const int buttonPin = 8;
bool systemActive = false;
bool lastButtonState = LOW;
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
bool buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastButtonState == LOW) {
delay(50);
if (digitalRead(buttonPin) == HIGH) {
systemActive = !systemActive;
if (systemActive) {
tone(buzzerPin, 2000, 200);
delay(300);
tone(buzzerPin, 2000, 200);
Serial.println("System ON");
} else {
tone(buzzerPin, 500, 200);
delay(300);
tone(buzzerPin, 500, 200);
Serial.println("System OFF");
}
}
}
lastButtonState = buttonState;
if (systemActive) {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
Serial.println("Motion detected! Alarm ON!");
for (int i = 0; i < 3; i++) {
// Nada
tone(buzzerPin, 2500, 300);
delay(300);
}
} else {
// Matikan buzzer
noTone(buzzerPin);
Serial.println("No motion. Alarm OFF!");
}
} else {
noTone(buzzerPin);
}
delay(200);
}