#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// LCD I2C
LiquidCrystal_I2C lcd(0x27,16,2);
Servo towerServo;
// IR Pins لكل ركنة
int IR1 = 2;
int IR2 = 3;
int IR3 = 4;
int IR4 = 5;
// Exit Buttons
int exitBtn1 = 10;
int exitBtn2 = 11;
int exitBtn3 = 12;
int exitBtn4 = 13;
// Ultrasonic Sensor
int trigPin = 6;
int echoPin = 7;
// حالة الركنات
int slot1 = 0;
int slot2 = 0;
int slot3 = 0;
int slot4 = 0;
// زاوية السيرفو لكل ركنة
int angles[4] = {0, 90, 180, 270};
void setup() {
Serial.begin(9600);
// IR
pinMode(IR1, INPUT);
pinMode(IR2, INPUT);
pinMode(IR3, INPUT);
pinMode(IR4, INPUT);
// Exit Buttons
pinMode(exitBtn1, INPUT_PULLUP);
pinMode(exitBtn2, INPUT_PULLUP);
pinMode(exitBtn3, INPUT_PULLUP);
pinMode(exitBtn4, INPUT_PULLUP);
// Ultrasonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Servo
towerServo.attach(9); // Servo على Pin 9
// LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Smart Parking");
delay(2000);
lcd.clear();
}
long readUltrasonicCM() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2;
}
void loop() {
// قراءة الركنات
slot1 = digitalRead(IR1);
slot2 = digitalRead(IR2);
slot3 = digitalRead(IR3);
slot4 = digitalRead(IR4);
// عرض الركنات على LCD
lcd.setCursor(0,0);
lcd.print("S1:"); lcd.print(slot1?"Occ":"Emp");
lcd.print(" S2:"); lcd.print(slot2?"Occ":"Emp");
lcd.setCursor(0,1);
lcd.print("S3:"); lcd.print(slot3?"Occ":"Emp");
lcd.print(" S4:"); lcd.print(slot4?"Occ":"Emp");
// Ultrasonic: اكتشاف العربية عند المدخل (مدى < 50 سم)
long distance = readUltrasonicCM();
if(distance > 0 && distance < 50){
if(slot1 == 0){
towerServo.write(angles[0]);
slot1 = 1;
delay(2000);
}
else if(slot2 == 0){
towerServo.write(angles[1]);
slot2 = 1;
delay(2000);
}
else if(slot3 == 0){
towerServo.write(angles[2]);
slot3 = 1;
delay(2000);
}
else if(slot4 == 0){
towerServo.write(angles[3]);
slot4 = 1;
delay(2000);
}
else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Parking Full!");
delay(1500);
}
}
// أزرار خروج لكل ركنة
if(digitalRead(exitBtn1) == LOW && slot1 == 1){
towerServo.write(angles[0]);
slot1 = 0;
delay(2000);
}
if(digitalRead(exitBtn2) == LOW && slot2 == 1){
towerServo.write(angles[1]);
slot2 = 0;
delay(2000);
}
if(digitalRead(exitBtn3) == LOW && slot3 == 1){
towerServo.write(angles[2]);
slot3 = 0;
delay(2000);
}
if(digitalRead(exitBtn4) == LOW && slot4 == 1){
towerServo.write(angles[3]);
slot4 = 0;
delay(2000);
}
delay(500);
}