#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const int PIRPin = 2;
const int ultrasonicDistanceSensorTrig_Pin = 13;
const int ultrasonicDistanceSensorEcho_Pin = 12;
const int keypadPin = 4;
const int lcdDisplayPin = 5;
const int Green_LEDPin = 27;
const int Red_LEDPin = 26;
String Username = "221234516"; // password
String UserInput = "";
int incorrectAttempts = 0; // Counter for incorrect attempts
LiquidCrystal_I2C lcdDisplay(0x27, 16, 2);
// Define keypad layout
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] = {22, 23, 24, 25};
byte colPins[COLS] = {26, 27, 28, 29};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
pinMode(PIRPin, INPUT);
pinMode(ultrasonicDistanceSensorEcho_Pin, INPUT);
pinMode(ultrasonicDistanceSensorTrig_Pin, OUTPUT);
pinMode(keypadPin, INPUT);
pinMode(lcdDisplayPin, OUTPUT);
pinMode(Green_LEDPin, OUTPUT);
pinMode(Red_LEDPin, OUTPUT);
lcdDisplay.init();
lcdDisplay.backlight();
lcdDisplay.clear();
lcdDisplay.setCursor(0, 0);
lcdDisplay.print("Enter Username");
}
void loop() {
int humanPresence = digitalRead(PIRPin);
int vehiclePresence = digitalRead(ultrasonicDistanceSensorEcho_Pin);
if (vehiclePresence == HIGH) {
Serial.println("Vehicle Detected");
}
if (humanPresence == HIGH) {
String enteredUsername = "";
char key;
do {
key = keypad.getKey();
if (key) {
enteredUsername += key;
lcdDisplay.print(key);
}
} while (key && enteredUsername.length() < Username.length());
if (enteredUsername == Username) {
accessGranted();
incorrectAttempts = 0; // Reset incorrect attempt counter
} else {
accessDenied();
incorrectAttempts++; // Increment incorrect attempt counter
if (incorrectAttempts >= 3) { // Check if three or more incorrect attempts
alarmTriggered();
incorrectAttempts = 0; // Reset incorrect attempt counter
}
}
} else {
accessDenied();
}
if (vehiclePresence == HIGH) {
digitalWrite(Red_LEDPin, HIGH);
delay(1000);
digitalWrite(Red_LEDPin, LOW);
}
delay(1000);
}
void accessGranted() {
lcdDisplay.clear();
lcdDisplay.setCursor(0, 0);
lcdDisplay.print("Access: Granted");
digitalWrite(Green_LEDPin, HIGH);
delay(1000);
digitalWrite(Green_LEDPin, LOW);
}
void accessDenied() {
lcdDisplay.clear();
lcdDisplay.setCursor(0, 0);
lcdDisplay.print("Access: Denied");
digitalWrite(Red_LEDPin, HIGH);
delay(1000);
digitalWrite(Red_LEDPin, LOW);
}
void alarmTriggered() {
Serial.println("Alert: Maximum incorrect attempts reached!");
for (int i = 0; i < 3; i++) { // Blink the red LED 3 times
digitalWrite(Red_LEDPin, HIGH);
delay(500);
digitalWrite(Red_LEDPin, LOW);
delay(500);
}
}