I think I have a capacitance problem (or something similar). I got my i2c bus working between that two keyboard halves without pull up resistors. However, whenever I press a key in the 5th or 6th column to be read i get the timeout most of the time. Adding 2K pull up resistors has no effect. The interesting part is that it is independent of the column. If I reorder the column pins in the array same result. The last two columns in the array of IO pins are the only ones to have this problem.
Here is the code I'm using to read the key pins
Code:
//################### keyboard #########################
byte rows[] = { 23, 22, 21, 20, 15, 14, 9 };
const int rowCount = sizeof(rows) / sizeof(rows[0]);
byte cols[] = { 5, 4, 3, 2, 1, 0 };
const int colCount = sizeof(cols) / sizeof(cols[0]);
byte keyStates[colCount][rowCount];
void setupKeys() {
initKeyStates();
Keyboard.begin();
for (int x = 0; x < rowCount; x++) {
pinMode(rows[x], INPUT_DISABLE);
}
for (int x = 0; x < colCount; x++) {
pinMode(cols[x], INPUT_DISABLE);
//digitalWrite(cols[x], LOW);
}
}
bool readKeys(int keyBufSize, byte* keysBuffer) {
int keyIdx = 0;
bool keyRead = false;
for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
byte row = rows[rowIdx];
pinMode(row, INPUT_PULLDOWN);
for (int colIdx = 0; colIdx < colCount; colIdx++) {
byte col = cols[colIdx];
pinMode(col, OUTPUT);
digitalWrite(col, HIGH);
delayMicroseconds(5);
byte btnState = digitalRead(row);
digitalWrite(col, LOW);
pinMode(col, INPUT_DISABLE);
if (keyStates[colIdx][rowIdx] != btnState) { // check if button state changed
keyRead = true;
if (keyIdx >= keyBufSize) { // Only so many button presses can be tracked and sent at one time via i2c
println("Key buffer full");
return true; // buffer full
}
// col row nonce state
// 111 111 1 1
byte state = colIdx;
state = state << 3;
state += rowIdx;
state = state << 2;
state += 2; // flip the empty bit as a nonce so we can distinguish between no value and 0,0 released
state += btnState;
keysBuffer[keyIdx++] = state;
}
keyStates[colIdx][rowIdx] = btnState;
}
pinMode(row, INPUT_DISABLE);
}
return keyRead;
}
void initKeyStates() {
for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
for (int colIdx = 0; colIdx < colCount; colIdx++) {
keyStates[colIdx][rowIdx] = 0;
}
}
}