I have this fairly large keyboard project running on a Teensy 3.6, and I'm trying to replace my delay(5) function with Bounce2 implementation. The section of code I am changing is working just fine(I'm typing on it right now) in this form:
void setup() {
Serial.begin(115200);
for (uint8_t j=0; j<columnsCount; j++) {
pinMode(columns[j], INPUT_PULLUP);
}
for (uint8_t i=0; i<rowsCount; i++) {
pinMode(rows[i], OUTPUT);
digitalWrite(rows[i], HIGH);
}
Keyboard.begin();
Mouse.begin();
}
void loop() {
for (uint8_t i=0; i<rowsCount; i++) {
digitalWrite(rows[i], LOW);
for (uint8_t j=0; j<columnsCount; j++) {
Key* key = getKey(i, j);
boolean current = !digitalRead(columns[j]);
boolean previous = key->pressed;
key->pressed = current;
LayoutKey* layout = getLayoutKey(key->row, key->column);
if (current && !previous) {
keyPressed(key, layout);
} else if (!current && previous) {
keyReleased(key, layout);
}
}
digitalWrite(rows[i], HIGH);
delay(5);
}
}
But when I change it to this, and many many other attempts, it doesn't work.
#include <Bounce2.h>
Bounce buttons[columnsCount];
void setup() {
Serial.begin(115200);
for (uint8_t j=0; j<columnsCount; j++) {
buttons[j].attach( columns[j], INPUT_PULLUP );
buttons[j].interval(10);
}
for (uint8_t i=0; i<rowsCount; i++) {
pinMode(rows[i], OUTPUT);
digitalWrite(rows[i], HIGH);
}
Keyboard.begin();
Mouse.begin();
}
void loop() {
for (uint8_t i = 0; i < rowsCount; i++) {
digitalWrite(rows[i], LOW);
for (uint8_t j = 0; j < columnsCount; j++) {
Key* key = getKey(i, j);
buttons[j].update();
LayoutKey* layout = getLayoutKey(key->row, key->column);
if (buttons[j].fell()) {
keyPressed(key, layout);
}
if (buttons[j].rose()) {
keyReleased(key, layout);
}
}
digitalWrite(rows[i], HIGH);
}
}
I've looked as hard as I can for Bounce2 documentation with arrays, copied some code from 1D arrays, never found any examples for 2D arrays. I presume the problem is that I have no idea what I'm doing. I've even asked ChatGPT for help, but all it does is hallucinate. I think that's because Bounce2 is not in it's training data, but the code it tried to tell me will work with the previous version of Bounce doesn't work either. Maybe the paid version is better, but I'm not impressed.
Anyway I digress, the problem that happens with the updated snippet here is that it doesn't remotely work. The keys don't register, not even physically. I checked as much as I could think to check with the serial monitor, and no related variables are changing when keys are pressed. Other sections of the code, such as my capslock LED works when I toggle capslock from windows on-screen-keyboard. I have an i2c capacitive touch strip that has some keybinds tied to gestures and they all work just fine, so the rest of the code isn't getting blocked or anything. It's just this matrix scan routine that breaks.