#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#define RELAY 10
#define OFFLED A0
#define ONLED A1
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
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Custom character for the menu
byte ukazatel[8] = {
0b00000,
0b00000,
0b00100,
0b01110,
0b01110,
0b00100,
0b00000,
0b00000
};
// Variables to store the lock settings
String password = "";
String newPassword = "";
String confirmPassword = "";
int menuIndex = 0;
int unlockTime = 5;
bool automaticRegime = true;
// Function prototypes
void showLoadingScreen();
void checkPassword();
void showMenu();
void changePassword();
void changeRegimes();
void changeUnlockTime();
void resetToFactorySettings();
void setup() {
pinMode(OFFLED, OUTPUT);
pinMode(ONLED, OUTPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(OFFLED, LOW);
digitalWrite(ONLED, LOW);
digitalWrite(RELAY, LOW);
lcd.init();
lcd.backlight();
lcd.createChar(0, ukazatel);
// Set the default password
password = "4321";
// Load the password from EEPROM
for (int i = 0; i < 4; i++) {
byte storedChar = EEPROM.read(i);
if (storedChar != 255) { // Check if the EEPROM location has been written to
password.setCharAt(i,(char) storedChar);
}
}
// Show "CODE LOCK" and loading stars
lcd.setCursor(0, 0);
lcd.print("CODE LOCK");
for (int i = 0; i < 16; i++) {
lcd.setCursor(i, 1);
lcd.print("*");
delay(50);
}
// Show "CODE:" and wait for password
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CODE:");
}
void loop() {
//checkPassword();
showMenu();
}
void showLoadingScreen() {
lcd.clear();
for (int i = 0; i < 16; i++) {
lcd.setCursor(i, 0);
lcd.print("*");
lcd.setCursor(15 - i, 1);
lcd.print("*");
delay(50);
}
}
void checkPassword() {
String inputPassword = "";
while (inputPassword.length() < 4) {
char customKey = keypad.getKey();
if (customKey) {
if(customKey>='0' && customKey<='9'){
inputPassword += customKey;
lcd.setCursor(inputPassword.length() + 4, 0);
lcd.print(customKey);
delay(50);
}
}
}
if (inputPassword == password) {
digitalWrite(ONLED, HIGH);
lcd.clear();
lcd.print("UNLOCK TIME");
lcd.setCursor(0, 1);
for (int i = unlockTime; i >= 0; i--) {
lcd.print(i);
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
}
digitalWrite(ONLED, LOW);
// Check if the user wants to enter the menu
bool enterMenu = false;
unsigned long menuStartTime = millis();
while (millis() - menuStartTime < 2000) {
char customKey = keypad.getKey();
if (customKey == '#') {
enterMenu = true;
break;
}
}
if (enterMenu) {
showMenu();
}
} else {
digitalWrite(OFFLED, HIGH);
delay(1000);
digitalWrite(OFFLED, LOW);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CODE:");
}
void showMenu() {
menuIndex = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1.CHANGE PASSWORD");
lcd.setCursor(0, 1);
lcd.print("2.CHANGE REGIME");
lcd.setCursor(15, 0);
lcd.write(byte(0));
while (true) {
char customKey = keypad.getKey();
if (customKey == 'A') {
menuIndex = (menuIndex + 3) % 4;
lcd.setCursor(15, 0);
lcd.print(" ");
lcd.setCursor(15, 1);
lcd.print(" ");
lcd.setCursor(15, menuIndex % 2);
lcd.write(byte(0));
} else if (customKey == 'B') {
menuIndex = (menuIndex + 1) % 4;
lcd.setCursor(15, 0);
lcd.print(" ");
lcd.setCursor(15, 1);
lcd.print(" ");
lcd.setCursor(15, menuIndex % 2);
lcd.write(byte(0));
} else if (customKey == 'C') {
if (menuIndex == 0) {
changePassword();
} else if (menuIndex == 1) {
changeRegimes();
} else if (menuIndex == 2) {
changeUnlockTime();
} else if (menuIndex == 3) {
resetToFactorySettings();
}
break;
}
}
showLoadingScreen();
}
void changePassword() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ENTER NEW PASSWORD");
newPassword = "";
while (newPassword.length() < 4) {
char customKey = keypad.getKey();
if (customKey) {
newPassword += customKey;
lcd.setCursor(newPassword.length() + 15, 0);
lcd.print("*");
delay(50);
}
}
lcd.setCursor(0, 1);
lcd.print("CONFIRM PASSWORD");
confirmPassword = "";
while (confirmPassword.length() < 4) {
char customKey = keypad.getKey();
if (customKey) {
confirmPassword += customKey;
lcd.setCursor(confirmPassword.length() + 15, 1);
lcd.print("*");
delay(50);
}
}
if (newPassword == confirmPassword) {
password = newPassword;
// Save the password to EEPROM
for (int i = 0; i < 4; i++) {
EEPROM.write(i, password[i]);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PASSWORD CHANGED");
delay(1000);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PASSWORD MISMATCH");
delay(1000);
}
}
void changeRegimes() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A: MANUAL");
lcd.setCursor(0, 1);
lcd.print("B: AUTOMATIC");
while (true) {
char customKey = keypad.getKey();
if (customKey == 'A') {
automaticRegime = false;
break;
} else if (customKey == 'B') {
automaticRegime = true;
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("REGIME CHANGED");
delay(1000);
}
void changeUnlockTime() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SET UNLOCK TIME");
int newUnlockTime = 0;
while (newUnlockTime < 3 || newUnlockTime > 10) {
char customKey = keypad.getKey();
if (customKey) {
newUnlockTime = customKey - '0';
lcd.setCursor(0, 1);
lcd.print("TIME: ");
lcd.print(newUnlockTime);
delay(50);
}
}
unlockTime = newUnlockTime;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TIME SET");
delay(1000);
}
void resetToFactorySettings() {
password = "1456";
unlockTime = 5;
automaticRegime = true;
// Save the password to EEPROM
for (int i = 0; i < 4; i++) {
EEPROM.write(i, password[i]);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RESET TO DEFAULT");
delay(1000);
}