<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/atom.xsl" type="text/xsl"?><feed
	xmlns="http://www.w3.org/2005/Atom"
	xmlns:thr="http://purl.org/syndication/thread/1.0"
	xml:lang="en-US"
	><title>Sacha Chua - tag - teensy</title>
	<subtitle>Emacs, sketches, and life</subtitle>
	<link rel="self" type="application/atom+xml" href="https://sachachua.com/blog/tag/teensy/feed/atom/index.xml" />
  <link rel="alternate" type="text/html" href="https://sachachua.com/blog/tag/teensy" />
  <id>https://sachachua.com/blog/tag/teensy/feed/atom/index.xml</id>
  <generator uri="https://11ty.dev">11ty</generator>
	<updated>2011-09-30T12:00:00Z</updated>
<entry>
		<title type="html">Converted my Arduino foot pedal into a Teensy foot pedal!</title>
		<link rel="alternate" type="text/html" href="https://sachachua.com/blog/2011/09/converted-my-arduino-foot-pedal-into-a-teensy-foot-pedal/"/>
		<author><name><![CDATA[Sacha Chua]]></name></author>
		<updated>2011-09-17T05:26:06Z</updated>
    <published>2011-09-30T12:00:00Z</published>
    <category term="geek" />
		<id>https://sachachua.com/blog/?p=22506</id>
		<content type="html"><![CDATA[<p><img loading="lazy" style="display: inline; float: right" align="right" src="https://lh4.googleusercontent.com/-xo4s7LpJ9qA/TnFYOF9yoeI/AAAAAAAACBM/PVtdgsy5wnk/s640/C360_2011-09-14%25252021-42-28.jpg" width="240" height="181">Look at how neat my foot pedal looks now! It’s much better than my dangling-wires prototype with the Arduino. At just 0.7” by 1.2”, the <a href="http://www.pjrc.com/teensy/">Teensy USB board</a> was small enough to tuck into the base of the foot pedal. Now it looks almost exactly like how it looked when I bought it from the store, except that the connector at the end of&#160; the cable is USB instead of some funky plug.</p>
<p>Thanks to <a href="http://pjrc.com/teensy/teensyduino.html">Teensyduino</a>, I didn’t have to rewrite a lot of my code. The Teensy was much easier to turn into a keyboard, because I could use the standard bootloader instead of reflashing back and forth between the standard bootloader and a HID keyboard hex. The only wrinkle was that the Teensy library used keyboard scancodes instead of the USB keycodes I had used before. I couldn’t figure out how to send F13 using the Teensy library, so I changed it to send Shift+F1&#8230;Shift+F6, and I updated my AutoHotkey script to map the new keys.</p>
<p>If you happen to have the same foot pedal, you can solder brown and black to GND, orange to B0, blue to B1, tan to B2, and red to B3.</p>
<p>Here’s the new code:</p>
<pre>const int redPin = 3;
const int tanPin = 2;
const int bluePin = 1;
const int orangePin = 0;
const int debounceDelay = 150;
const int longPressThreshold = 650;

int currentState;
int lastSwitch;
long lastDebounce;
long lastPressed;
int lastSwitchDebounced;
#define LED 11

uint8_t buf[8] = { 0 };	/* Keyboard report buffer */

#define SWITCH_NONE 0
#define SWITCH_LEFT 1
#define SWITCH_CENTER 2
#define SWITCH_RIGHT 3

#define STATE_WAITING 0
#define STATE_SHORT_PRESSED 1
#define STATE_LONG_PRESSED 2

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);
  Serial.begin(9600);
  pinMode(LED, OUTPUT); digitalWrite(LED, HIGH);
  delay(200);
  lastSwitch = 0;
  lastDebounce = millis();
  currentState = 0;
  digitalWrite(LED, LOW);
}

int getCurrentSwitch() {
  if (!digitalRead(orangePin)) { return SWITCH_LEFT; }
  if (!digitalRead(tanPin)) { return SWITCH_CENTER; }
  if (!digitalRead(redPin)) { return SWITCH_RIGHT; }
  return SWITCH_NONE;
}

