Peuchele bio photo

Peuchele

Pedí pollo que yo pago la diferencia!

Email

Do you have a spare Teensy 3.0, like I did, and you want to (under) use it? this might be a good one-afternoon DIY project: an Arcade Joystick.

Besides the ARM powered board, you will need a 4-switch arcade joystick, buttons, and a box that you need to still from your wife.

The firmware emulates an USB keyboard, so all you have to do is connect the switches from the stick and the buttons to the teensy digital inputs. No other external components are needed. For convenience, I’ve used inputs 0 to 7. I’m using only 4 push buttons, but you can easily extend it if you want, just adjust the NUM_BUTTONS and the keys array accordingly.

Joystick switches emulates the arrow keys, and the buttons, well, you need to program them to whatever fits better for your games, in my case [Enter], [Space], [Left Alt] and [Z] did the trick for Super Frog HD. I should think in putting an internal switch to change the button’s configuration to something more standard like the MAME layout.

Just for reference, this is the list of materials I’ve used for this project:

Qty Name Link Price (U$S)
1 Teensy 3.0 www.pjrc.com 19.00
1 4-Ways Red Ball Arcade Joystick with 4-Switch dx.com 11.55
4 Arcade Joystick Button dx.com and dx.com 3.59 each
1 Box -  

The Code

Of course, I am using the existing Teensy USB keyboard libraries, so the code is pretty simple – 32 lines of code.

/* Arcade Keyboard-Joystick */

#include <usb_keyboard.h>

#define NUM_BUTTONS 8
int keys[NUM_BUTTONS] = {KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_ENTER, KEY_SPACE, KEY_LEFT_ALT, KEY_Z};
int mask = 0;
int i = 0;

void setup() {
	for (i = 0; i < NUM_BUTTONS; i++) {
		pinMode(i, INPUT_PULLUP);
	}
	delay(1000);
}

void loop() {
	for (i = 0; i < NUM_BUTTONS; i++) {
		if (digitalRead(i) == LOW) {
			if (!(mask & (1 << i))) {
				Keyboard.press(keys[i] | (0x40 << 8));
				mask |= (1 << i);
			}
		} else {
			if ((mask & (1 << i))) {
				Keyboard.release(keys[i] | (0x40 << 8));
				mask &= ~(1 << i);
			}
		}
	}
	delay(2);
}

See joystick.ino on gist

Connection Diagram

No explanation needed

Joystick Connection Diagram
Joystick Connection Diagram

Pictures

Joystick connections Box holes Inside Joystick
Inside Joystick Joystick Joystick testing with uncle Alf