// Pin Definitions
const int pirPin = 2; // PIR sensor output pin
const int ledPin = 3; // LED output pin
const int buzzerPin = 4; // Buzzer output pin
const int buttonPin = 5; // Button pin
bool detectionEnabled = false; // To track if detection mode is active
void setup() {
pinMode(pirPin, INPUT); // PIR sensor pin as input
pinMode(ledPin, OUTPUT); // LED pin as output
pinMode(buzzerPin, OUTPUT); // Buzzer pin as output
pinMode(buttonPin, INPUT_PULLUP); // Button pin as input with internal pull-up
Serial.begin(9600); // Initialize serial communication (optional for debugging)
}
void loop() {
// Check if the button is pressed to start detection
if (digitalRead(buttonPin) == LOW) {
detectionEnabled = true; // Enable detection
Serial.println("Detection mode enabled!"); // Optional debug message
delay(500); // Debounce delay
}
if (detectionEnabled) {
int pirState = digitalRead(pirPin); // Read PIR sensor state
if (pirState == HIGH) {
// Movement detected, activate LED and Buzzer
digitalWrite(ledPin, HIGH); // Turn on LED
digitalWrite(buzzerPin, HIGH); // Turn on Buzzer
Serial.println("Motion detected!"); // Optional debug message
delay(500); // Keep LED and Buzzer on for 500ms
} else {
// No movement, turn off LED and Buzzer
digitalWrite(ledPin, LOW); // Turn off LED
digitalWrite(buzzerPin, LOW); // Turn off Buzzer
}
}
}