#include <LiquidCrystal_I2C.h> // For 16*2 LCD Display
#include <Wire.h>
//#include <WiFi.h>
#include <ESP32Servo.h> // For Servo Motor
#define SERVO_PIN 2 // Define servo motor pin
#define I2C_SDA 21
#define I2C_SCL 22
TwoWire I2CBME = TwoWire(0); // I2C Bus Create
#define IR1_PIN 12 // Define IR sensor 1 pin
#define IR2_PIN 13 // Define IR sensor 2 pin
#define IR3_PIN 14 // Define IR sensor 3 pin
#define IR4_PIN 15 // Define IR sensor 4 pin
int lcdColumns = 16; // Number of Coloums in LCD Display
int lcdRows = 2; // Number of Rows in LCD Display
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
Servo servo; // Create a Servo object
void setup() {
Wire.begin(I2C_SDA, I2C_SCL);
I2CBME.begin(I2C_SDA, I2C_SCL, 400000); // initialize LCD
lcd.backlight(); // turn on LCD backlight
pinMode(IR1_PIN, INPUT); // Set IR sensor 1 pin as input
pinMode(IR2_PIN, INPUT); // Set IR sensor 2 pin as input
pinMode(IR3_PIN, INPUT); // Set IR sensor 3 pin as input
pinMode(IR4_PIN, INPUT); // Set IR sensor 4 pin as input
servo.attach(SERVO_PIN); // Attach the servo motor to the specified pin
Serial.begin(9600); // Start serial communication
// Start the web server
}
void loop() {
lcd.setCursor(0, 0); // set cursor to first column, first row
lcd.print("Smart Parking"); // print message on Display
int ir1 = digitalRead(IR1_PIN); // Read the state of IR sensor 1
int ir2 = digitalRead(IR2_PIN); // Read the state of IR sensor 2
int ir3 = digitalRead(IR3_PIN); // Read the state of IR sensor 3
int ir4 = digitalRead(IR4_PIN); // Read the state of IR sensor 4
Serial.print("IR Sensor Values: "); // Print Values of sensors on Serial Monitor
Serial.print(ir1);
Serial.print(", ");
Serial.print(ir2);
Serial.print(", ");
Serial.print(ir3);
Serial.print(", ");
Serial.println(ir4);
if (ir1 == HIGH ) { // Entry Sensor detect Obstacle
if (ir3 == LOW) { // SLot 1 is Empty
lcd.setCursor(0,1);
lcd.print("1"); // Print slot Number 1
servo.write(90); // ROtate Servo to 90 Degree
delay(5000); // Wait for 5 Sec
servo.write(-90); // Rotate Servo to -90 Degree
lcd.clear(); // Clear display
}
else if (ir3 == HIGH && ir4 == LOW) { // Slot 2 is Empty
lcd.setCursor(0,1);
lcd.print("2");
servo.write(90);
delay(5000);
servo.write(-90);
lcd.clear();
}
else if (ir3 == LOW && ir4 == LOW) { // Both Slots are Empty
lcd.setCursor(0,1);
lcd.print("1");
delay(2000);
servo.write(90);
delay(5000);
servo.write(-90);
lcd.clear();
}
else if (ir3 == HIGH && ir4 == HIGH) { // Both Slots are Full
lcd.setCursor(0,1);
lcd.print("FULL !!!");
delay(2000);
lcd.clear();
}
}
if (ir2 == HIGH ) { // Exit Sensor detect an obstacle
servo.write(90);
delay(5000);
servo.write(-90);
}
else {
servo.write(0); // No Detection
}
delay(1000); // Wait for 100 milliseconds before checking the IR sensors again
}