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);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(PIR_PIN, INPUT_PULLUP);
pinMode(BUZ_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
state = 0;
}
void loop() {
if (state == 0) {
// ceck the PIR sensor
if (digitalRead(PIR_PIN)) {// trun everything on
tone(BUZ_PIN, 440);
digitalWrite(LED_PIN, HIGH);
Serial.print("Motion detected!\t");
Serial.println("Alarm On");
state = 0;
}
}
else { // here state must be 1...
// check the buttom
if (digitalRead(BTN_PIN) ) { //turn everything off
noTone(BUZ_PIN);
digitalWrite(LED_PIN, LOW);
Serial.print("Alarm canceled \t");
Serial.println("Alarm Off\n");
state = 0;
}
}
}