Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
sacha chua :: living an awesome life
4829 comments
2140 subscribers
4597 on Twitter
HomeAboutSpeakingYearlyArchivesTopical indexContact
« Practising drawing - Weekly review: Week ending August 27, 2011 »
Code and circuit for a six-function Arduino-based USB footswitch
Posted on August 27th, 2011 by Sacha Chua
[Link] (1 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
I’d been thinking about footswitches for a while, but I held off on buying one because they were
expensive. Turns out that building your own is easy, even for someone with limited electronics
experience. I do have the unfair advantage of having a spouse who’s an electrical engineer, but I
figured out this circuit and code by myself!
The hardware for this circuit is really simple. If you’re lucky, you might find a three-way foot switch
at your local audio-equipment-carrying surplus shop. If not, you could make your own, but I haven’t
tried doing that yet. =)
The fun part is in the code that makes this a six-function USB keyboard. The code below maps left,
center, and right short presses to F13, F14, and F15, while left, center, and right long presses send
F16, F17, and F18. Here’s the code:
const int redPin = 9;
const int tanPin = 10;
const int bluePin = 11;
const int orangePin = 12;
const int debounceDelay = 150;
const int longPressThreshold = 650;
int currentState;
int lastSwitch;
long lastDebounce;
long lastPressed;
int lastSwitchDebounced;
uint8_t buf[8] = { 0 }; /* Keyboard report buffer */
#define SWITCH_NONE 0
#define SWITCH_LEFT 1
#define SWITCH_CENTER 2
[Link] (2 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
#define SWITCH_RIGHT 3
#define STATE_WAITING 0
#define STATE_SHORT_PRESSED 1
#define STATE_LONG_PRESSED 2
#define KEY_F13 0x68
#define KEY_F14 0x69
#define KEY_F15 0x6A
#define KEY_F16 0x6B
#define KEY_F17 0x6C
#define KEY_F18 0x6D
#define KEY_F1D 0x6E
#define KEY_PAGEUP 0x4b
#define KEY_PAGEDOWN 0x4e
void setup() {
pinMode(redPin, INPUT); digitalWrite(redPin, HIGH);
pinMode(tanPin, INPUT); digitalWrite(tanPin, HIGH);
pinMode(orangePin, INPUT); digitalWrite(orangePin, HIGH);
pinMode(bluePin, INPUT); digitalWrite(bluePin, HIGH);
[Link](9600);
delay(200);
lastSwitch = 0;
lastDebounce = millis();
currentState = 0;
}
int getCurrentSwitch() {
if (!digitalRead(orangePin)) { return SWITCH_LEFT; }
if (!digitalRead(tanPin)) { return SWITCH_CENTER; }
if (!digitalRead(redPin)) { return SWITCH_RIGHT; }
[Link] (3 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
return SWITCH_NONE;
}
void sendKey(int currentSwitch, boolean isShort, boolean keyDown) {
buf[0] = 0;
buf[1] = 0;
int debug = 0;
if (keyDown) {
switch (currentSwitch) {
case SWITCH_LEFT: buf[2] = isShort ? KEY_F13 : KEY_F16; break;
case SWITCH_CENTER: buf[2] = isShort ? KEY_F14 : KEY_F17; break;
case SWITCH_RIGHT: buf[2] = isShort ? KEY_F15 : KEY_F18; break;
}
if (debug) {
[Link](currentSwitch);
[Link](((int) buf[2]) - KEY_F13);
[Link]("Down");
[Link](isShort ? "Short" : "Long");
}
} else {
buf[2] = 0;
if (debug) { [Link]("Up"); [Link](isShort ? "Short" :
"Long"); }
}
if (!debug) { [Link](buf, 8); }
}
void loop() {
int currentSwitch = getCurrentSwitch();
if (currentSwitch != lastSwitch) {
lastDebounce = millis();
}
[Link] (4 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
// [Link](currentSwitch);
// Debounce it
if (millis() - lastDebounce > debounceDelay) {
switch (currentState) {
case STATE_WAITING:
// No keys pressed yet
if (currentSwitch != SWITCH_NONE) {
lastPressed = millis();
currentState = STATE_SHORT_PRESSED;
}
break;
case STATE_SHORT_PRESSED:
// Wait to see if this counts as a long press
if (currentSwitch == SWITCH_NONE) {
// Send the keystroke
sendKey(lastSwitchDebounced, true, true);
sendKey(lastSwitchDebounced, true, false);
currentState = STATE_WAITING;
} else if (currentSwitch != lastSwitch) {
// Shouldn't happen, but just in case you're using a different
footpedal...
sendKey(lastSwitchDebounced, true, true);
sendKey(lastSwitchDebounced, true, false);
lastPressed = millis();
} else if (millis() - lastPressed > longPressThreshold) {
currentState = STATE_LONG_PRESSED;
sendKey(lastSwitch, false, true);
}
break;
case STATE_LONG_PRESSED:
// Wait for the transition
if (currentSwitch == SWITCH_NONE) {
[Link] (5 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
currentState = STATE_WAITING;
sendKey(lastSwitch, false, false);
} else if (currentSwitch != lastSwitch) {
// Likewise, switching between inputs shouldn't happen with this
footpedal,
// but just in case...
sendKey(lastSwitch, false, false);
currentState = STATE_SHORT_PRESSED;
lastPressed = millis();
}
}
lastSwitchDebounced = currentSwitch;
}
lastSwitch = currentSwitch;
}
After you upload the code to the Arduino, you’ll also need to reflash the ATMega8U2 chip so that it
can act like a USB keyboard. This sounds scary, but the instructions on the Arduino site can help
you. When you’ve gotten the hang of reflashing the ATMega8U2 with the standard firmware, reflash
it with the [Link] (Uno) or [Link] (Mega) firmware
from Arduino Hacking. After you reflash, unplug, and re-plug your Arduino, it should now appear as a
keyboard. If you made a mistake, don’t panic. Just reflash the standard firmware onto it, and you
can upload new programs again.
The last step is to map the F13..F18 function keys to something useful on the computer. I do this in
software instead of hardcoding it into sendKey so that I can easily change the keycodes without
reflashing the device. I’m on Windows 7 for work and other reasons, so I use AutoHotkey to map the
keys. For example, the following AutoHotkey code maps left and right to Page Up and Page Down,
and the center to Alt-Tab.
F13::Send, {PgUp}
[Link] (6 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
F14::Send, !{Tab}
F15::Send, {PgDn}
This is a great combination for, say, reading an e-book while eating noodles.
On Linux, you can use Xmodmap or XBindKeys to remap your keys. For the Mac, KeyRemap4MacBook
might work – haven’t tried it, though.
Picture!
Making a USB footswitch turned out to be an easy and fun weekend Arduino project. Hope you can
build on this idea for more awesomeness! =) I’m looking forward to finding my next project idea.
[Link] (7 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
Hmm…
More posts about: arduino, code, electronics, geek, sketches //
Short URL: [Link] 2 Comments »
2 Responses to “Code and circuit for a six-function Arduino-based USB footswitch”
1.
Weekly review: Week ending August 27, 2011 | sacha chua :: living an awesome life // Aug 28,
2011 at 8:01 am
[...] 4401 comments 1933 subscribers 4086 on Twitter « Code and circuit for a six-function Arduino-
based USB footswitch [...]
2.
Grigory // Aug 30, 2011 at 8:27 pm
Awesome! Yay for DIY!
I wanted something like this for a “panic button” (locking the screen – I used a “hot corner” in OS X,
but a hardware button is better – I could’ve used KeyRemap4MacBook, but an *external* hardware
button is much cooler) when I was using a desktop (Mac mini, now my mom uses it), but now that I
have a MacBook, there’s no such problem :-)
Comment, share a thought, ask a question...
Please comment as you, not your organization.
[Link] (8 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
Name (required)
Mail (will not be published) (required)
Website
Comment
Submit Comment
Notify me of followup comments via e-mail
« Practising drawing - Weekly review: Week ending August 27, 2011 »
Related posts
● Converted my Arduino foot pedal into a Teensy foot pedal!
● Ruby on Rails: Extending ActiveRecord::Base to define your own
ActiveRecord association methods
● Emacs and W3M: Toggling between work and the Web
● Disabling automatic typeahead find
[Link] (9 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
● I just have to ride out the panic…
On This Day...
● 2010: On a Lenovo X61 — I’ve been saving up for a Lenovo X61 for a while. Drawing had turned
out to be tons of fun, [...]
● 2009: Improv 101: Learning more about characterizations — Improv is so hard! <laugh> It’s a
good kind of hard, though. We warmed up with a game of Hotseat, but [...]
● 2008: Work that I love: reflecting on the whats and hows — At the team-building event the
other day, I got to meet a number of other people who had been with [...]
● 2008: Mouse woes on Ubuntu Hardy — It’s really quite odd. I’ve tried two USB mice (one
wireless, one wired) on my Kubuntu system. They work well… [...]
● 2008: Emacs and W3M: Toggling between work and the Web — Here’s a handy shortcut that
toggles between the W3M web browser and other buffers you’re working on. I use it [...]
● 2007: First day of driving lessons — ACK! One full day of lectures! What was I thinking?! Well, I
wanted a crash course… I’ve *definitely* outgrown high-school-type education. In other [...]
● 2005: Totally blown away — I attended the 74th Annual Convention of Toastmasters
International, and the experience just totally blew me away. The quality of speech was [...]
● 2003: Exim port — firewalled_smtp: driver = smtp port = 1234 firewalled_hosts: driver =
lookuphost transport = firewalled_smtp domains = [Link]:[Link]:[Link]
●
Aug 27
Loading » ● Featured
What can I help you learn?
●
Categories
● Want to help me with what I'm
learning? ❍ Work
● Recent Sketches
❍ Geek
[Link] (10 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
❍ Life
❍ Sketches
●
❍ Delegation
Text Size: 14 px Default
● Recent Comments
❍ James on The confidence of ❍ Presenting
non-expertise
❍ Peter on The confidence of
non-expertise ❍ Social
networking
❍ Alex on Visual book notes:
How to Read a Book
❍ Emacs
❍ Sacha Chua on Visual book
notes: How to Read a Book
❍ Mark on Visual book notes: ❍ Drupal
How to Read a Book
❍ Mark on Visual book notes:
[Link] (11 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
How to Read a Book ❍ Weekly
reports
❍ elmot on Visual book notes:
How to Read a Book
❍ elmot on Visual book notes:
The Start-up of You (Reid
Hoffman, Ben Casnocha)
❍ Charles on Visual book notes:
How to Read a Book
❍ Sandor Benko on Visual book
notes: The Start-up of You
(Reid Hoffman, Ben Casnocha)
● Recent
❍ Mapping my blog archives
❍ Meetup sketchnotes: The
Publishing Side of WordPress,
Andy McIlwain
❍ Weekly review: Week ending
March 9, 2012
❍ The confidence of non-
expertise
❍ Adjusting
[Link] (12 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
August 2011
M T W T F S S
« Jul Sep »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
● Elsewhere on the Net...
Sacha Chua
Comments
240 all
time
View my feed - Subscribe to me
sacha chua :: living an awesome life
Mapping my blog archives
23 hours ago
sacha chua :: living an awesome life
[Link] (13 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
Weekly review: Week ending
March 9, 2012
Saturday at 6:16 pm
sacha chua :: living an awesome life
Meetup sketchnotes: The
Publishing Side of WordPress,
Andy McIlwain
Saturday at 5:44 pm
Twitter
“Posted my sketchnotes from
@andymci's talk on the Publishing
Side of Wordpress: [Link]
DX9xYrl1 #wpto Thanks for the
tips!”
Saturday at 5:44 pm
sacha chua :: living an awesome life
The confidence of non-expertise
Thursday at 5:50 pm
● Meta
❍ Log in
❍ Entries RSS
❍ Comments RSS
❍ [Link]
❍ Subscribe to Posts
Like this? Subscribe using your feed reader or your e-mail client! Follow me on Twitter for quick
[Link] (14 από 15) [12/3/2012 2:02:45 μμ]
Code and circuit for a six-function Arduino-based USB footswitch | sacha chua :: living an awesome life
updates, too. Contact me: I'd love to hear your thoughts, questions, and suggestions!
HomeAboutSpeakingArchivesTopical indexPrivacy Contact
Copyright © 2001-2012 Sacha Chua (sacha@[Link]). Please feel free to reuse content under
a Creative Commons Attribution Non-commercial Sharealike license unless otherwise noted.
[Link] (15 από 15) [12/3/2012 2:02:45 μμ]