/*#include <Keypad.h>
#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
lcd.begin();
lcd.backlight();
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
lcd.println(key);
}
}*/
// *************** LCD screen start
#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);
// **************** LCD end
const int numRows = 4; // number of rows in the keypad
const int numCols = 4; // number of columns
const int debounceTime = 20; // number of milliseconds for switch to be stable
// keymap defines the character returned when the corresponding key is pressed
const char keymap[numRows][numCols] = {
{ '1', '2', '3' ,'A'} ,
{ '4', '5', '6','B' } ,
{ '7', '8', '9','C' } ,
{ '*', '0', '#','D' }
};
// this array determines the pins used for rows and columns
const int rowPins[numRows] = {9,8, 7, 6}; // Rows 0 through 3
const int colPins[numCols] = {5,4, 3, 2}; // Columns 0 through 2
void setup()
{
// Init LCD screen
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("Using LCD");
for (int row = 0; row < numRows; row++)
{
pinMode(rowPins[row],INPUT_PULLUP); // Set row pins as input with pullups
}
for (int column = 0; column < numCols; column++)
{
pinMode(colPins[column],OUTPUT); // Set column pins as outputs
digitalWrite(colPins[column],HIGH); // Make all columns inactive
}
}
void loop()
{
char key = getKey();
if( key != 0) { // if the character is not 0 then it's a valid key press
lcd.setCursor(3, 1);
lcd.print("Got key ");
lcd.print(key);
}
}
// returns the key pressed, or zero if no key is pressed
char getKey()
{
char key = 0; // 0 indicates no key pressed
for(int column = 0; column < numCols; column++)
{
digitalWrite(colPins[column],LOW); // Activate the current column.
for(int row = 0; row < numRows; row++) // Scan all rows for key press
{
if(digitalRead(rowPins[row]) == LOW) // Is a key pressed?
{
delay(debounceTime); // debounce
while(digitalRead(rowPins[row]) == LOW)
; // wait for key to be released
key = keymap[row][column]; // Remember which key
// was pressed.
}
}
digitalWrite(colPins[column],HIGH); // De-activate the current column.
}
return key; // returns the key pressed or 0 if none
}