You should really look which key is soldered to what pin, otherwise it is REALLY confusing to use
Oh and I added another tactile button to act as an ESC key.
Code: Select all
#include <Bounce.h>
#define NUM_KEYS 13
struct Key {
int keycode;
Bounce* bounce;
};
Key keys[NUM_KEYS];
Key key(int keycode, int pin) {
Key *ret = new Key;
ret->keycode = keycode;
ret->bounce = new Bounce(pin, 10);
pinMode(pin, INPUT_PULLUP);
return *ret;
}
void setupKeys() {
keys[0] = key(KEY_LEFT_ARROW, 0);
keys[1] = key(KEY_UP_ARROW, 1);
keys[2] = key(KEY_DOWN_ARROW, 2);
keys[3] = key(KEY_RIGHT_ARROW, 3);
keys[4] = key(KEY_TAB, 4);
keys[5] = key(KEY_RETURN, 5);
keys[6] = key('o', 6);
keys[7] = key('k', 7);
keys[8] = key('x', 8);
keys[9] = key('z', 9);
keys[10] = key('q',10);
keys[11] = key('e',11);
keys[12] = key(KEY_ESC, 12);
}
void setup() {
setupKeys();
Keyboard.begin();
// pinMode(0, INPUT_PULLUP);
}
void loop() {
for (int i = 0; i < NUM_KEYS; i++) {
keys[i].bounce->update();
if (keys[i].bounce->fallingEdge()) {
Keyboard.press(keys[i].keycode);
} else if (keys[i].bounce->risingEdge()) {
Keyboard.release(keys[i].keycode);
}
}
}