#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int numRows = 4; // number of rows in the keypad
const int numCols = 4; // number of columns
const int debounceTime = 20; // number of milliseconds for switch to be stable
const char keymap[numRows][numCols] = {
{ '1', '2', '3' ,'A'} ,
{ '4', '5', '6' ,'B'} ,
{ '7', '8', '9' ,'C'} ,
{ '*', '0', '#' ,'D'}
};
const int rowPins[numRows] = { 12, 11, 10, 9 }; // Rows 0 through 3
const int colPins[numCols] = { 7, 6, 5,4 }; // Columns 0 through 3
const char* passwordOn = "123"; // Password to turn on the LED
const char* passwordOff = "456"; // Password to turn off the LED
bool ledOn = false; // Keep track of LED state
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
for (int row = 0; row <numRows; row++) {
pinMode(rowPins[row],INPUT); // Set row pins as input
digitalWrite(rowPins[row],HIGH); // turn on Pull-ups
}
for (int column = 0; column <numCols; column++) {
pinMode(colPins[column],OUTPUT); // Set column pins as outputs
// for writing
digitalWrite(colPins[column],HIGH); // Make all columns inactive
}Serial.print("Enter password : ");
}
void loop() {
char key = getKey();
if (key != 0) { // if the character is not 0 then it's a valid key press
Serial.print(key);
// Display the selected key on the LCD screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password : ");
lcd.setCursor(0, 1);
lcd.println(key);
// Check if the key is a valid password character
if (key == passwordOn[strlen(passwordOn) - 1]) {
// Check if the full password to turn on the LED has been entered
if (strcmp(passwordOn, "123") == 0) {
// Password is correct, turn on the LED
digitalWrite(2, HIGH);
ledOn = true;
}
} else if (key == passwordOff[strlen(passwordOff) - 1]) {
// Check if the full password to turn off the LED has been entered
if (strcmp(passwordOff, "456") == 0) {
// Password is correct, turn off the LED
digitalWrite(2, LOW);
ledOn = false;
}
} else {
// Invalid password character, reset passwords
strcpy(passwordOn, " ");
strcpy(passwordOff, " ");
}
}
}
// Returns the key pressed, or 0 if no key is pressed
char getKey() {
char key = 0; // 0 indicates no key pressed
for (int column = 0; column <numCols; column++) {
digitalWrite(colPins[column],LOW); // Activate the current column.
for (int row = 0; row <numRows; row++) { // Scan all rows for a key press.
if (digitalRead(rowPins[row]) == LOW) { // Is a key pressed?
delay(debounceTime); // debounce
while (digitalRead(rowPins[row]) == LOW); // wait for key to be released
key = keymap[row][column]; // Remember which key was pressed.
}
}
digitalWrite(colPins[column],HIGH); // De-activate the current column.
}
// If the '#' key is pressed, check for the password to turn off the LED
if (key == '#') {
Serial.println(" Enter password to turn off the LED:");
char passwordInput[1] = {0}; // Array to store user input
int i = 0;
while (i < 4 ) { // Read up to 4 characters
char x = getKey();
if (x != 0) {
passwordInput[i] = x;
i++;
}
}
// Check if the entered password matches the password to turn off the LED
if (strcmp(passwordInput, passwordOff) == 0) {
key = passwordOff[strlen(passwordOff) - 1]; // Set key to '8' to turn off the LED
} else {
Serial.println("Incorrect password");
key = 0; // Clear key
}
}
return key; // returns the key pressed or 0 if none
}