OVERVIEW
As well as being able to run programs like other Arduino’s, the Leonardo and Micro models can emulate a USB Keyboard or mouse when plugged into a USB port.
In this tutorial we will see how to connect Arcade controllers to a Leonardo, and use them in the popular retro arcade emulator MAME to play all those nostalgic video games.
You could also use the Arduino MICRO which is the same as the Leonardo but much smaller.
We will connect an 8 way joystick and an arcade button to play one of my favorite game: GALAGA!
PARTS USED
Arduino Leonardo
Arcade Joystick
Arcade Buttons
These are Amazon affiliate links...
They don't cost you anything and it helps me keep the lights on
if you buy something on Amazon. Thank you!
CONNECTIONS
As you can see in the schematic above, most arcade joystick are nothing more than regular switches that get activated when you move the stick in a specific direction.
The NO (Normally Open) pin of the switches are connected to pin 2, 3 and 4 of the Leonardo.
The COM or ground pins are connected together and then go to the GND pin of the Leonardo.
THE CODE
Before we start we need to know what Keyboard shortcuts the MAME emulator uses.
Depending on the game you play you might need more or less keys, since some of them use more buttons and might be 2 Players.
For our tutorial we only need to know the keys for the Player 1 joystick and fire button.
MAME default keys for the P1 joystick are the Arrow keys and the Left-Ctrl key for fire.
Here are the default keyboard shortcuts in MAME.
This list is simplified since there’s a lot more available.
You can do a Google search to find out more.
Keyboard Key MAME Command
5 Insert Coin
1,2 Players 1 -2 Start buttons
P Pause game
Esc Exit game
Arrow Keys Player 1 Control
Left Ctrl Fire 1
Left Alt Fire 2
Space Fire 3
R,F,G,D Player 2 Control
A P2 Fire 1
S P2 Fire 2
Q P2 Fire 3
As always please watch our Tutorial video for more information.
#include "Keyboard.h"
void setup() {
Keyboard.begin();
//Joystick and button connections
pinMode(2, INPUT_PULLUP); //Joystick Left Switch
pinMode(3, INPUT_PULLUP); //Joystick Right Switch
pinMode(4, INPUT_PULLUP); //Fire Button
}
void loop() {
// Check the switches:
int buttonState2 = digitalRead(2);
int buttonState3 = digitalRead(3);
int buttonState4 = digitalRead(4);
// Joystick Left = Arrow Left Key
if (buttonState2 == LOW) {
Keyboard.press(216);
}
else {
Keyboard.release(216);
}
// Joystick Right = Arrow Right Key
if (buttonState3 == LOW) {
Keyboard.press(215);
}
else{
Keyboard.release(215);
}
// Fire Button = Left Ctrl key
if(buttonState4 == LOW) {
Keyboard.press(128);
}
else{
Keyboard.release(128);
}
}
TUTORIAL VIDEO
DOWNLOAD
Copy the above Sketch code in your Arduino IDE software to program your Arduino Leonardo or Micro.
The MAME version we used in this tutorial is called MAMEUI32.
You can Download MAMEUI32 from here: MAMEUI32 Download.
Comentários