#include <Servo.h> // Include the Servo library
const int pirPin = 6; // Digital pin connected to the PIR sensor
const int buzzerPin = 8; // Digital pin connected to the passive buzzer
const int servoPin = 3; // Digital pin connected to the servo motor
Servo myServo; // Create a Servo object
void setup() {
pinMode(pirPin, INPUT); // Set PIR sensor pin as input
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
myServo.attach(servoPin); // Attach the servo motor to the specified pin
myServo.write(0); // Initialize servo to 0 degrees
}
void loop() {
int pirState = digitalRead(pirPin); // Read the state of the PIR sensor
if (pirState == HIGH) { // If motion is detected
Serial.println("Motion detected!");
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer (makes sound if it's a passive buzzer and you're controlling it with HIGH/LOW)
delay(500); // Keep buzzer on for 0.5 seconds
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
myServo.write(90); // Rotate servo to 90 degrees
Serial.println("Action completed. Waiting for next motion...");
delay(500); // Add a small delay after an event to avoid rapid re-triggering
} else {
// No motion detected, do nothing or you can add code here for no motion state
// Serial.println("No motion"); // Uncomment this line if you want to see "No motion" continuously
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off when no motion
}
}