#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin definitions
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // LEDs for parking slots
const int voltagePin = 34; // ADC pin for power supply check (ESP32)
// Parking slots status (0: free, 1: occupied)
int parkingSlots[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Create LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD with I2C address 0x27, 16 columns, 2 rows
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
// Initialize the LCD
lcd.begin(16, 2); // 16 columns, 2 rows
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Smart Parking");
// Initialize LED pins as output
for (int i = 0; i < 10; i++) {
pinMode(ledPins[i], OUTPUT);
}
delay(2000); // Wait for 2 seconds to show the welcome message
}
void loop() {
// Simulate car entry and exit by toggling parking slots every 5 seconds
if (millis() % 5000 == 0) {
int randomSlot = random(10);
parkingSlots[randomSlot] = !parkingSlots[randomSlot]; // Toggle slot status
}
// Update the parking slot status on the LEDs and LCD
updateParkingStatus();
// Check the power supply voltage
checkPowerSupply();
delay(500); // Delay for stability
}
void updateParkingStatus() {
// Clear LCD screen and update parking status
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Parking Slots:");
// Update the LEDs and LCD with parking slot status
for (int i = 0; i < 10; i++) {
digitalWrite(ledPins[i], parkingSlots[i]); // Update the LED for each slot
lcd.setCursor(i, 1); // Set cursor for LCD
if (parkingSlots[i] == 0) {
lcd.print("F"); // Free slot
} else {
lcd.print("X"); // Occupied slot
}
}
}
void checkPowerSupply() {
// Read the ADC value (power supply voltage)
int sensorValue = analogRead(voltagePin); // Read analog value from the ADC pin
float voltage = sensorValue * (3.3 / 4095.0); // Convert the ADC value to voltage
// Print the voltage value to the Serial Monitor for debugging
Serial.print("Power Supply Voltage: ");
Serial.println(voltage);
// Display voltage value on the LCD
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print("Voltage: ");
lcd.print(voltage);
lcd.print("V");
}