Skip to content

Instantly share code, notes, and snippets.

@pbrdmn
Last active December 16, 2022 08:03
Show Gist options
  • Save pbrdmn/646eedc374ec37e680a70c88dc4bfcc3 to your computer and use it in GitHub Desktop.
Save pbrdmn/646eedc374ec37e680a70c88dc4bfcc3 to your computer and use it in GitHub Desktop.
/*
Ascii banana
Writing bits from key state and typing characters
*/
#include "Keyboard.h"
#define DEBOUNCE_TIME 50
#define NUM_KEYS 8
int pins[] = { 7, 8, 9, 10, 16, 14, 15, 18, };
char bits = 0;
int now = 0;
int lastActive = 0;
bool updateBits() {
int active = 0;
// read value of keys in as bits.
for (int i = 0; i < NUM_KEYS; i++) {
if (digitalRead(pins[i]) == LOW) {
bitSet(bits, NUM_KEYS - 1 - i);
active++;
}
}
return active;
}
void setup() {
for (int i = 0; i < NUM_KEYS; i++) pinMode(pins[i], INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
now = millis();
bool active = updateBits();
if (active) {
lastActive = now;
} else {
if (now > lastActive + DEBOUNCE_TIME) {
Keyboard.write(bits);
bits = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment