2013-10-30

Morse code USB keyboard

Further exploring the capabilities of Arduino Micro as an input device, I made a telegraph key that works as a regular USB keyboard. Here's a demonstration (please excuse my lack of Morse code skills):



For the input part I used the CapacitiveSensor library. The way it works is there's a send pin and a receive pin. You put a resistor between the two pins (I used 1 megaohm) and then also connect a piece of metal to the receive pin that will act as the sensor that you touch. I used a copper coin soldered to a piece of wire. (One serious limitation of capacitive sensing done this way is that the Arduino needs to be grounded, so for example it may not work well with battery-powered laptops.)

I also added a buzzer for feedback, the one I used makes a sound when the signal pin is driven low.

Here's the sketch that's running on the Arduino. As you can see the transmission speed is fixed (though it wouldn't be hard to make it adapt to the observed speed).

If you're a Morse code enthusiast, I also recommend my Android application that listens to Morse code using your smartphone's microphone and translates it to text.

2013-10-25

Emergency mute button

Don't you hate it when one of the fifteen tabs you just opened decides to autoplay some video with an obnoxious audio track? Pressing the mute key on my keyboard just doesn't convey the feelings I have at that moment. I want to hit something hard. That's why I made this emergency mute button:



It connects over USB and speaks the regular HID protocol, so it works with any computer with no additional software or drivers. I got the button itself for 7 dollars on eBay. Inside it there are two screw terminals that are normally connected and get disconnected when you press the big red button (to connect them again you have to twist the button). I connected one of the terminals to one of the digital pins on an Arduino Micro and the other terminal to the ground pin.



Then it was a matter of writing the software for the Arduino that would send the right key events over USB to the computer. Arduino Micro is based on the ATmega32u4 chip (like the Leonardo), so it can act as a USB keyboard out of the box. Problem is, by default the library only knows how to send regular key events, not media keys like mute or volume control. So it requires a slight modification, adding the appropriate HID descriptor and key events. Fortunately I didn't have to figure it all out by myself, because Stefan Jones provided a helpful description of the modifications necessary. With that all that was left was writing the simple sketch reproduced below. It reads the pin that the button is connected to and when it detects a change, it sends the mute key event. (You will notice a slight problem with this: it sends the same event to mute and to unmute the audio. So if the state of the button is out of sync with what the computer thinks, it will work the other way round.)

int sw = 2;

void setup() {
  pinMode(sw, INPUT);
  digitalWrite(sw, HIGH); // pull-up
}

int prevstate = 0;

void loop() {
  int state = digitalRead(sw);
  if (prevstate != state) {
    prevstate = state;
    Remote.mute();
    Remote.clear();
    delay(5); // debounce
  }
}