/*
=== Task 3 - PIR Motion Alert Using Boolean Logic ===
Author: Roberto Palozzo
=====================================================
*/
const int PIR_PIN = 4; // PIR motion sensor output pin
const int BUZZER_PIN = 15; // passive buzzer pin
bool afterHours = true; // manually simulates whether it's after working hours
// Plays a rising/falling siren tone by sweeping the buzzer frequency
// up and down between 600Hz and 1600Hz, one step per call
void startSiren() {
static int frequency = 600; // keeps its value between calls (only initialised once)
static int direction = 20; // how much to change the frequency each call, and which way
tone(BUZZER_PIN, frequency); // play the current frequency
frequency += direction; // step the frequency up or down
if (frequency >= 1600) {
direction = -20; // hit the top, start sweeping down
}
else if (frequency <= 600) {
direction = 20; // hit the bottom, start sweeping up
}
}
// Stops the siren sound
void stopSiren() {
noTone(BUZZER_PIN);
}
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT); // PIR sensor as input
pinMode(BUZZER_PIN, OUTPUT); // buzzer as output
stopSiren(); // make sure the buzzer starts silent
}
void loop() {
int motionDetected = digitalRead(PIR_PIN); // read the sensor fresh every loop
// Sound the alarm only when BOTH conditions are true:
// motion is detected AND it's currently after hours
if (afterHours == true && motionDetected == HIGH) {
startSiren();
Serial.println("Detected movements. ALARM!");
}
else {
stopSiren();
Serial.println("Undetected movements. All Good! ");
}
delay(100); // small pause between readings
}