/*
Arduino | general-help
JugandoMiguel — 4/20/24 at 1:21 AM
heyaaa, how can I print a key pressed in a 4x4 keypad to a lcd with i2c?
*/
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {{'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'}};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
char buffer[16]; // or whatever row width you have
char keyBuff[16];
int keyIdx = 0;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.print("Pass:");
}
void updateDisplay(char* password) {
lcd.setCursor(0, 0);
snprintf(buffer, 16, "Pass: %-8s", password);
lcd.print(buffer);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key != '#') {
keyBuff[keyIdx] = key;
keyIdx++;
if (keyIdx > 16) keyIdx = 0;
} else {
updateDisplay(keyBuff);
for (int i = 0; i < 16; i++)
keyBuff[i] = '\0';
keyIdx = 0;
}
//updateDisplay(key);
}
}