#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
byte LIGHT = A0; // the Arduino pin, which connects to the IN pin of relay
const int ROW_NUM = 4; // four rows
const int COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String password= "ABC9"; // TURN ON THE LED (A0)
const String turnOFF= "ABC8"; // TURN OFF THE LED (A0)
String input_password;
void setup() {
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
input_password.reserve(32); // maximum input characters is 33, change if needed
pinMode(LIGHT, OUTPUT); // initialize pin as an output.
lcd.setCursor(0,0);
lcd.print("KEYWORD: ");
}
void loop() {
char key = keypad.getKey();
if (key){
Serial.println(key);
if(key == '*') {
input_password = ""; // reset the input password
}
else if(key == '#') {
if(input_password == password ) {
lcd.clear();
delay(500);
lcd.print("LIGHT ON");
digitalWrite(LIGHT, 1);
delay(3000);
lcd.setCursor(0,0);
lcd.clear();
lcd.print("KEYWORD:");
lcd.setCursor(0,1);
lcd.print("LIGHT ON");
}else if(input_password == turnOFF){
lcd.clear();
delay(500);
lcd.print("LIGHT OFF");
digitalWrite(LIGHT, 0);
delay(3000);
lcd.setCursor(0,0);
lcd.clear();
lcd.print("KEYWORD:");
lcd.setCursor(0,1);
lcd.print("LIGHT OFF");
}
else {
lcd.setCursor(9,0);
lcd.print("WRONG");
delay(2000);
lcd.setCursor(0,0);
lcd.clear();
lcd.print("KEYWORD: ");
}
input_password = ""; // reset the input password
} else {
input_password += key; // append new character to input password string
}
}
}