#include <Wire.h> // include I2C library
#include <LiquidCrystal_I2C.h> // include LCD library
#include <Keypad.h> // include keypad library
// Define keypad pins and keypad matrix
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {34, 36, 38, 40};
// Initialize keypad and LCD
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
char key = ' ';
char key2 = ' ';
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome to");
lcd.setCursor(0, 1);
lcd.print("my project!");
delay(2000); // wait 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting");
}
void loop() {
char tempKey = keypad.getKey();
if (tempKey != NO_KEY) {
key2 = key;
key = tempKey;
Serial.println(key);
// Display the last key pressed on the top row of the LCD
lcd.setCursor(0, 0);
lcd.print("Last key: ");
lcd.print(key2);
// Display the new key pressed on the bottom row of the LCD
lcd.setCursor(0, 1);
lcd.print("New key: ");
lcd.print(key);
while (keypad.getState() == PRESSED && keypad.getKey() == key) {
delay(1000); // debounce delay
}
}
}