#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the key map as per your keypad connections
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {9, 8, 7, 6};
// Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
byte colPins[COLS] = {5, 4, 3, 2};
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Predefined credentials
String correctUsername = "user";
String correctPassword = "pass";
String inputUser = "";
String inputPass = "";
// Control variables
int attemptCount = 0;
bool isLocked = false;
unsigned long lockStartTime;
void setup() {
lcd.init(); // Initialize the lcd
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Enter Username:");
}
void loop() {
if (isLocked) {
if (millis() - lockStartTime > 900000) { // 15 minutes lockout
isLocked = false;
attemptCount = 0;
lcd.clear();
lcd.print("Enter Username:");
} else {
return; // Stay locked
}
}
char key = keypad.getKey();
if (key) {
lcd.setCursor(0, 1);
if (inputUser.length() < 4) { // Assuming username is 4 characters
inputUser += key;
lcd.print(inputUser);
} else if (inputPass.length() < 4) { // Assuming password is 4 characters
inputPass += key;
lcd.print('*');
}
if (inputPass.length() == 4) {
if (inputUser == correctUsername && inputPass == correctPassword) {
lcd.clear();
lcd.print("Access Granted");
delay(2000); // Display for 2 seconds
lcd.clear();
lcd.print("Enter Username:");
inputUser = "";
inputPass = "";
} else {
attemptCount++;
if (attemptCount >= 3) {
lcd.clear();
lcd.print("Locked 15 min");
isLocked = true;
lockStartTime = millis();
} else {
lcd.clear();
lcd.print("Try Again");
delay(2000); // Display for 2 seconds
lcd.clear();
lcd.print("Enter Username:");
}
inputUser = "";
inputPass = "";
}
}
}
}