#include <Servo.h>
const int pirPin = 2; // PIR sensor connected to digital pin 2
const int buzzerPin = 9; // Pin for the buzzer
const int ledPin = 8; // Pin for the LED
const int servoPin = 10; // Pin for the servo motor
Servo servoMotor; // Create a servo object to control the servo motor
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
servoMotor.attach(servoPin); // Attach the servo to its pin
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
// Motion detected
tone(buzzerPin, 1000); // Sound the buzzer at 1000 Hz
digitalWrite(ledPin, HIGH); // Turn on the LED
servoMotor.write(90); // Set servo to middle position
delay(500); // Wait for 0.5 seconds
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
servoMotor.write(0); // Reset servo to starting position
delay(500); // Wait for 0.5 seconds (LED blinks at 1Hz)
} else {
// No motion detected
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
servoMotor.write(0); // Reset servo to starting position
}
}