I have my keyboard matrix wired up with diodes and they are all in the right direction.
It's 4 rows and 16 columns. I don't get any ghosting between rows, however when I press and hold a key I get alternating prints of press/released continuously for most keys. But a few of the keys work correctly for some reason.
I don't thing it's mechanical bounce since it's only happening when holding down keys.
Adding the 10 microsecond delay seem to make it worse.
Printing just the currentStates continuously looks to be registrering correctly and not flicker. There might be some minimal flicker at times but it's hard to tell when it's printing so fast.
I'm I doing something wrong in the code or is there some electrical issue?
bool KeyboardMatrix::scanKeyboard()
{
static bool currentStates[64];
static bool prevStates[64];
int i = 0;
for (int r = 0; r < NUM_ROWS; r++)
{
// Put all rows in INPUT_PULLUP (isolated)
for (int rr = 0; rr < NUM_ROWS; rr++) {
pinMode(rowPins[rr], INPUT_PULLUP);
}
// Drive row r LOW
pinMode(rowPins[r], OUTPUT);
digitalWrite(rowPins[r], LOW);
// delayMicroseconds(20); // This makes it worse
// Read the columns for row r and store into currentStates[]
for (int c = 0; c < NUM_COLUMNS; c++)
{
bool isPressed = (digitalRead(columnPins[c]) == LOW);
uint8_t keyId = r * NUM_COLUMNS + c;
currentStates[keyId] = isPressed;
i++;
}
digitalWrite(rowPins[r], INPUT_PULLUP);
}
for (int i = 0; i < 64; i++)
{
if (currentStates[i] != prevStates[i])
{
if (currentStates[i])
{
Serial.print("Key pressed at row ");
Serial.print(i % 16);
Serial.print(", keyID ");
Serial.println(i);
}
else
{
Serial.print("Key released at row ");
Serial.print(i % 16);
Serial.print(", keyID ");
Serial.println(i);
}
Serial.println();
}
// Serial.print(keyStates[i]);
}
for (int i = 0; i < 64; i++)
{
prevStates[i] = currentStates[i];
}
delay(1);
return true;
}



