#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define TRIGGER_ENTRANCE_PIN 4
#define ECHO_ENTRANCE_PIN 5
#define TRIGGER_EXIT_PIN 6
#define ECHO_EXIT_PIN 7
#define SERVO_PIN 3
#define SLOT_CAPACITY 4
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo parkingServo;
int availableSlots = SLOT_CAPACITY;
void setup() {
pinMode(TRIGGER_ENTRANCE_PIN, OUTPUT);
pinMode(ECHO_ENTRANCE_PIN, INPUT);
pinMode(TRIGGER_EXIT_PIN, OUTPUT);
pinMode(ECHO_EXIT_PIN, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("SMART PARKING");
delay(2000);
lcd.clear();
parkingServo.attach(SERVO_PIN);
parkingServo.write(0); // Set servo to initial position
}
void loop() {
int entranceDistance = detectDistance(TRIGGER_ENTRANCE_PIN, ECHO_ENTRANCE_PIN);
int exitDistance = detectDistance(TRIGGER_EXIT_PIN, ECHO_EXIT_PIN);
lcd.setCursor(0, 0);
lcd.print("Welcome");
lcd.setCursor(0, 1);
if (availableSlots > 0) {
lcd.print("Slots: ");
lcd.print(availableSlots);
delay(1000);
lcd.clear();
if (entranceDistance <= 30) {
lcd.setCursor(0, 0);
lcd.print("Car Entering!");
delay(2000);
lcd.clear();
availableSlots--;
moveServo(); // Move servo to allow the car in
}
if (exitDistance <= 30) {
lcd.setCursor(0, 0);
lcd.print("Car Exiting!");
delay(2000);
lcd.clear();
availableSlots++;
moveServo(); // Move servo to allow the car out
}
} else {
lcd.print("Parking full!");
delay(1000);
lcd.clear();
}
}
int detectDistance(int triggerPin, int echoPin) {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
int duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
void moveServo() {
parkingServo.write(90); // Move the servo to open position
delay(2000);
parkingServo.write(0); // Move the servo back to initial position
delay(500);
}