void sendKey(int currentSwitch, boolean isShort, boolean keyDown) {
  Keyboard.set_key1(0);
  Keyboard.set_key2(0);
  Keyboard.set_key3(0);
  Keyboard.set_key4(0);
  Keyboard.set_key5(0);
  Keyboard.set_key6(0);
  int debug = 0;
  digitalWrite(LED, keyDown ? HIGH : LOW);
  if (keyDown) {
    Keyboard.set_modifier(MODIFIERKEY_SHIFT);
    switch (currentSwitch) {
        case SWITCH_LEFT:   if (isShort) { Keyboard.set_key1(KEY_F1); } else { Keyboard.set_key1(KEY_F4); } break;
        case SWITCH_CENTER: if (isShort) { Keyboard.set_key1(KEY_F2); } else { Keyboard.set_key1(KEY_F5); } break;
        case SWITCH_RIGHT:  if (isShort) { Keyboard.set_key1(KEY_F3); } else { Keyboard.set_key1(KEY_F6); } break;
    }
    if (debug) {
      Serial.println(currentSwitch);
      Serial.println(&quot;Down&quot;);
      Serial.println(isShort ? &quot;Short&quot; : &quot;Long&quot;);
    }
  } else {
    Keyboard.set_modifier(0);
    if (debug) { Serial.println(&quot;Up&quot;); Serial.println(isShort ? &quot;Short&quot; : &quot;Long&quot;); }
  }
  if (!debug) { Keyboard.send_now(); }
}

void loop() {
  int currentSwitch = getCurrentSwitch();
  if (currentSwitch != lastSwitch) {
    lastDebounce = millis();
  }
//  Serial.println(currentSwitch);
  // Debounce it
  if (millis() - lastDebounce &gt; 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 &gt; longPressThreshold) {
          currentState = STATE_LONG_PRESSED;
          sendKey(lastSwitch, false, true);
        }
        break;
      case STATE_LONG_PRESSED:
        // Wait for the transition
        if (currentSwitch == SWITCH_NONE) {
          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;
}</pre>
<p>Here is the relevant AutoHotkey snippet I&#8217;m working with: </p>
<pre>+F1::Send, {PgUp}
+F2::Send, !{Tab}
+F3::Send, {PgDn}
+F4::Send, {PgUp}
+F5::Send, !{Tab}
+F6::Send, {PgDn}</pre>
<p>Wheeee! It looks so neat now. I’ve still got some flakiness to work out, but it looks awesome!</p>
<p>&#8212;- Another foot pedal story &#8212;-</p>
<p>One Saturday, W- and I were at Active Surplus to buy some electronic components and to browse their ever-interesting collections. I overheard a woman looking for a footswitch. As she asked one of the Active Surplus employees about the switch characteristics so that she could turn it into a USB keyboard-type device, I couldn&#8217;t help but tell her the footswitch was totally awesome and that I&#8217;d done something similar recently. She was also interested in building a footswitch for transcription. I told her to look for &quot;Sacha Chua Arduino USB footswitch&quot; as a way to get to <a href="https://sachachua.com/blog/2011/08/code-and-circuit-for-a-six-function-arduino-based-usb-footswitch/">my notes on building a 6-way USB footswitch</a> using the very same footswitch she was thinking of buying. We had a short but great conversation about hacking stuff. I hope she ends up making something awesome out of the footswitch! </p>
<p>You can <a href="mailto:sacha@sachachua.com?subject=Comment%20on%20https%3A%2F%2Fsachachua.com%2Fblog%2F2011%2F09%2Fconverted-my-arduino-foot-pedal-into-a-teensy-foot-pedal%2F&body=Name%20you%20want%20to%20be%20credited%20by%20(if%20any)%3A%20%0AMessage%3A%20%0ACan%20I%20share%20your%20comment%20so%20other%20people%20can%20learn%20from%20it%3F%20Yes%2FNo%0A">e-mail me at sacha@sachachua.com</a>.</p>]]></content>
		</entry>
</feed>