Skip to content

Instantly share code, notes, and snippets.

@narze
Created January 22, 2020 10:51
Show Gist options
  • Select an option

  • Save narze/5e2df303a95d18c2cb8e4ee1fa597594 to your computer and use it in GitHub Desktop.

Select an option

Save narze/5e2df303a95d18c2cb8e4ee1fa597594 to your computer and use it in GitHub Desktop.
Pedal : Arduino foot app switcher
#include <Keyboard.h>
bool pressed = false;
bool currentState;
bool lastState = false;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 10; // the debounce time; increase if the output flickers
unsigned long holdTime = 500;
unsigned long holdTimeout = 5000; // If hold more than 5s, don't trigger keys on release
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
void setup() {
// make pin 2 an input and turn on the pull-up resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
int input = digitalRead(2);
if (input != lastState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (input != currentState) {
currentState = input;
// On release
if (currentState == HIGH) {
bool isHold = (millis() - pressedTime) > holdTime && (millis() - pressedTime) < holdTimeout;
if (isHold) {
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_TAB);
delay(10);
Keyboard.releaseAll();
releasedTime = millis();
}
} else {
// On press
bool isHold = (millis() - releasedTime) > holdTime;
if (isHold) {
pressedTime = millis();
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press(KEY_TAB);
delay(10);
Keyboard.releaseAll();
}
}
}
}
lastState = input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment