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
  }
}

No comments:

Post a Comment