#include <Servo.
h>
Servo gateServo;
const int trigPin = 7;
const int echoPin = 6;
const int servoPin = 9;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
[Link](servoPin);
[Link](0); // Gate initially closed
[Link](9600);
}
void loop() {
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read pulse duration
duration = pulseIn(echoPin, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
if (distance < 20) { // Car detected
[Link](90); // Open gate
delay(3000); // Keep open for 3 seconds
[Link](0); // Close gate
delay(1000); // Small delay before next detection
}
delay(100); // Small delay to stabilize sensor
}