#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Change 0x27 to 0x3F if your LCD doesn't display text
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; // No object detected
return duration * 0.034 / 2;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 4; i++) {
pinMode(trigPins[i], OUTPUT);
pinMode(echoPins[i], INPUT);
}
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SMART PARKING");
lcd.setCursor(0, 1);
lcd.print("SYSTEM");
delay(2000);
}
void loop() {
int freeSlots = 0;
int nearest = -1;
for (int i = 0; i < 4; i++) {
long distance = readDistance(trigPins[i], echoPins[i]);
if (distance > 10) {
occupied[i] = false;
freeSlots++;
if (nearest == -1)
nearest = i + 1;
} else {
occupied[i] = true;
}
}
// Print on Serial Monitor
Serial.println("-----------");
for (int i = 0; i < 4; i++) {
Serial.print("Slot ");
Serial.print(i + 1);
Serial.print(": ");
if (occupied[i])
Serial.println("Occupied");
else
Serial.println("Empty");
}
// LCD Display
lcd.clear();
if (freeSlots == 0) {
lcd.setCursor(0, 0);
lcd.print("PARKING FULL");
lcd.setCursor(0, 1);
lcd.print("No Empty Slot");
} else {
lcd.setCursor(0, 0);
lcd.print("Free:");
lcd.print(freeSlots);
lcd.print("/4");
lcd.setCursor(0, 1);
lcd.print("Next:S");
lcd.print(nearest);
}
delay(1000);
}