#include "pitches.h"
int SPEAKER_PIN = 13;
int PIR = 2;
int pirState = LOW;
int val = 0;
void setup() {
pinMode (SPEAKER_PIN, OUTPUT);
pinMode(PIR, INPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(PIR);
if (val == HIGH) {
Alarm();
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void Alarm() {
tone(SPEAKER_PIN, NOTE_D8);
delay(120);
tone(SPEAKER_PIN, NOTE_B6);
delay(120);
noTone(SPEAKER_PIN);
}