#include <ESP32Servo.h>
const int trigPin = 26;
const int echoPin = 27;
Servo myServo;
void setup() {
myServo.attach(14); // Servo connected to GPIO 9
pinMode(trigPin, OUTPUT); // Ultrasonic trig pin
pinMode(echoPin, INPUT); // Ultrasonic echo pin
myServo.write(0); // Start with gate closed
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2;
if (distance >= 100) {
myServo.write(90); // Open gate if distance <= 100 cm
}
else {
myServo.write(0);
}
delay(100); // Wait for 500 ms before next reading
}