Skip to content

Instantly share code, notes, and snippets.

@NobodyIsThere
Created August 27, 2021 16:41
Show Gist options
  • Save NobodyIsThere/6ec8de310754f25949cb2c8392ffa630 to your computer and use it in GitHub Desktop.
Save NobodyIsThere/6ec8de310754f25949cb2c8392ffa630 to your computer and use it in GitHub Desktop.
Code for Teensy-based dance pad
// Based on Promit Roy's code at https://pastebin.com/Y1vNZ56K
// This is the LED pin for a Teensy LC, may need to change on other boards
const int LedPin = 13;
// The analog threshold value for triggering a button
const int TriggerThreshold = 5;
// How long a step has to be in one state for before we register it
const unsigned long debounce_millis = 5;
// Pin mappings for where things got soldered
const int p[8] = {0, 1, 2, 3, 4, 5, 6, 7};
// Binary read values
int a[8] = {0}; // current values
int b[8] = {0}; // last frame's values
int k[8] = {0}; // output values
unsigned long d[8] = {0}; // debounce timestamps
bool pressed = false; // check if any buttons are pressed, so we know whether to light the LED
void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
}
void loop() {
pressed = false;
for(int i = 0; i < 8; ++i)
{
a[i] = analogRead(p[i]) < TriggerThreshold ? 1 : 0;
if (a[i] != b[i])
{
// Input changed!
d[i] = millis();
}
if (millis() - d[i] > debounce_millis)
{
k[i] = a[i];
}
if (k[i])
{
pressed = true;
}
b[i] = a[i];
}
Keyboard.set_key1(k[0] ? KEY_BACKSPACE : 0);
Keyboard.set_key2(k[1] ? KEY_UP : 0);
Keyboard.set_key3(k[2] ? KEY_ENTER : 0);
Keyboard.set_key4(k[3] ? KEY_LEFT : 0);
Keyboard.set_key5(k[4] ? KEY_RIGHT : 0);
Keyboard.set_key6(k[6] ? KEY_DOWN : 0);
Keyboard.set_modifier((k[5] ? MODIFIERKEY_SHIFT : 0) | (k[7] ? MODIFIERKEY_CTRL : 0));
Keyboard.send_now();
// Illuminate the LED if a button is pressed
if(pressed)
digitalWrite(LedPin, HIGH);
else
digitalWrite(LedPin, LOW);
// Enable this block if you need to debug the electricals of the pad
if(0)
{
Serial.printf("Pins: %d %d %d %d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
delay(250);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment