#include <Keypad.h>
const int numRows = 4;
const int numCols = 4;
const int debounceTime = 20;
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() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int row = 0; row < numRows; row++)
{
pinMode(rowPins[row], INPUT_PULLUP);
}
for(int column = 0; column < numRows; column++)
{
pinMode(colPins[column], OUTPUT);
digitalWrite(colPins[column], HIGH);
}
}
void loop() {
// put your main code here, to run repeatedly:
char key = getKey();
if(key != 0)
{
Serial.print("Got key ");
Serial.println(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);
while(digitalRead(rowPins[row]) == LOW);
key = keymap[row][column];
}
}
digitalWrite(colPins[column],HIGH);
}
return key;
}