#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define IR_ENTER_PIN 2
#define IR_BACK_PIN 3
#define IR_CAR1_PIN 4
#define IR_CAR2_PIN 5
#define IR_CAR3_PIN 6
#define IR_CAR4_PIN 7
#define TOTAL_SLOTS 4
int S1 = 0, S2 = 0, S3 = 0, S4 = 0;
int availableSlots = TOTAL_SLOTS;
void setup() {
Serial.begin(9600);
pinMode(IR_CAR1_PIN, INPUT);
pinMode(IR_CAR2_PIN, INPUT);
pinMode(IR_CAR3_PIN, INPUT);
pinMode(IR_CAR4_PIN, INPUT);
pinMode(IR_ENTER_PIN, INPUT);
pinMode(IR_BACK_PIN, INPUT);
myservo.attach(9);
myservo.write(90);
lcd.begin(20, 4);
lcd.setCursor(0, 0);
lcd.print(" Smart Car ");
lcd.setCursor(0, 1);
lcd.print(" Parking System ");
delay(2000);
lcd.clear();
}
void loop() {
Read_Sensors();
Update_LCD();
if (digitalRead(IR_ENTER_PIN) == HIGH ) {
lcd.setCursor(0, 3);
Allocate_Parking();
}
if (digitalRead(IR_BACK_PIN) == HIGH) {
myservo.write(180);
delay(1000);
myservo.write(90);
}
delay(100); // Adjust delay based on system responsiveness
}
void Read_Sensors() {
S1 = digitalRead(IR_CAR1_PIN);
S2 = digitalRead(IR_CAR2_PIN);
S3 = digitalRead(IR_CAR3_PIN);
S4 = digitalRead(IR_CAR4_PIN);
}
void Update_LCD() {
lcd.setCursor(0, 0);
lcd.print(" Available Slots: ");
lcd.print(availableSlots);
lcd.setCursor(0, 1);
lcd.print("S1: ");
lcd.print(S1 == HIGH ? "Fill " : "Empty");
lcd.setCursor(10, 1);
lcd.print("S2: ");
lcd.print(S2 == HIGH ? "Fill " : "Empty");
lcd.setCursor(0, 2);
lcd.print("S3: ");
lcd.print(S3 == HIGH ? "Fill " : "Empty");
lcd.setCursor(10, 2);
lcd.print("S4: ");
lcd.print(S4 == HIGH ? "Fill " : "Empty");
}
void Allocate_Parking(){
for (int i = 0; i < TOTAL_SLOTS; i++) {
if (!digitalRead(IR_CAR1_PIN + i)) { // Find the first empty parking slot
lcd.setCursor(0, 3);
// lcd.print("Parking allocated to slot: ");
if(S1 == HIGH ){
if(S2 == HIGH){
if(S3 == HIGH){
if(S4 == HIGH){
lcd.print("PARKING SPOT FILLED");
}
else{
lcd.print("Allocate Spot 4");
}
}
else{
lcd.print("Allocate Spot 3");
}
}
else{
lcd.print("Allocate Spot 2");
}
}
else{
lcd.print("Allocate Spot 1");
}
lcd.print(" ");
myservo.write(180);
delay(1000); // Adjust delay for gate opening time
myservo.write(90);
availableSlots--;
// delay(100); // Adjust delay based on system responsiveness
return;
}
}
lcd.setCursor(0, 3);
lcd.print("No available parking slot. ");
}