#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Declare Servos
Servo servoIn;
Servo servoOut;
// IR Sensor Pins
const int sensorIn = 2; // IR sensor for CAR IN
const int sensorOut = 3; // IR sensor for CAR OUT
// Counter Variables
int carCount = 0;
const int parkingLimit = 5; // Maximum parking capacity
// Servo Positions
int openPosition = 90; // Open position (90 degrees)
int closePosition = 0; // Closed position (0 degrees)
// LCD Initialization
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to your LCD's I2C address
void setup() {
// Attach Servos
servoIn.attach(9); // Servo for CAR IN
servoOut.attach(10); // Servo for CAR OUT
// Set Servo Initial Positions
servoIn.write(closePosition);
servoOut.write(closePosition);
// IR Sensor Pins
pinMode(sensorIn, INPUT);
pinMode(sensorOut, INPUT);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Parking System");
delay(2000); // Display welcome message
lcd.clear();
updateLCD();
// Initialize Serial Monitor
Serial.begin(9600);
}
void loop() {
// Check CAR IN sensor
if (digitalRead(sensorIn) == HIGH) {
if (carCount < parkingLimit) {
Serial.println("Car detected at entry!");
// Open barricade
servoIn.write(openPosition);
delay(3000); // Wait for car to pass
servoIn.write(closePosition);
delay(3000);
// Increment car count
carCount++;
Serial.print("Cars in parking: ");
Serial.println(carCount);
// Update LCD
updateLCD();
} else {
// Parking Full Message
lcd.setCursor(0, 0);
lcd.print("Parking Full!");
lcd.setCursor(0, 1);
lcd.print("Wait to Exit...");
delay(2000);
lcd.clear();
updateLCD();
}
}
// Check CAR OUT sensor
if (digitalRead(sensorOut) == HIGH) {
if (carCount > 0) {
Serial.println("Car detected at exit!");
// Open barricade
servoOut.write(openPosition);
delay(2000); // Wait for car to pass
servoOut.write(closePosition);
delay(500);
// Decrement car count
carCount--;
Serial.print("Cars in parking: ");
Serial.println(carCount);
// Update LCD
updateLCD();
}
}
}
// Function to Update LCD
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cars: ");
lcd.print(carCount);
lcd.setCursor(0, 1);
lcd.print("Limit: ");
lcd.print(parkingLimit);
}