#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 numRows = 4;
const int numCols = 4;
const int debounceTime = 20;
const char keymap[numRows][numCols] = {
{ '1', '2', '3' ,'A'} ,
{ '4', '5', '6','B' } ,
{ '7', '8', '9','C' } ,
{ '*', '0', '#','D' }
};
const int rowPins[numRows] = {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 < numRows; row++)
{
pinMode(rowPins[row],INPUT_PULLUP);
}
for (int column = 0; column < numCols; column++)
{
pinMode(colPins[column],OUTPUT);
digitalWrite(colPins[column],HIGH);
}
}
void loop()
{
char key = getKey();
if( key != 0) {
lcd.setCursor(3, 1);
lcd.print("Got key ");
lcd.print(key);
}
}
char getKey()
{
char key = 0;
for(int column = 0; column < numCols; column++)
{
digitalWrite(colPins[column],LOW);
for(int row = 0; row < numRows; row++)
{
if(digitalRead(rowPins[row]) == LOW)
{
delay(debounceTime); // debounce
while(digitalRead(rowPins[row]) == LOW)
;
key = keymap[row][column];
}
}
digitalWrite(colPins[column],HIGH);
}
return key;
}