#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define password_length 8
char Data[password_length];
char Master[password_length] = "123A456";
byte data_count = 0;
// pin configuration
const int relay_pin = 13;
LiquidCrystal_I2C lcd(0x27, 16, 4); //16x4 LCD with I2C address 0x27
/* Keypad setup */
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void showStartupMessage() {
lcd.setCursor(4, 0);
lcd.print("Welcome!");
delay(1000);
lcd.setCursor(0, 1);
String message = " ECE Department";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
lcd.clear();
}
void doorlocked(){
digitalWrite(relay_pin, HIGH);
lcd.setCursor(0,1);
lcd.write("door locked");
delay(2000);
lcd.clear();
}
void doorunlocked(){
digitalWrite(relay_pin, LOW);
lcd.setCursor(0,1);
lcd.write("door Unlocked");
delay(2000);
lcd.clear();
}
void setup(){
Serial.begin(115200);
lcd.begin(16, 2); //starting lcd
lcd.backlight(); // turn on the back light 🚦 of lcd display
pinMode(relay_pin, OUTPUT);
showStartupMessage();
}
void loop(){
// char customKey = customKeypad.getKey();
// if (customKey){
// lcd.println(customKey);
// lcd.clear();
// }
lcd.setCursor(0,0);
lcd.print("enter password");
char customKey = customKeypad.getKey();
if(customKey){
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
if (data_count == password_length - 1) {
lcd.clear();
if (!strcmp(Data, Master)) {
// Password is correct
lcd.print("Correct");
// Turn on relay for 5 seconds
digitalWrite(relay_pin, HIGH);
delay(5000);
digitalWrite(relay_pin, LOW);
}
else {
// Password is incorrect
lcd.print("Incorrect");
delay(1000);
}
// Clear data and LCD display
lcd.clear();
clear_data();
}
}
void clear_data(){
while(data_count !=0){
Data[data_count--] = 0;
}
return;
}