#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define trigPin 9
#define echoPin 8
#define buzzerPin 10
#define ledAlertPin 11
#define ledClearPin 12
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 chars, 2 rows
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledAlertPin, OUTPUT);
pinMode(ledClearPin, OUTPUT);
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Parking System");
delay(2000); // Display the message for 2 seconds
lcd.clear();
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin high for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin and calculate the distance
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Display the distance on the LCD
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm "); // Clear remaining characters
// If the distance is less than 100 cm, activate the continuous beep and alert LED
if (distance < 300 && distance > 0) { // Adjust if necessary
tone(buzzerPin, 1000); // Produce continuous beep at 1000 Hz
digitalWrite(ledAlertPin, HIGH);
digitalWrite(ledClearPin, LOW);
// Display alert message on the LCD
lcd.setCursor(0, 1); // Move to second row
lcd.print("Vehicle Detected");
} else {
// Otherwise, stop the beep and show clear status
noTone(buzzerPin); // Stop the beep
digitalWrite(ledAlertPin, LOW);
digitalWrite(ledClearPin, HIGH);
// Display clear road message on the LCD
lcd.setCursor(0, 1);
lcd.print("Road is Clear "); // Clear remaining characters
}
delay(500); // Delay for half a second
}