#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define t1 10
#define t2 9
#define t3 8
#define IR_SENSOR_PIN 7
#define SERVO_PIN 6
#define GREEN_LED1 A0
#define GREEN_LED2 A2
#define GREEN_LED3 A4
#define RED_LED1 A1
#define RED_LED2 A3
#define RED_LED3 A5
Servo servoMotor;
int distanceThreshold = 150;
unsigned long lastServoMoveTime = 0;
int servoPosition = 0;
bool gateOpen = false;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
pinMode(IR_SENSOR_PIN, INPUT_PULLUP);
pinMode(GREEN_LED1, OUTPUT);
pinMode(GREEN_LED2, OUTPUT);
pinMode(GREEN_LED3, OUTPUT);
pinMode(RED_LED1, OUTPUT);
pinMode(RED_LED2, OUTPUT);
pinMode(RED_LED3, OUTPUT);
servoMotor.attach(SERVO_PIN);
servoMotor.write(servoPosition); // Ensure gate starts closed
Serial.begin(9600);
}
long readDistance(int triggerPin, int echoPin) {
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
void handleLEDs(float distance, int greenLedPin, int redLedPin) {
if (distance >= distanceThreshold) {
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
Serial.println("Empty slot!!");
} else {
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
Serial.println("Car is parked");
}
}
void openGate() {
for (int pos = 0; pos <= 90; pos += 1) {
servoMotor.write(pos);
delay(15); // Small delay for smoother movement
}
gateOpen = true;
lastServoMoveTime = millis();
}
void closeGate() {
// Only close the gate if it has been open for at least 5 seconds
if (millis() - lastServoMoveTime > 5000) {
for (int pos = 90; pos >= 0; pos -= 1) {
servoMotor.write(pos);
delay(15); // Small delay for smoother movement
}
gateOpen = false;
}
}
void updateLCD(float d1, float d2, float d3) {
int freeSlots = 0;
if (d1 > 100) freeSlots++;
if (d2 > 100) freeSlots++;
if (d3 > 100) freeSlots++;
lcd.clear();
lcd.setCursor(0, 0);
if (freeSlots == 3) {
lcd.print("3 Slots Free");
lcd.setCursor(0, 1);
lcd.print("Slot 1 2 3 Free");
} else if (freeSlots == 2) {
lcd.print("2 Slots Free");
lcd.setCursor(0, 1);
if (d1 > 100 && d2 > 100)
lcd.print("Slot 1 & 2 Free");
else if (d1 > 100 && d3 > 100)
lcd.print("Slot 1 & 3 Free");
else
lcd.print("Slot 2 & 3 Free");
} else if (freeSlots == 1) {
lcd.print("1 Slot Free");
lcd.setCursor(0, 1);
if (d1 > 100)
lcd.print("Slot 1 is Free");
else if (d2 > 100)
lcd.print("Slot 2 is Free");
else
lcd.print("Slot 3 is Free");
} else {
lcd.print("No Slot Free");
lcd.setCursor(0, 1);
lcd.print("Parking Full");
}
}
void loop() {
float d1 = 0.01723 * readDistance(t1, t1);
float d2 = 0.01723 * readDistance(t2, t2);
float d3 = 0.01723 * readDistance(t3, t3);
Serial.println("d1 = " + String(d1) + "cm");
Serial.println("d2 = " + String(d2) + "cm");
Serial.println("d3 = " + String(d3) + "cm");
bool vehicleDetected = (digitalRead(IR_SENSOR_PIN) == LOW);
bool parkingAvailable = (d1 > 100 || d2 > 100 || d3 > 100);
if (vehicleDetected && parkingAvailable && !gateOpen) {
openGate();
} else if ((!vehicleDetected || !parkingAvailable) && gateOpen) {
closeGate();
}
// Handle LEDs based on the distance readings
handleLEDs(d1, GREEN_LED1, RED_LED1);
handleLEDs(d2, GREEN_LED2, RED_LED2);
handleLEDs(d3, GREEN_LED3, RED_LED3);
// Update LCD
updateLCD(d1, d2, d3);
delay(100); // Short delay to prevent too rapid updates
}