#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <Keypad.h>
const int trigPin = 2;
const int echoPin = 4;
const int greenLEDPin = 16;
const int redLEDPin = 17;
const int buzzerPin = 18;
const int servoPin = 19;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo gateServo;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {34, 35, 32, 33}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {25, 26, 27, 14}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String password = "679";
String enteredPassword = "";
unsigned long startTime;
bool carInside = false;
bool carDetected = false;
const float costPerHour = 2.0;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
gateServo.attach(servoPin);
gateServo.write(0); // Closed position
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
void loop() {
detectCar();
if (!carDetected) {
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
noTone(buzzerPin);
} else {
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
tone(buzzerPin, 1000);
}
char key = keypad.getKey();
if (key) {
if (key == '#') {
if (enteredPassword == password) {
if (carInside) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Amount:");
float amountPaid = getAmountPaid();
unsigned long endTime = millis();
unsigned long totalHours = (endTime - startTime) / (1000 * 60 * 60);
float totalCost = totalHours * costPerHour;
if (amountPaid >= totalCost) {
gateServo.write(90); // Open position
delay(5000); // Keep gate open for 5 seconds
gateServo.write(0); // Close position
carInside = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Insufficient Funds");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
} else {
gateServo.write(90); // Open position
delay(5000); // Keep gate open for 5 seconds
gateServo.write(0); // Close position
startTime = millis();
carInside = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong Password");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
}
enteredPassword = "";
} else {
enteredPassword += key;
lcd.setCursor(0, 1);
lcd.print(enteredPassword);
}
}
}
void detectCar() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.034) / 2;
if (distance < 10) {
carDetected = true;
} else {
carDetected = false;
}
}
float getAmountPaid() {
String amountStr = "";
lcd.setCursor(0, 1);
lcd.print(amountStr);
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
return amountStr.toFloat();
} else {
amountStr += key;
lcd.setCursor(0, 1);
lcd.print(amountStr);
}
}
}
}