#include <EEPROM.h> // Include the EEPROM library
#include <LiquidCrystal.h> // Include LCD library
#include <Keypad.h> // Include Keypad library
// LCD Initialization
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
// Keypad Configuration
const byte numRows = 4;
const byte numCols = 4;
char keymap[numRows][numCols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {5, 4, 3, 2};
byte colPins[numCols] = {6, 7, 8, 9};
Keypad mykeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
// Variables for Password Storage and Input
String password = "";
String input_password = "";
bool isEnteringPassword = false; // Track if the user is inputting a password
// Function Declarations
void mainMenu();
void addPassword();
void editPassword();
void deletePassword();
void enterPassword(String message, bool isNewPassword = false);
void displayMessage(String line1, String line2, int delayTime);
void savePasswordToEEPROM(String password);
String readPasswordFromEEPROM();
void clearEEPROM();
// Setup Function
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
delay(100); // Add delay for LCD initialization
pinMode(10, OUTPUT); // Access Granted LED
pinMode(11, OUTPUT); // Access Denied LED
// Load the stored password from EEPROM
password = readPasswordFromEEPROM();
Serial.print("Loaded Password: ");
Serial.println(password); // Debugging line
mainMenu();
}
// Main Menu Loop
void loop() {
char key = mykeypad.getKey();
if (key) {
delay(100); // Debounce delay for keypad
Serial.print("Key Pressed: ");
Serial.println(key); // Debugging line
if (isEnteringPassword) {
if (key == '*') {
// Backspace functionality only if input exists
if (input_password.length() > 0) {
input_password.remove(input_password.length() - 1);
lcd.setCursor(input_password.length(), 1);
lcd.print(" ");
lcd.setCursor(input_password.length(), 1);
} else {
// If no input, return to main menu
mainMenu();
}
}
} else if (key == '*') {
// Always return to main menu if not in password input mode
mainMenu();
} else {
// Handle menu options
switch (key) {
case 'A': // Access Check
enterPassword("Enter PIN:");
if (input_password == password) {
displayMessage("Access", "Granted", 2000);
digitalWrite(10, HIGH); // Access Granted LED On
delay(1000);
digitalWrite(10, LOW); // Access Granted LED Off
} else {
displayMessage("Access Denied", "Invalid Password", 2000);
digitalWrite(11, HIGH); // Access Denied LED On
delay(1000);
digitalWrite(11, LOW); // Access Denied LED Off
}
input_password = "";
mainMenu();
break;
case 'B': // Add Password
if (password != "") {
enterPassword("New Password:", true);
} else {
displayMessage("Password", "Exists", 2000);
}
break;
case 'C': // Edit Password
if (password == "") {
displayMessage("No Password", "Stored", 2000);
} else {
enterPassword("PIN to edit:");
if (input_password == password) {
enterPassword("New Password:", true);
displayMessage("Password", "Updated", 2000);
} else {
displayMessage("Invalid", "Password", 2000);
}
}
input_password = "";
mainMenu();
break;
case 'D': // Delete Password
if (password == "") {
displayMessage("No Password", "Stored", 2000);
} else {
enterPassword("PIN to delete:");
if (input_password == password) {
clearEEPROM();
displayMessage("Password", "Deleted", 2000);
} else {
displayMessage("Delete", "Invalid", 2000);
}
}
input_password = "";
mainMenu();
break;
default: // Invalid Option for other keys
displayMessage("Invalid", "Option", 2000);
mainMenu();
break;
}
}
}
}
// Display the Main Menu
void mainMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Choose: A B C D");
isEnteringPassword = false; // Not in password input mode
}
// Add/Edit Password with Validation
void enterPassword(String message, bool isNewPassword) {
input_password = "";
lcd.clear();
lcd.print(message);
lcd.setCursor(0, 1);
isEnteringPassword = true; // In password input mode
while (true) {
char key = mykeypad.getKey();
if (key) {
delay(100); // Debounce delay
if (key == '#' && input_password.length() == 4) {
// Only submit if the input is exactly 4 digits
if (isNewPassword) {
password = input_password;
savePasswordToEEPROM(password); // Save to EEPROM
displayMessage("Password", "Set Successfully", 2000);
}
break;
}
if (key == '#' && input_password.length() < 4) {
// Warn if the input is less than 4 digits
displayMessage("PIN must be", "4 digits", 2000);
input_password = "";
lcd.clear();
lcd.print(message);
lcd.setCursor(0, 1);
} else if (key == '*' && input_password.length() > 0) {
// Backspace functionality
input_password.remove(input_password.length() - 1);
lcd.setCursor(input_password.length(), 1);
lcd.print(" ");
lcd.setCursor(input_password.length(), 1);
} else if (isDigit(key) && input_password.length() < 4) {
input_password += key;
lcd.print(key); // Directly show input (no masking)
}
}
}
isEnteringPassword = false; // Exit password input mode after submission
}
// Display Message on LCD
void displayMessage(String line1, String line2, int delayTime) {
lcd.clear();
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
delay(delayTime);
lcd.clear();
}
// Save Password to EEPROM
void savePasswordToEEPROM(String password) {
for (int i = 0; i < password.length(); i++) {
EEPROM.write(i, password[i]); // Store each character
delay(5); // Small delay for EEPROM write
}
EEPROM.write(password.length(), '\0'); // Null terminator
delay(5); // Small delay after writing null terminator
Serial.println("Password saved to EEPROM"); // Debugging line
}
// Read Password from EEPROM
String readPasswordFromEEPROM() {
String password = "";
char ch;
for (int i = 0; (ch = EEPROM.read(i)) != '\0'; i++) {
password += ch;
delay(5); // Small delay for EEPROM read
}
Serial.println("Password read from EEPROM: " + password); // Debugging line
return password;
}
// Clear EEPROM
void clearEEPROM() {
for (int i = 0; i < 10; i++) { // Clear the first 10 bytes (enough for a 4-character password)
EEPROM.write(i, '\0');
delay(5); // Small delay for EEPROM clear
}
password = ""; // Clear the stored password variable as well
Serial.println("EEPROM cleared"); // Debugging line
}