#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte RowPins[KEYPAD_ROWS] = {5,4,3,2};
byte ColPins[KEYPAD_COLS] = {A3, A2, A1, A0};
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);
char enteredKeys[17]; // Buffer to store entered keys (plus one for null-terminator)
byte index = 0;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
}
void loop() {
char key = keypad.getKey();
if (key) {
if (index < 16){
enteredKeys[index] = key; // Store the key in the buffer
index++;
lcd.setCursor(index - 1, 0); // Set the cursor to the next position
lcd.print(key); // Print the pressed key
}
if (index == 16) { // If 4 keys have been entered
enteredKeys[index] = '\0'; // Null-terminate the buffer
lcd.setCursor(0, 1); // Set the cursor to the beginning of the second line
lcd.print(enteredKeys); // Display the entered sequence
delay(2000); // Delay to show the sequence
lcd.clear(); // Clear the LCD screen
index = 0; // Reset the buffer index for the next sequence
}
if (key == 'D'){
lcd.setCursor(0,1);
lcd.print(enteredKeys);
delay(2000);
lcd.clear();
index = 0;
}
delay(100); // Small delay to avoid bouncing issues
}
}