#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Pin definitions
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 6;
// Create the servo object
Servo gateServo;
// Create the LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD 16x2 size, adjust I2C address if necessary
long duration;
int distance;
int openAngle = 90; // Servo angle to open the gate
int closedAngle = 0; // Servo angle to close the gate
void setup() {
// Start serial monitor for debugging
Serial.begin(9600);
// Initialize the servo motor
gateServo.attach(servoPin);
gateServo.write(closedAngle); // Initially close the gate
// Initialize LCD
lcd.begin(16, 2);
lcd.setCursor(3, 0); // Set the cursor to column 4, row 0 for centering "COOL MALL"
lcd.print("COOL MALL!");
delay(2000); // Display message for 2 seconds
// Initialize ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Measure the distance using the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0344 / 2; // Distance in cm
// If the distance is less than a certain threshold (car detected), open the gate
if (distance < 15) {
lcd.setCursor(1, 1); // Set the cursor to row 1, column 2 for centering the message
lcd.print("Please Come In");
//delay(500); // Display the message for half a second
// Re-display "COOL MALL" after the car enters
lcd.setCursor(3, 0); // Set the cursor to column 4, row 0 for centering "COOL MALL"
lcd.print("COOL MALL!");
gateServo.write(closedAngle); // Close the gate after car enters
} else {
lcd.clear();
lcd.setCursor(4, 1); // Set the cursor to row 1, column 4 for centering "Welcome!"
lcd.print("Welcome!");
//delay(1000); // Display "Welcome!" for 1 second
// Re-display "COOL MALL"
lcd.setCursor(3, 0); // Set the cursor to column 4, row 0 for centering "COOL MALL"
lcd.print("COOL MALL!");
gateServo.write(openAngle); // Open the gate
}
delay(500); // Delay before next measurement
}