#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
Servo entryGateServo;
Servo exitGateServo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the I2C address, 16 is the number of columns, and 2 is the number of rows
const int entryInfraredPin = 2;
const int exitInfraredPin = 3;
const int maxParkingSpaces = 4; // จำนวนสูงสุดของที่จอดรถ
int carsInside = 0;
void setup() {
entryGateServo.attach(9);
exitGateServo.attach(10);
lcd.begin(16, 2);
pinMode(entryInfraredPin, INPUT);
pinMode(exitInfraredPin, INPUT);
lcd.print("Automatic Parking");
lcd.backlight(); // เปิดแสง LCD
}
void loop() {
int entryInfraredValue = digitalRead(entryInfraredPin);
int exitInfraredValue = digitalRead(exitInfraredPin);
if (entryInfraredValue == HIGH && carsInside < maxParkingSpaces) {
enterParkingLot();
} else if (exitInfraredValue == HIGH && carsInside > 0) {
exitParkingLot();
}
delay(100);
}
void enterParkingLot() {
entryGateServo.write(0); // เปิดประตูทางเข้า
delay(500);
entryGateServo.write(90); // ปิดประตูทางเข้า
carsInside++;
updateLCD();
delay(5000); // แสดงผลเป็นเวลา 5 วินาที
}
void exitParkingLot() {
exitGateServo.write(0); // เปิดประตูทางออก
delay(500);
exitGateServo.write(90); // ปิดประตูทางออก
carsInside--;
updateLCD();
delay(5000);
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Parked Cars: " + String(carsInside) + "/" + String(maxParkingSpaces));
}