/*
WARNING - read this link expressing concern over this sketch...
https://forum.arduino.cc/t/programming-a-keypad-without-a-library-doesnt-work/1204085/6?u=xfpd
In the above link, this description of Keypad.h pin reading...
"If you look at the code in the Keypad library, the column pins are all set to
INPUT_PULLUP, and the row pins are also set to INPUT mode. During the scan of
the keypad, one row pin at a time is set to OUTPUT mode and used to output a LOW,
then the column pins are each read to determine if a key is pressed, after which
the row pin is set back to HIGH and then to INPUT mode. At no time is there more
than one pin set to OUTPUT mode."
*/
// Keypad Scanning without Library
// ee-diary.blogspot.com
const char rows = 4; // set display to four rows
const char cols = 4; // set display to three columns
const char keys[rows][cols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char rowPins[rows] = {11, 10, 9, 8};
char colPins[cols] = { 7, 6, 5, 4};
void setup () {
Serial.begin(9600);
for (char r = 0; r < rows; r++) {
pinMode(rowPins[r], INPUT); //set the row pins as input
digitalWrite(rowPins[r], HIGH); //turn on the pullups
}
for (char c = 0; c < cols; c++) {
pinMode(colPins[c], OUTPUT); //set the column pins as output
}
}
void loop() {
char key = getKey();
if (key != 0) {
Serial.println(key);
}
}
char getKey() {
char k = 0;
for (char c = 0; c < cols; c++) {
digitalWrite(colPins[c], LOW);
for (char r = 0; r < rows; r++) {
if (digitalRead(rowPins[r]) == LOW) {
delay(20); //20ms debounce time
while (digitalRead(rowPins[r]) == LOW);
k = keys[r][c];
}
}
digitalWrite(colPins[c], HIGH);
}
return k;
}