#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Ultrasonic Sensor Pins
const int trigPins[4] = {PA0, PA2, PA4, PA6};
const int echoPins[4] = {PA1, PA3, PA5, PA7};
bool occupied[4];
long readDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0)
return 999;
return duration * 0.034 / 2;
}
void setup() {
Serial.begin(115200);
Wire.begin();
lcd.init();
lcd.backlight();
for (int i = 0; i < 4; i++) {
pinMode(trigPins[i], OUTPUT);
pinMode(echoPins[i], INPUT);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SMART PARKING");
lcd.setCursor(0, 1);
lcd.print("SYSTEM");
delay(2000);
}
void loop() {
int freeSlots = 0;
for (int i = 0; i < 4; i++) {
long d = readDistance(trigPins[i], echoPins[i]);
if (d > 10) {
occupied[i] = false;
freeSlots++;
} else {
occupied[i] = true;
}
}
lcd.clear();
if (freeSlots == 4) {
lcd.setCursor(0, 0);
lcd.print("All Empty");
lcd.setCursor(0, 1);
lcd.print("Free Slots:4");
}
else if (freeSlots == 0) {
lcd.setCursor(0, 0);
lcd.print("Parking Full");
lcd.setCursor(0, 1);
lcd.print("No Empty");
}
else {
lcd.setCursor(0, 0);
lcd.print("Empty:");
int col = 6;
for (int i = 0; i < 4; i++) {
if (!occupied[i]) {
lcd.setCursor(col, 0);
lcd.print("S");
lcd.print(i + 1);
col += 3;
}
}
lcd.setCursor(0, 1);
lcd.print("Free:");
lcd.print(freeSlots);
}
// Serial Monitor Output
Serial.println("----------------");
for (int i = 0; i < 4; i++) {
Serial.print("Slot ");
Serial.print(i + 1);
if (occupied[i])
Serial.println(" : Occupied");
else
Serial.println(" : Empty");
}
delay(1000);
}