#include<LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR,LCD_COLUMNS,LCD_LINES);
const int numRow=4;
const int numCols=4;
const int debounsTime = 20;
const char keymap[numRow][numCols]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
const int RowPins[numRow]={9,8,7,6};
const int ColPins[numCols]={5,4,3,2};
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Using LCD");
for(int Row=0;Row<numRow;Row++){
pinMode(RowPins[Row], INPUT_PULLUP);
}
for (int Column=0;Column<numCols;Column++){
pinMode (ColPins[Column],OUTPUT);
digitalWrite(ColPins[Column], HIGH);
}
// put your setup code here, to run once:
}
void loop() {
char key=getKey();
if (key!=0){
lcd.setCursor(3,1);
lcd.print("Got key ");
lcd.print(key);
}
// put your main code here, to run repeatedly:
}
char getKey(){
char Key=0;
for(int column=0;column<numCols;column++){
digitalWrite(ColPins[column], LOW);
for(int row=0;row<numRow;row++){
if (digitalRead(RowPins[row])==LOW){
delay(debounsTime);
while(digitalRead(RowPins[row])==LOW);
Key=keymap[row][column];
}
}
digitalWrite(ColPins[column], HIGH);
}
return Key;
}