Problem with 4x3 button matrix

Good afternoon, I am making a 4x3 keypad for an access control system. The problem I have is that when I press a key for the first time on the keypad, the key pressed is displayed correctly in serial, but if I press another key it is not displayed. Am I missing something in the circuit? Is there something missing in my programming? I appreciate your help, thank you.

const char keys[4][3] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
const byte rowPins[4] = {13, 12, 11, 10};
const byte colPins[3] = {9, 8, 7};
byte i, j;

void setup(){
    for (i=0; i<4; i++){
        pinMode(rowPins[i], OUTPUT);
    }

    for (i=0; i<3; i++){
        pinMode(colPins[i], INPUT_PULLUP);
    }
    Serial.begin(9600);
}
 
void loop(){
    for (i=0; i<4; i++){
        digitalWrite(rowPins[i], LOW);
        for (j=0; j<3; j++) {
            if (!digitalRead(colPins[j])){
                Serial.print("Key: ");
                Serial.println(keys[i][j]);
                while(digitalRead(!colPins[j]));
            }
        }
        digitalWrite(rowPins[i], HIGH);
    }
    delay(10);
}

Read thru this discussion:

https://www.gammon.com.au/forum/?id=14175

Are you releasing the first key before pressing the second one ?

I figured out the glitch, the problem was that I was negating the column and had to negate the read in:
while(digitalRead(!colPins[j]));
I changed it to: while(!digitalRead(colPins[j]))
Now it works fine.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.