#include <Servo.h>
Servo motorServo; // Define motorServo as object for Servo to control the Motor Servo
const int motorPin = 7; // Pin for the Motor Servo to digital pin 7
const int pirPin = 2; // PIR sensor connected to digital pin 2
const int buzzerPin = 9; // Pin for the buzzer to digital pin 9
const int ledPin = 8; // Pin for the LED to digital pin 8
void setup() {
motorServo.attach(motorPin);
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
// Motion detected
motorServo.write(180); // Set the Motor Servo angle to 180 degrees
tone(buzzerPin, 1000); // Sound the buzzer at 1000 Hz
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(500); // Wait for 0.5 seconds
motorServo.write(0); // Set the Motor Servo angle back to 0 degrees
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
delay(500); // Wait for 0.5 seconds (LED blinks at 1Hz)
} else {
// No motion detected
motorServo.write(0); // Turn off the Motor Servo
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
}
}