/*
  Arduino | hardware-help
  Need help with school project regarding automatic door
  involving PIR sensor and servo motor

  dunkindonkey — 11/13/24 at 11:26 PM
*/

#include <Servo.h>

const int pirPin = 2; // PIR sensor output connected to pin 2
const int servoPin = 9; // Servo motor connected to pin 9

Servo doorServo;
bool motionDetected = false;

void setup() {
  pinMode(pirPin, INPUT);
  doorServo.attach(servoPin);
  doorServo.write(0); // Start with the door closed
  Serial.begin(9600);
}

void loop() {
  int pirValue = digitalRead(pirPin);

  if (pirValue == HIGH && !motionDetected) { // Motion detected
    motionDetected = true;
    doorServo.write(90); // Open the door (90 degrees)
    Serial.println("Motion detected: Door opening");
    delay(5000); // Wait for 5 seconds
    doorServo.write(0); // Close the door (0 degrees)
    Serial.println("Door closing");
    motionDetected = false;
  }
}