#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>
// Define the LCD address and dimensions
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change address if necessary
// Define the relay pin
const int relayPin = 10;
const int settingsButtonPin = 11; // Push button for entering settings menu
// Define keypad size and keys
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
// Create keypad instance
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define a preset PIN code length (we'll store 4 digits)
const int PIN_LENGTH = 4;
String correctPIN = ""; // Declare correctPIN as a global variable
String inputPIN = "";
bool lockerUnlocked = false;
// Address in EEPROM to store the PIN
int eepromAddress = 0;
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set relay pin and button pin as output/input
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Initially, the locker is locked (relay off)
pinMode(settingsButtonPin, INPUT_PULLUP); // Button with internal pull-up
// Display welcome message
lcd.setCursor(0, 0);
lcd.print("Locker System");
lcd.setCursor(0, 1);
lcd.print("Enter PIN:");
// Load the correct PIN from EEPROM
correctPIN = readPINFromEEPROM(); // Fix: Initialize correctPIN from EEPROM
}
void loop() {
char key = keypad.getKey();
// Check if the settings button is pressed
if (digitalRead(settingsButtonPin) == LOW) {
enterSettingsMenu(); // Enter the settings menu when button is pressed
delay(300); // Debounce delay
}
if (key) {
if (key == '#') {
// When '#' is pressed, check the PIN
if (inputPIN == correctPIN) {
unlockLocker();
} else {
incorrectPIN();
}
inputPIN = ""; // Reset the input PIN
} else if (key == '*') {
// '*' to reset the input
inputPIN = "";
lcd.setCursor(0, 2);
lcd.print(" "); // Clear the input field
lcd.setCursor(0, 1);
lcd.print("Enter PIN: ");
} else {
// Add the pressed key to the inputPIN string
inputPIN += key;
lcd.setCursor(0, 2);
lcd.print("PIN: " + inputPIN);
}
}
}
void unlockLocker() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PIN Correct!");
lcd.setCursor(0, 1);
lcd.print("Locker Unlocked");
digitalWrite(relayPin, HIGH); // Unlock (turn relay on)
lockerUnlocked = true;
delay(5000); // Keep unlocked for 5 seconds
lockLocker();
}
void lockLocker() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Locker Locked");
digitalWrite(relayPin, LOW); // Lock (turn relay off)
lockerUnlocked = false;
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Locker System");
lcd.setCursor(0, 1);
lcd.print("Enter PIN:");
}
void incorrectPIN() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect PIN!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Locker System");
lcd.setCursor(0, 1);
lcd.print("Enter PIN:");
}
// Enter Settings Menu to change the PIN
void enterSettingsMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Change PIN:");
String newPIN = "";
while (newPIN.length() < PIN_LENGTH) {
char key = keypad.getKey();
if (key) {
newPIN += key;
lcd.setCursor(0, 1);
lcd.print("New PIN: " + newPIN);
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Saving New PIN...");
savePINToEEPROM(newPIN);
correctPIN = newPIN; // Fix: Store the new PIN in correctPIN
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Locker System");
lcd.setCursor(0, 1);
lcd.print("Enter PIN:");
}
// Save the new PIN to EEPROM
void savePINToEEPROM(String pin) {
for (int i = 0; i < PIN_LENGTH; i++) {
EEPROM.write(eepromAddress + i, pin[i]);
}
}
// Read the PIN from EEPROM
String readPINFromEEPROM() {
String pin = "";
for (int i = 0; i < PIN_LENGTH; i++) {
pin += char(EEPROM.read(eepromAddress + i));
}
return pin;
}