const int numRows = 4;
const int numCols = 4;
char keymap[numRows][numCols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
/*bool arrayRows[numRows][numCols] =
{
{0, 1, 1, 1},
{1, 0, 1, 1},
{1, 1, 0, 1},
{1, 1, 1, 0},
};*/
// connect to the row pinouts of the keypad
int rowPins[numRows] = {9, 8, 7, 6};
// connect to the column pinouts of the keypad
int colPins[numCols] = {5, 4, 3, 2};
void setup()
{
Serial.begin(9600);
}
void loop()
{
char key = getKey();
if (key != 0)
{
Serial.println(key);
delay(100);
}
}
char getKey()
{
for (int row = 0; row < numRows; ++row)
{
pinMode(rowPins[row], OUTPUT);
digitalWrite(rowPins[row], LOW);
for (int col = 0; col < numCols; ++col)
{
pinMode(colPins[col], INPUT_PULLUP);
if (digitalRead(colPins[col]) == LOW)
{
delay(50); // Debounce
while (digitalRead(colPins[col]) == LOW)
{} // Wait for key release
return keymap[row][col];
}
pinMode(colPins[col], INPUT); // Set column pin back to input
}
pinMode(rowPins[row], INPUT); // Set row pin back to input
}
return 0; // Return 0 if no key is pressed
}