#include <ESP32Servo.h>
const int irSensorPin = 34; // Analog pin connected to the potentiometer (simulated IR sensor)
const int servoPin = 13; // Digital pin connected to the servo motor
const int threshold = 300; // Threshold value for the potentiometer
const unsigned long doorOpenTime = 5000; // Time in milliseconds the door stays open
Servo myServo;
bool doorOpen = false;
unsigned long lastDetectionTime = 0;
void setup() {
Serial.begin(115200);
myServo.attach(servoPin);
myServo.write(0); // Ensure the door is initially closed
}
void loop() {
int irValue = analogRead(irSensorPin);
Serial.println(irValue);
if (irValue > threshold) {
if (!doorOpen) {
openDoor();
}
lastDetectionTime = millis();
}
if (doorOpen && millis() - lastDetectionTime > doorOpenTime) {
closeDoor();
}
delay(100);
}
void openDoor() {
myServo.write(180); // Rotate servo to open door
doorOpen = true;
}
void closeDoor() {
myServo.write(0); // Rotate servo to close door
doorOpen = false;
}