Greetings, I am relatively new to this forum. Please pardon me for any mistakes I make while posting new topic in this forum. With this being said, I have been trying to write a 'standard' 4x4 keypad scanning code. However, the code did not work correctly, I have tried to debug my code using the Serial Monitor with no luck. According to the Serial Monitor, only few keys would actually be correctly registered (usually 5, 8, 4, and 1), and the rest give me no output at all. However, when I insert a delay of 1 second within the loop itself, all the keys are correctly registered, but it give me a 'cyclic' pattern (ie. pressing 7, will give me 1, 4, and then 7). I have spent a lot of time on this, and a Google search of 4x4 keypad always refer me to the Arduino library. Overview of the code:set column pins to pull_up resistor, and set the row pins as output. Begin to scan the column pins with a given row pin output, and return the index.*Note: I know there is an Arduino library for this, but I want to practice in coding/programming the 4x4 keypad, and I don't know where did I went wrong.const byte ROWS = 4;const byte COLS = 4;
int rowval;
int colval;
char hexaKeys[ROWS][COLS] = {{'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'}};
int rowPins[ROWS] = {7, 6, 5, 4};
int colPins[COLS] = {3, 2, 1, 0};
boolean col_state;
boolean prev_state;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 4; i++) {
pinMode(colPins[i], INPUT_PULLUP);
pinMode(rowPins[i],OUTPUT);
digitalWrite(rowPins[i],HIGH);
}
}void loop() {
for (int i = 0; i < 4; i++) {
rowval=0;
colval=0;
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], LOW);
for (int j = 0; j < 4; j++) {
col_state = digitalRead(colPins[j]);
if (col_state == 0) {
prev_state=col_state;
delay(50);
if(digitalRead(colPins[j])==prev_state){
rowval = i;
colval = j;
Serial.println(hexaKeys[rowval][colval]);
delay(1000); ///add a 1 second delay here to make the output of Serial monitor more manageable
digitalWrite(rowPins[i],HIGH);
pinMode(rowPins[0], INPUT);
break;
}
}
}
if(rowval!=0 && colval!=0)
break;
}
}