#include <Servo.h>;
#include <LiquidCrystal_I2C.h>;
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define t1 3
#define t2 11
#define t3 9
#define t4 7
#define t5 5
long echoPin;
long trigPin;
int distance1, distance2 , distance3, distance4, distance5 ;
int duration1 , duration2, duration3 ,duration4, duration5 ;
int d1, d2, d3, d4, d5;
Servo myservo;
int distanceThreshold = 100;
int parkingAvailable = 3;
int barrierState = 0;
void setup()
{
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
lcd.clear ();
lcd.print("Parking System");
Serial.begin (9600);
myservo.attach(3);
myservo.write(0);
//long readDistance(int triggerPin, int echoPin)
pinMode(trigPin, OUTPUT);
pinMode (echoPin, INPUT);
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);
//duration = pulseIn(11,HIGH);
//distance = duration *0.034/2;
//lcd.setCursor(0,1);
//lcd.print(distance);
}
void loop()
{
duration1 = pulseIn(12,HIGH);
d1 = duration1 *0.034/2;
lcd.clear();
lcd.println( "d1 ="+ String(d1) +"cm");
duration2 = pulseIn(10,HIGH);
lcd.clear();
d2 = duration2*0.034/2;
lcd.println( "d2 ="+ String(d2) +"cm");
duration3 = pulseIn(8,HIGH);
lcd.clear();
d3 = duration3*0.034/2;
lcd.println( "d3 ="+ String(d3) +"cm");
duration4 = pulseIn(6,HIGH);
lcd.clear();
d4 = duration4*0.034/2;
lcd.println( "d4 ="+ String(d3) +"cm");
duration5 = pulseIn(4,HIGH);
lcd.clear();
d5 = duration5*0.034/2;
lcd.println( "d5 ="+ String(d3) +"cm");
/* For below command
barierState is used to ensure that the barrier either is closed or open for enter or exit
So while barrierState is:
0 barrier is closed
-1 barrier is open for enter
1 barrier is open for exit
-2 barrier is closed after the vehicle passed the barrier gate (enter)
2 barrier is closed after the vehicle passed the barrier gate (exit)
after it reached to -2 or 2 value, it will reset back to 0 when there is no vehicle detected
by two sensor.
For parkingAvailable, we set to 3 as we have 3 parking lot only
so it will automatically update while the barrier is open.
Therefore, the barrier will not be open while there is no more parking lot available.
*/
if (barrierState == 0)
{
if (d4<100; d5>=100 && parkingAvailable>0)
{
parkingAvailable -= 1;
barrierState = -1;
myservo.write(90);
}
if (d4>=100 && d5<100 && parkingAvailable<3)
{
parkingAvailable += 1;
barrierState = 1;
myservo.write(90);
}
}
else if (barrierState == -1)
{
if (d4>=100 && d5<100)
{
barrierState = -2;
myservo.write(0);
}
}
else if (barrierState == 1)
{
if (d5>100 && d4<100)
{
barrierState = 2;
myservo.write(0);
}
}
else if (barrierState == -2)
{
if (d5>=100)
{
barrierState = 0;
}
}
else if (barrierState == 2)
{
if (d4>=100)
{
barrierState = 0;
}
}
/*
The command below is used to print out the information on the LCD Screen
For the first row of the LCD Screen, it will show the number of parking lot available
While it full, it will show Parking Full
For the second row of the LCD Screen, it will show which parking lot is still empty
This allow users to know which parking lot is still available without wasting time
to find an empty parking lot.
*/
lcd.setCursor(0,0);
if (parkingAvailable == 0)
{
lcd.print("Parking Full");
}
else
{
lcd.print("Parking left");
lcd.print(parkingAvailable);
}
if (d1>100 & d2>100 & d3>100)
{
lcd.setCursor(0,1);
lcd.print("Slot 1 2 3 Free");
delay(500);
}
else if((d1>100 & d2>100)|(d2>100 & d3>100)|(d3>=100 & d1>100))
{
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(d1>100 & d2>100 & d3>100)
{
lcd.setCursor(0,1);
lcd.print("Parking Full");
}
else if((d1<100 & d2<100)|(d2<100 & d3<100)|(d3<100 & d1<100))
{
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 ");
}
delay(100);
}