// LCD2004 Tiny Pacman on Wokwi
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
//LiquidCrystal(rs, enable, d4, d5, d6, d7) ;
#define LED_GREEN 0
#define LED_RED 1
// set pin numbers:
int latchPin = 13; // Pin connected to ST_CP of 74HC595(Pin13)
int clockPin = 12; // Pin connected to SH_CP of 74HC595(Pin12)
int dataPin = 11; // Pin connected to DS of 74HC595(Pin11)
int P1 = 8 ;
int P2 = 9 ;
int P3 = 10 ;
bool toogleP1 = false;
bool toogleP2 = false;
bool toogleP3 = false;
byte LEDs = 0b01010101;
int counter = 4;
int spaceOne = 1;
int spaceTwo = 2;
int spaceThree = 3;
int spaceFour = 4;
int sensor1Pin = A0; // IR Sensor 1 Pin
int sensor2Pin = A1; // IR Sensor 2 Pin
int sensor3Pin = A2; // IR Sensor 3 Pin
int sensor4Pin = A3; // IR Sensor 4 Pin
void setup() {
lcd.begin(20, 4);
//lcd.begin(cols, rows):
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(P1, INPUT);
pinMode(P2, INPUT);
pinMode(P3, INPUT);
lcd.print(" AVAILABLE");// Print a message to the LCD
lcd.setCursor(3,1);
lcd.print("0");
lcd.print (counter);
lcd.print(" SPACES");
updateShiftRegister();
}
void loop() {
if (digitalRead(P1) == LOW)
{
LEDs = 0b00000000;
updateShiftRegister();
lcd.clear();
lcd.setCursor(3,0);
lcd.print ("System Off");
}
else
{
if (digitalRead(P2) == LOW)
{
if( analogRead(sensor1Pin)>500)
{
updateLEDsParking(spaceOne, LED_RED);
counter--;
delay(100);
}
else
{
updateLEDsParking(spaceOne, LED_GREEN);
counter++;
delay(100);
}
lcd.setCursor(3,1);
lcd.print("0");
lcd.print (counter);
lcd.print(" SPACES");
}
if (digitalRead(P2) == LOW)
{
if( analogRead(sensor2Pin)>500)
{
updateLEDsParking(spaceTwo, LED_RED);
counter--;
delay(100);
}
else
{
updateLEDsParking(spaceTwo, LED_GREEN);
counter++;
delay(100);
}
lcd.setCursor(3,1);
lcd.print("0");
lcd.print (counter);
lcd.print(" SPACES");
}
if (digitalRead(P2) == LOW)
{
if( analogRead(sensor3Pin)>500)
{
updateLEDsParking(spaceThree, LED_RED);
counter--;
delay(100);
}
else
{
updateLEDsParking(spaceThree, LED_GREEN);
counter++;
delay(100);
}
lcd.setCursor(3,1);
lcd.print("0");
lcd.print (counter);
lcd.print(" SPACES");
}
if (digitalRead(P2) == LOW)
{
if( analogRead(sensor4Pin)>500)
{
updateLEDsParking(spaceFour, LED_RED);
counter--;
delay(100);
}
else
{
updateLEDsParking(spaceFour, LED_GREEN);
counter++;
delay(100);
}
lcd.setCursor(3,1);
lcd.print("0");
lcd.print (counter);
lcd.print(" SPACES");
}
}
if (digitalRead(P3) == LOW)
{
LEDs = 0b01010101;
counter=4;
updateShiftRegister();
lcd.clear();
lcd.setCursor(3,0);
lcd.print ("Reset System");
lcd.setCursor(3,1);
lcd.print("0");
lcd.print (counter);
lcd.print(" SPACES");
}
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW); // Output low level to latchPin
shiftOut(dataPin, clockPin, LSBFIRST, LEDs); // Send serial data to 74HC595
digitalWrite(latchPin, HIGH); // Output high level to latchPin, and 74HC595 will update the data to the parallel output port.
}
/*************************************************************/
void updateLEDsParking(int parkingSpace, int status)
{
if (parkingSpace == spaceOne)
{
if(status) LEDs = ((LEDs&0b10111111)| 0b10000000);
else LEDs = ((LEDs&0b01111111)|0b01000000);
}
if (parkingSpace == spaceTwo)
{
if(status) LEDs = ((LEDs&0b11101111)| 0b00100000);
else LEDs = ((LEDs&0b11011111)|0b00010000);
}
if (parkingSpace == spaceThree)
{
if(status) LEDs = ((LEDs&0b11111011)| 0b00001000);
else LEDs = ((LEDs&0b11110111)|0b00000100);
}
if (parkingSpace == spaceFour)
{
if(status) LEDs = ((LEDs&0b11111110)| 0b00000010);
else LEDs = ((LEDs&0b11111101)|0b00000001);
}
updateShiftRegister();
delay(200);
}