// import the header files
#include <LiquidCrystal.h>
#include <Keypad.h>
// declare LCD object with pins marked
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
// define number of rows and columns
const byte ROWS = 4;
const byte COLUMNS = 4;
// initialize the cursor column to print the key pressed on the lcd
int cursorColumn = 0;
// define the buttons on the keypad
char hexaKeys[ROWS][ROWS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// define row pins of the keypad
byte rowPins[ROWS] = {7,6,5,4};
// define column pins of the keypad
byte colPins[COLUMNS] = {3,2,1,0};
// declare object of keypad
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLUMNS);
void setup() {
// setup the LCD type
lcd.begin(16, 2);
// clear the LCD screen
lcd.clear();
// set the cursor at the 0th row and 0th column
lcd.setCursor(0,0);
}
void loop() {
// get the key pressed on the keypad by the user
char key = customKeypad.getKey();
// check if key is pressed
if(key)
{
// set cursor in the lcd
lcd.setCursor(cursorColumn, 0);
// display the key pressed on the lcd
lcd.print(key);
// increment the cursor column
cursorColumn = cursorColumn + 1;
// check if the cursor has reached the end of the line
if(cursorColumn == 16)
{
// clear the LCD
lcd.clear();
// reset cursorColumn
cursorColumn = 0;
}
}
}