Skip to content

Instantly share code, notes, and snippets.

@samsheffield
Last active September 27, 2017 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsheffield/1a58c5e26aed1e81940aa67fd13a2c7f to your computer and use it in GitHub Desktop.
Save samsheffield/1a58c5e26aed1e81940aa67fd13a2c7f to your computer and use it in GitHub Desktop.
Basic keyboard example for Adafruit Pro Trinket with simultaneous input
// This requires Adafruit's Pro Trinket Keyboard library
#include <ProTrinketKeyboard.h>
// Array to hold keypresses.
uint8_t pressedKeys[9];
void setup() {
// Each pin uses internal pullup resistors
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
TrinketKeyboard.begin();
}
void loop() {
TrinketKeyboard.poll();
// All keypresses are recorded as KEYCODE_* or 0 (which releases key)
if (digitalRead(3) == LOW)
pressedKeys[0] = KEYCODE_A;
else
pressedKeys[0] = 0;
if (digitalRead(4) == LOW)
pressedKeys[1] = KEYCODE_S;
else
pressedKeys[1] = 0;
if (digitalRead(5) == LOW)
pressedKeys[2] = KEYCODE_D;
else
pressedKeys[2] = 0;
if (digitalRead(6) == LOW)
pressedKeys[3] = KEYCODE_F;
else
pressedKeys[3] = 0;
if (digitalRead(8) == LOW)
pressedKeys[4] = KEYCODE_G;
else
pressedKeys[4] = 0;
if (digitalRead(9) == LOW)
pressedKeys[5] = KEYCODE_H;
else
pressedKeys[5] = 0;
if (digitalRead(10) == LOW)
pressedKeys[6] = KEYCODE_J;
else
pressedKeys[6] = 0;
if (digitalRead(11) == LOW)
pressedKeys[7] = KEYCODE_K;
else
pressedKeys[7] = 0;
if (digitalRead(12) == LOW)
pressedKeys[8] = KEYCODE_L;
else
pressedKeys[8] = 0;
// Update key press array and emulate
TrinketKeyboard.pressKey(0, pressedKeys[0], pressedKeys[1], pressedKeys[2], pressedKeys[3], pressedKeys[4], pressedKeys[5]);
// Maximum of 6 simultaneous keypresses, so this is the leftover UPDATE: THIS CAUSES ISSUES WITH SUSTAINED BUTTON PRESSES, SO I'LL NEED TO COME UP WITH A WORKAROUND
//TrinketKeyboard.pressKey(0, pressedKeys[6], pressedKeys[7], pressedKeys[8]);
// Brief delay to more closely emulate speed of keyboard
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment