#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo;
#define entry 2
#define exit 4
#define slot1 5
#define slot2 6
#define slot3 7
int S1 = 0, S2 = 0, S3 = 0;
int flag1 = 0, flag2 = 0;
void setup() {
Serial.begin(9600);
pinMode(slot1, INPUT);
pinMode(slot2, INPUT);
pinMode(slot3, INPUT);
pinMode(entry, INPUT);
pinMode(exit, INPUT);
myservo.attach(3);
myservo.write(90);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 1);
lcd.print(" Hi Welcome ");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Project Title ");
lcd.setCursor(0, 1);
lcd.print(" Car Parking ");
lcd.setCursor(0, 2);
lcd.print(" System ");
delay(5000);
lcd.clear();
Read_Sensor();
}
void loop() {
Read_Sensor();
int slot = 3;
int total = S1 + S2 + S3 ;
slot = slot - total;
lcd.setCursor(0, 0);
lcd.print(" Available Slot: ");
lcd.print(slot);
lcd.print(" ");
lcd.setCursor(0, 1);
if (S1 == 1) {
lcd.print("S1:Full ");
} else {
lcd.print("S1:Empty");
}
lcd.setCursor(11, 1);
if (S2 == 1) {
lcd.print("S2:Full ");
} else {
lcd.print("S2:Empty");
}
lcd.setCursor(0, 2);
if (S3 == 1) {
lcd.print("S3:Full ");
} else {
lcd.print("S3:Empty");
}
if (digitalRead(entry) == 0 && flag1 == 0) {
if (slot > 0) {
flag1 = 1;
if (flag2 == 0) {
myservo.write(0);
slot = slot - 1;
}
} else {
lcd.setCursor(0, 0);
lcd.print(" Parking Full ");
delay(1500);
}
}
if (digitalRead(exit) == 0 && flag2 == 0) {
flag2 = 1;
if (flag1 == 0) {
myservo.write(0);
slot = slot + 1;
}
}
if (flag1 == 1 && flag2 == 1) {
delay(1000);
myservo.write(90);
flag1 = 0, flag2 = 0;
}
delay(1);
}
void Read_Sensor() {
S1 = 0, S2 = 0, S3 = 0;
if (digitalRead(slot1) == 0) {
S1 = 1;
}
if (digitalRead(slot2) == 0) {
S2 = 1;
}
if (digitalRead(slot3) == 0) {
S3 = 1;
}
}