// C++ code
//
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Ultrasonic Sensor Pins
const int trigPin = 2;
const int echoPin = 3;
int Vcc = 5;
int Gnd = 2;
// LED Pins
const int greenLed = 12;
const int redLed = 13;
// Buzzer Pin
const int buzzer = 9;
// Parking Configuration
const int totalSlots = 3;
int availableSlots = totalSlots;
// LCD Configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16x2 display
// Ultrasonic Sensor Parameters
const int maxDistance = 200; // cm
const int parkingThreshold = 50; // Distance to detect vehicle
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.init();
lcd.backlight();
lcd.print("Available Slots:");
updateDisplay();
}
void loop() {
int distance = getDistance();
if(distance <= parkingThreshold && availableSlots > 0) {
availableSlots--;
triggerBuzzer(1);
updateLeds();
updateDisplay();
delay(1000); // Debounce
}
else if(distance > parkingThreshold && availableSlots < totalSlots) {
availableSlots++;
triggerBuzzer(2);
updateLeds();
updateDisplay();
delay(1000); // Debounce
}
}
int getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // Convert to cm
}
void updateLeds() {
digitalWrite(redLed, availableSlots == 0 ? HIGH : LOW);
digitalWrite(greenLed, availableSlots > 0 ? HIGH : LOW);
}
void updateDisplay() {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(availableSlots);
lcd.print("/");
lcd.print(totalSlots);
}
void triggerBuzzer(int beeps) {
for(int i=0; i<beeps; i++) {
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(100);
}
}