Update: It works.
See below for Arduino sketch code and a video of SMB3 in action with FCEUltra. There is also some information at the bottom of the post from sinembarg0 on reddit about the Teensy 2, which also provides USB emulation support for a lower price!
Found this Space Invaders TV game at Radio Shack for $15, that’s going to make a fine MAME controller – it actually contains real arcade microswitches and feels like true arcade controls. AWESOME.
Confession: I was going to use Mega Man 2 as the example game, but apparently Mega Man is *super* difficult with arcade controls… or I’m just rusty these days.
Here’s the code and a copy of the Arduino sketch (uggo, code view below is changing brackets on me, just download the RAR file above to get a working version of the code or replace < and > below with less than/greater than symbols):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /* * Space Invaders TV Game Joystick test sketch * Chris Mahoney / March 17, 2013 * * Use keyboard emulation of Leonardo to press keyboard * keys when switches are triggered on Space Invaders controller. */ //Variables for the buttons /* * Switch order: * (8) Ground, L, U, R, D, B, A (2) */ // Arduino pin -> function int buttonLeft=7, buttonUp=6, buttonRight=5, buttonDown=4, buttonB=3, buttonA=2; // Set all pins high before setup() int prevLeft = HIGH, prevUp = HIGH, prevRight = HIGH, prevDown = HIGH, prevB = HIGH, prevA = HIGH; // Array of button pins int buttons[6] = {buttonLeft, buttonUp, buttonRight, buttonDown, buttonB, buttonA}; // Previous state of buttons int prevButtons[6] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; // Actual ASCII values to "press" int buttonNames[6] = {KEY_LEFT_ARROW, KEY_UP_ARROW, KEY_RIGHT_ARROW, KEY_DOWN_ARROW, 122 /* Z */, 120 /* X */}; int pinCount = 6; // # of pins void setup(void) { // Set all pins INPUT to begin for (int pin = 0; pin < pinCount; pin++) { pinMode(pin, INPUT); } // Enable keyboard interaction (REQUIRES LEONARDO/MICRO) Keyboard.begin(); } void loop(void) { // Run each pin each loop for (int pin = 0; pin < pinCount; pin++) { // Get pin value int buttonState = digitalRead(buttons[pin]); // Only process if different than last check if (buttonState != prevButtons[pin]) { // State change, process it // Pressed? if (buttonState == HIGH) { Keyboard.press(buttonNames[pin]); } // Released? if (buttonState == LOW) { Keyboard.release(buttonNames[pin]); } // Set new previous state prevButtons[pin] = buttonState; } // End state change // Delay to avoid freakouts if experienced // delay(10); } } |
Update 2: sinembarg0 on r/Arduino gives us a lot of good info on historical USB keyboard support with the Teensy, and how that translates to emulation available through Leonardo-model Arduinos. According to their post, the Teensy 2 will allow for USB keyboard emulation and clocks in at only around $16 (I spent $35 for the Arduino Micro at Radio Shack). Thanks sinembarg0!
The same result (only one fire button) with very low cost device using an atMega8 and the V-USB library:
http://opentech.altervista.org/dokuwiki/doku.php?id=start:hardware:miniusbdiggerjoystick
demo video:
This is fantastic, thank you for sharing! I have an Atari Flashback that has two Atari joysticks and this would be perfect for creating two enclosed devices that are recognized as HID devices simultaneously.
Any good recommendations on where to best source the ATMega8 from? Cheers!
In these days the atMega8 is hard to find at low cost, at the same price you can find an atMega328.
For this reason i have based the USBDiggerJoystick on atMega328
http://opentech.altervista.org/dokuwiki/doku.php?id=start:hardware:usbdiggerjoystick