#include <Servo.h>
#define MOTION_SENSOR_PIN 2 // Define the pin for the motion sensor
#define SERVO_PIN 9 // Define the pin for the servo motor
Servo doorServo; // Create a servo object
void setup() {
doorServo.attach(SERVO_PIN); // Attach the servo to the specified pin
pinMode(MOTION_SENSOR_PIN, INPUT); // Set the motion sensor pin as input
doorClosed(); // Ensure the door is closed initially
}
void loop() {
if (digitalRead(MOTION_SENSOR_PIN) == HIGH) { // If motion detected
openDoor(); // Open the door
delay(5000); // Wait for 5 seconds (adjust as needed)
closeDoor(); // Close the door
}
}
void openDoor()
{
doorServo.write(90); // Open the door by rotating servo to 90 degrees
}
void closeDoor()
{
doorServo.write(0); // Close the door by rotating servo to 0 degrees
}
void doorClosed()
{
doorServo.write(0); // Ensure the door is closed by default
}