const int PIR_PIN = 8;
const int BUZ_PIN = 7;
const int LED_PIN = 6;
const int BTN_PIN = 2;
int state;
void setup() {
Serial.begin(115200); // initializing BAUD RAte
pinMode(BTN_PIN, INPUT_PULLUP); // configure as input pin
pinMode(PIR_PIN, INPUT_PULLUP); // Some PIR sensors are open-collector
pinMode(BUZ_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
state = 0;
}
void loop() {
if (state == 0) {
// Check the PIR sensor
if (digitalRead(PIR_PIN)) { // turn everything on
tone(BUZ_PIN, 440);
digitalWrite(LED_PIN, HIGH);
Serial.print("Motion detected!\t");
Serial.println("Alarm ON");
state = 1;
}
} else { // here state must be 1...
// Check the button
if (!digitalRead(BTN_PIN) ) { // turn everything off
noTone(BUZ_PIN);
digitalWrite(LED_PIN, LOW);
Serial.print("Alarm cancelled \t");
Serial.println("Alarm OFF\n");
state = 0;
}
}
}