Thursday, October 9, 2008

the 1-Key-Keyboard project

Update: Please have a look at my 4-key-keyboard project as well!

Click here to download the complete sourcecode of this project (including .hex and Flash demo program)



Introduction:

The idea for a 1-key keyboard comes from the need as an interaction designer to make “cheap, quick and dirty prototypes.” When creating a quick screen-based demo in e.g. Adobe Flash, a common way of quickly prototyping physical actions is to assign actions to certain key presses on a keyboard. In more advanced prototypes, physical hacks to the circuit board from a keyboard are also often used to interface custom buttons and switches with a computer. (see http://wiki.arcadecontrols.com/wiki/Keyboard_Hacks)

What this project does is to create a keyboard hack that is smaller and cheaper and better reproducible than what one would get when hacking a keyboard circuit; a perfect solution for when you only want to use one switch in your prototype. (Note: 3 IO ports from the microprocessor are still unused, so it’s very easy to add 3 more switches. If you want to interface more than 4 switches, I suggest having a look here: http://www.obdev.at/products/vusb/hidkeys.html)

To maximize the functionality of the 1-key keyboard, I implemented having a different key being sent to the computer for a key-down and a key-up event. This can be useful when you create for example a prototype that has to detect whether a physical object is pushing down a switch or not.

This project implements V-USB (previously called AVR-USB) from Objective Development to emulate a standard USB keyboard using an Atmel AVR microcontroller. This project has been heavily inspired by the EasyLogger Reference project.


What does it do?

This 1-Key-Keyboard can be plugged into any computer and will be recognised as a standard USB keyboard.
When closing the switch, the keystroke ‘]’ will be sent.
When opening the switch, the keystoke ‘[’ will be sent.

That’s all.


What do we need:

1x Atmel ATTiny45-20PU or ATTiny85-20PU microcontroller (I used a ATTiny85, which has more memory than the ATTiny45 but the program will fit the ATTiny45 too)
2x Diodes (0.5W) (ordinary 1N4148 are sufficient)
2x 68 ohm resistors
1x 1.5K resistor
1x 0.1 uF capacitor
1x switch
a piece of prototype-board (striped 2.54mm single-sided)

a programmer for the Atmel AVR
the .hex file from the 1-Key-Keyboard project (Download it here)


Instructions:

First program the microcontroller with the .hex file (don’t forget to set the fuses first to 0xFF 0xDF 0xC1)

Link: Instructions for how to set up the Atmel AVR STK500 programmer to program an 8-pin ATTiny

Link: Instructions for setting up AVR studio for using the Atmel STK500 programmer

Link: Instructions for programming a .hex file with AVR Studio


With the programmed ATTiny, build the following circuit:



1_key_keyboard.jpg


To keep the costs low, you can make a usb-plug from strip board (veroboard) see the pictures below




1-key-keyboard.jpg
dsc00328.jpg



Now it’s time to test the 1-Key-Keyboard! you can test it by opening a text editor and press and release the switch a couple of times. If everything went well, you should see a series like this being typed: ][][][][][



Creating a demo-program in Adobe Flash

Here’s the actionscript 3.0 code for a demo-program which detects whether the switch is pressed or not. If the switch is pressed, the movie-clip ‘onSwitch’ is visible. When released, the movie-clip ‘offSwitch’ is visible.

//actionscript 3.0 source file 
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
offSwitch.visible = true;
onSwitch.visible = false;
function reportKeyDown(event:KeyboardEvent):void {
	switch (String.fromCharCode(event.charCode)) {

		case ']' :
			offSwitch.visible = false;
			onSwitch.visible = true;
			break;

		case '[' :
			offSwitch.visible = true;
			onSwitch.visible = false;

			break;

		default :

			break;

	}
}
// end of source file

Try it out by clicking here and press/release the switch on your 1-Key-Keyboard.

or click here to download a .zip file with the .fla and .swf files




Click here to download the complete sourcecode of this project (including .hex and Flash demo program)



Feedback
Please feel free to leave comments on my 1-key-keyboard project!