#include <LiquidCrystal_I2C.h>
//shift register control signals
#define data_pin 11
#define latch_pin 12
#define clock_pin 13
// simulate parking sensors using pushbuttons
#define park4_pb 4
#define park3_pb 5
#define park2_pb 6
#define park1_pb 7
// initial status of parking lots - empty is false, full is true
bool p4status=false;
bool p3status=false;
bool p2status=false;
bool p1status=false;
// storage place for pushbuttons values for each parking slot
int value_park4=1;
int value_park3 =1;
int value_park2=1;
int value_park1 =1;
// storage for available space
int space =0;
// variable to store binary weightage for each parking
int p1,p2,p3,p4,p;
// array that store parking display led that need to be shifted out
byte display[]={0xAA,0xA9,0xA6,0xA5,0x9A,0x99,0x96,0x95,0x6A,0x69,0x66,0x65,0x5A,0x59,0x56,0x55};
// create an instance on liquid crystal object
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
// all shift register control signals are output
pinMode(data_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
//all parking sensors are input
pinMode(park4_pb, INPUT);
pinMode(park3_pb, INPUT);
pinMode(park2_pb, INPUT);
pinMode(park1_pb, INPUT);
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on backlight
}
void loop()
{
// read parking sensor values and store in respective variable
value_park4 = digitalRead(park4_pb);
value_park3 = digitalRead(park3_pb);
value_park2 = digitalRead(park2_pb);
value_park1 = digitalRead(park1_pb);
delay(200); // delay to avoid debouncing
// parking sensors are active low; when full the logic is 0
if(value_park4==0) // if park 4 is full
{
p4status=!p4status; // change the status from false to true
(p4status==true)? p4=1 :p4 =0; // if park 4 status is full, then assign weight 1
}
if(value_park3==0)
{
p3status=!p3status;
(p3status==true)? p3=2 : p3 =0;
}
if(value_park2==0)
{
p2status=!p2status;
(p2status==true)? p2=4 : p2 =0;
}
if(value_park1==0)
{
p1status=!p1status;
(p1status==true)? p1=8 : p1 =0;
}
//add all the weightage to determine the data that need to send to shift register
int p=p4+p3+p2+p1;
// call the function that send data to shift register and turn on related led
display_led(p);
}
void display_led( int p)
{
for ( int i=0;i<16;i++)
{
if( i==p)
{
// send data to shift register and shift out parallelly
digitalWrite(latch_pin, LOW); // Output low level to latchPin
shiftOut(data_pin, clock_pin, MSBFIRST, display[i]); // Send serial data to 74HC595
digitalWrite(latch_pin, HIGH);
// display available space based on p value
if (p==0)
{
lcd.setCursor(4,0);
lcd.print("AVAILABLE");
lcd.setCursor(4,1);
lcd.print("4 SPACES");
}
else if (p==1 || p==2|| p==4||p==8)
{
lcd.setCursor(4,0);
lcd.print("AVAILABLE");
lcd.setCursor(4,1);
lcd.print("3 SPACES");
}
else if (p==3 || p==5|| p==6||p==9||p==10||p==12)
{
lcd.setCursor(4,0);
lcd.print("AVAILABLE");
lcd.setCursor(4,1);
lcd.print("2 SPACES");
}
else if (p==7 || p==11|| p==13||p==14)
{
lcd.setCursor(4,0);
lcd.print("AVAILABLE");
lcd.setCursor(4,1);
lcd.print("1 SPACES");
}
else
{
lcd.setCursor(4,0);
lcd.print("AVAILABLE");
lcd.setCursor(4,1);
lcd.print("0 SPACE");
}
}
}
}