#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// IR sensor pins
#define IR_SENSOR_1_PIN 2
#define IR_SENSOR_2_PIN 3
// Servo motor pin
#define SERVO_PIN 9
// LCD configuration
#define LCD_ADDRESS 0x27 // I2C address of the LCD
#define LCD_COLS 16 // Number of columns in the LCD
#define LCD_ROWS 2 // Number of rows in the LCD
LiquidCrystal_I2C lcd(0x27, 16, 1);
Servo gateServo;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Attach servo motor
gateServo.attach(SERVO_PIN);
// Set IR sensor pins as inputs
pinMode(IR_SENSOR_1_PIN, INPUT);
pinMode(IR_SENSOR_2_PIN, INPUT);
}
void loop() {
// Read IR sensor values
int irSensor1Value = digitalRead(IR_SENSOR_1_PIN);
int irSensor2Value = digitalRead(IR_SENSOR_2_PIN);
// Display status on LCD
lcd.setCursor(0, 0);
lcd.print("Parking 1: ");
lcd.print(irSensor1Value == HIGH ? "Occupied" : "Vacant");
lcd.setCursor(0, 1);
lcd.print("Parking 2: ");
lcd.print(irSensor2Value == HIGH ? "Occupied" : "Vacant");
// Check if both parking spaces are occupied
if (irSensor1Value == HIGH && irSensor2Value == HIGH) {
openGate(); // If both spaces are occupied, open the gate
} else {
closeGate(); // Otherwise, keep the gate closed
}
delay(1000); // Delay for stability
}
void openGate() {
gateServo.write(90); // Open the gate
lcd.setCursor(0, 1);
lcd.print("Gate: Open ");
}
void closeGate() {
gateServo.write(0); // Close the gate
lcd.setCursor(0, 1);
lcd.print("Gate: Closed ");
}