#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 bluePin = 6; // Pin for the LED
const int servoPin = 7; // Pin for the servo motor
Servo myServo; // Create a servo object
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(bluePin, OUTPUT);
myServo.attach(servoPin); // Attach the servo to the 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
myServo.write(90); // Rotate the servo to 90 degrees
delay(500); // Wait for 0.5 seconds
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
myServo.write(0); // Rotate the servo to 0 degrees
delay(500); // Wait for 0.5 seconds (LED blinks at 1Hz)
digitalWrite(bluePin, HIGH); // Turn on the LED
myServo.write(0); // Rotate the servo to 0 degrees
delay(500); // Wait for 0.5 seconds
digitalWrite(bluePin, LOW); // Turn on the LED
myServo.write(0); // Rotate the servo to 0 degrees
delay(500); // Wait for 0.5 seconds
} else {
// No motion detected
noTone(buzzerPin); // Turn off the buzzer
digitalWrite(ledPin, LOW); // Turn off the LED
}
}