#define echoPin 3 // Echo Pin
#define trigPin 2 // Trigger Pin
#define LED_EMPTY 13
#define LED_FULL 11
#define LED_PENDING 12
#define BUZZER 7
void ULT(void);
void displayMessage(const char *line1, const String &line2);
void updateLcdDisplay();
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 14, 15); // Change the HEX address
int IR1 = 2;
int IR2 = 4;
int SmokeDetectorPin = 6; // Digital pin for the smoke detector
int BuzzerPin = 7; // Digital pin for the buzzer
int Slot = 4; // Set the initial available parking slots to 1
int carsParked = 0; // Variable to count the number of cars parked
bool carDetected = false;
unsigned long lastLcdUpdate = 0; // Variable to track the time of the last LCD update
unsigned long lcdUpdateInterval = 1000; // Update the LCD every 1000 milliseconds (1 second)
void setup()
{
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED_EMPTY, OUTPUT);
pinMode(LED_FULL, OUTPUT);
pinMode(LED_PENDING, OUTPUT);
pinMode(BUZZER, OUTPUT);
lcd.begin(16, 2); // Initialize LCD with 16 columns and 2 rows
lcd.backlight();
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
pinMode(SmokeDetectorPin, INPUT);
pinMode(BuzzerPin, OUTPUT);
lcd.setCursor(0, 0);
lcd.print(" ARDUINO ");
lcd.setCursor(0, 1);
lcd.print(" PARKING SYSTEM ");
delay(2000);
lcd.clear();
}
void loop()
{
ULT();
Serial.println(distance); // Show distance
/* vacant, out green light */
if (distance >= 200)
{
digitalWrite(LED_EMPTY, HIGH);
digitalWrite(LED_PENDING, LOW);
digitalWrite(LED_FULL, LOW);
Serial.println("Empty Space.");
carDetected = false; // Reset the flag when the space is empty
}
/* someone is parking, out yellow light */
else if (distance < 200 && distance >= 50)
{
digitalWrite(LED_EMPTY, LOW);
digitalWrite(LED_PENDING, HIGH);
digitalWrite(LED_FULL, LOW);
tone(BUZZER, 800);
delay(100);
digitalWrite(LED_EMPTY, LOW);
digitalWrite(LED_PENDING, LOW);
digitalWrite(LED_FULL, LOW);
noTone(BUZZER);
delay(500);
Serial.println("Car is going to park here or going out.");
// Increment the number of cars parked
carsParked++;
carDetected = true; // Set the flag to true to indicate a car is detected
}
/* occupied, out red light */
else
{
digitalWrite(LED_EMPTY, LOW);
digitalWrite(LED_PENDING, LOW);
digitalWrite(LED_FULL, HIGH);
Serial.println("Car is Parking.");
// Increment the number of cars parked
carsParked++;
carDetected = true; // Set the flag to true to indicate a car is detected
}
if (digitalRead(IR1) == LOW && !carDetected)
{
if (Slot > 0)
{
Slot--;
// Increment the number of cars parked
carsParked++;
carDetected = true; // Set the flag to true to indicate a car is detected
}
else
{
displayMessage(" SORRY :( ", " Parking Full ");
}
}
if (digitalRead(IR2) == LOW && !carDetected)
{
Slot++;
// Decrement the number of cars parked
carsParked--;
carDetected = true; // Set the flag to true to indicate a car is detected
}
// Update the LCD display with a delay
if (millis() - lastLcdUpdate >= lcdUpdateInterval)
{
updateLcdDisplay();
lastLcdUpdate = millis();
}
}
void ULT()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Calculate the distance (in cm) based on the speed of sound.
distance = duration / 58.2;
}
void displayMessage(const char *line1, const String &line2)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
void updateLcdDisplay()
{
if (digitalRead(SmokeDetectorPin) == HIGH)
{
displayMessage(" WARNING! ", " Smoke Detected ");
digitalWrite(BuzzerPin, HIGH); // Turn on the buzzer
}
else
{
displayMessage(" WELCOME! ", "Slot Left: " + String(Slot));
digitalWrite(BuzzerPin, LOW); // Turn off the buzzer
}
}