#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {9, 8, 7, 6};
byte colPins[KEYPAD_COLS] = {5, 4, 3, 2};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
const int relay = 1;
const String correctCode = "1234";
String enteredCode;
int codeIndex = 0;
bool backlightOn = true;
void setup() {
pinMode(relay, OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Enter code :");
lcd.backlight();
}
void loop() {
key();
A();
}
void relayControl() {
if (enteredCode== correctCode) {
lcd.setCursor(6, 1);
lcd.print("WIN");
digitalWrite(relay, HIGH);
resetCode();
} else {
lcd.setCursor(6, 1);
lcd.print("LOSE");
resetCode();
}
}
void resetCode() {
delay(2000);
lcd.clear();
digitalWrite(relay,LOW);
enteredCode = "";
}
void key() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
relayControl();
} else {
lcd.print(key);
enteredCode += key;
}
}
}
void A() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == 'A') {
if (backlightOn) {
lcd.noBacklight();
} else {
lcd.backlight();
}
backlightOn = !backlightOn;
}
}
}