Skip to content

Instantly share code, notes, and snippets.

@jes
Created July 6, 2020 12:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jes/fe1aeb18457a06562516372684d1bd47 to your computer and use it in GitHub Desktop.
Save jes/fe1aeb18457a06562516372684d1bd47 to your computer and use it in GitHub Desktop.
#include <Keyboard.h>
#define NKEYS 3
unsigned long mintime = 20; // ms (debounce time)
int keypin[NKEYS] = {2,3,4};
char letter[NKEYS] = {'j', 'e', 's'};
int isdown[NKEYS];
unsigned long lastchange[NKEYS];
void setup() {
Keyboard.begin();
for (int i = 0; i < NKEYS; i++)
pinMode(keypin[i], INPUT_PULLUP);
}
void loop() {
for (int i = 0; i < NKEYS; i++) {
int down = !digitalRead(keypin[i]);
if (down != isdown[i]) {
isdown[i] = down;
lastchange[i] = millis();
}
if (millis() - lastchange[i] > mintime) {
if (isdown[i]) {
Keyboard.press(letter[i]);
} else {
Keyboard.release(letter[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment