Matthew Henderson

college station, tx

Arduino Arcade Controller

May 13, 2013

UPDATE NOTE: When I wrote this, I was using a Pro Micro. Unfortunately, the micro usb connector was a casualty in the mockup stage. Because of that, I did some research on alternatives, and ended up going with the Arduino Leonardo. I only had to change the pin numbers in the program. I also set one of the red buttons to act as the “Enter” key, and set a combination of that button and the start button to act as the “Esc” key when pressed together. I will try to update the code on this page soon. (8/28/2013)

Saturday, for a fun project with some of my young helpers around here, we went about seeing if we could mock up some arcade cabinet controls that they could enjoy. None of them have ever had the joy of using a real joystick all that much. This weekend, we got out the soldering iron and mocked up a temporary board that would let us test our set up of one 4-way joystick, and four red and two white buttons. I decided to use an Arduino board as a HID controller, and the Sparkfun Pro Micro (https://www.sparkfun.com/products/11098) seemed to best fit that bill for this project.

Arcade Controls Tinkering

To accomplish the test mock up, I mounted a breadboard to the back of a piece of plywood to which I also attached the buttons and joystick. The Pro Micro does not come with headers attached, and I had a friend who graciously did the heavy lifting for me on that soldering job. Then, I simply plugged the Pro Micro into the breadboard which allowed me to complete the prototype. I attached the ground pin of the Pro Micro to one of the power rails on the breadboard. Next, I soldered all the grounds on the joystick switches in a series, and then connected those to the ground rail. Then, I attached the signal terminal of each switch to the appropriate Pro Micro pin. Each button was then connected in similar fashion to the ground rail and appropriate Pro Micro pin. The pins I chose to use are in identified in the code comments.

The code was developed from what I found in a Sparkfun tutorial (https://www.sparkfun.com/tutorials/337).

//*******************************
// Used to set up Sparkfun Arduino 
// Pro Micro as Keyboard HID device, 
// allowing use of 4-way joystick, 
// and six buttons. 
// https://www.sparkfun.com/products/11098
//*******************************

// Joystick
// Up (Pin 10) - Up Arrow
// Right (Pin 16) - Right Arrow
// Down (Pin 14) - Down Arrow
// Left (Pin 15) - Left Arrow

// Red Buttons
// Button 1 (Pin 9) - Left Control
// Button 2 (Pin 8) - Left Alt
// Button 3 (Pin 7) - Spacebar
// Button 4 (Pin 6) - Left Shift

// White Buttons
// Button 1 (Pin 5) - 1
// Button 2 (Pin 4) - 5

// Assign variables for buttons to pins
int joyUp = 10;
int joyRight = 16;
int joyDown = 14;
int joyLeft = 15; 
int redButton1 = 9;
int redButton2 = 8;
int redButton3 = 7;
int redButton4 = 6;
int whiteButton1 = 5;
int whiteButton2 = 4;

void setup()
{
pinMode(joyUp, INPUT);  // Set the button as an input
digitalWrite(joyUp  , HIGH);  // Pull the button high
pinMode(joyRight, INPUT); 
digitalWrite(joyRight  , HIGH);
pinMode(joyDown, INPUT); 
digitalWrite(joyDown  , HIGH);
pinMode(joyLeft, INPUT); 
digitalWrite(joyLeft  , HIGH);
pinMode(redButton1, INPUT); 
digitalWrite(redButton1  , HIGH);
pinMode(redButton2, INPUT); 
digitalWrite(redButton2  , HIGH);
pinMode(redButton3, INPUT); 
digitalWrite(redButton3  , HIGH);
pinMode(redButton4, INPUT); 
digitalWrite(redButton4  , HIGH);
pinMode(whiteButton1, INPUT); 
digitalWrite(whiteButton1  , HIGH);
pinMode(whiteButton2, INPUT); 
digitalWrite(whiteButton2  , HIGH);
}

void loop()
{
//Set up joystick
if (digitalRead(joyUp) == 0)  // if button pressed (goes low)
{
Keyboard.press(KEY_UP_ARROW);  // send keystroke to computer
//delay(100);  // delay between sends 
}
else if (digitalRead(joyUp) > 0)
{  
Keyboard.release(KEY_UP_ARROW);  
}

if (digitalRead(joyRight) == 0)  // if button pressed (goes low)
{
Keyboard.press(KEY_RIGHT_ARROW);  // send keystroke to computer
//delay(100);  // delay between sends 
}
else if (digitalRead(joyRight) > 0)
{  
Keyboard.release(KEY_RIGHT_ARROW);  
}

if (digitalRead(joyDown) == 0)  // if button pressed (goes low)
{
Keyboard.press(KEY_DOWN_ARROW);  // send keystroke to computer
//delay(100);  // delay between sends 
}
else if (digitalRead(joyDown) > 0)
{  
Keyboard.release(KEY_DOWN_ARROW);  
}

if (digitalRead(joyLeft) == 0)  // if button pressed (goes low)
{
Keyboard.press(KEY_LEFT_ARROW);  // send keystroke to computer
//delay(100);  // delay between sends 
} 
else if (digitalRead(joyLeft) > 0)
{  
Keyboard.release(KEY_LEFT_ARROW);  
}

// Using .press and .release for buttons 
// so that individual key presses
// are used (no hold to keep firing).

if (digitalRead(redButton1) == 0)
{
Keyboard.press(KEY_LEFT_CTRL);
}
else if (digitalRead(redButton1) > 0)
{  
Keyboard.release(KEY_LEFT_CTRL);  
}

if (digitalRead(redButton2) == 0)
{
Keyboard.press(KEY_LEFT_ALT);
}
else if (digitalRead(redButton2) > 0)
{  
Keyboard.release(KEY_LEFT_ALT);  
}

#define SPACE (0x20)  // Spacebar
if (digitalRead(redButton3) == 0)
{
Keyboard.press(SPACE);
}
else if (digitalRead(redButton3) > 0)
{  
Keyboard.release(SPACE);  
}

if (digitalRead(redButton4) == 0)
{
Keyboard.press(KEY_LEFT_SHIFT);
}
else if (digitalRead(redButton4) > 0)
{  
Keyboard.release(KEY_LEFT_SHIFT);  
}

if (digitalRead(whiteButton1) == 0)
{
Keyboard.press('1');
}
else if (digitalRead(whiteButton1) > 0)
{  
Keyboard.release('1');  
}

if (digitalRead(whiteButton2) == 0)
{
Keyboard.press('5');
}
else if (digitalRead(whiteButton2) > 0)
{  
Keyboard.release('5');  
}
}