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');  
}
}

June 2019 Jun 01, 2019

May 2019 May 01, 2019

April 2019 Apr 01, 2019

March 2019 Mar 01, 2019

Team Us Feb 26, 2019

February 2019 Feb 01, 2019

December 2018 Dec 19, 2018

July 2018 Jul 01, 2018

June 2018 Jun 01, 2018

May 2018 May 01, 2018

April 2018 Apr 01, 2018

December 2017 Dec 03, 2017

November 2017 Nov 05, 2017

October 2017 Oct 01, 2017

Bandina Week May 31, 2017

Book Review: The Plot to Kill God Apr 27, 2016

John (10 song series) Apr 27, 2016

Song for Songs Dec 31, 2015

Falling Away Aug 26, 2015

Easy README Files on Your Mac Jan 28, 2015

Word Clouds from the Gospels (and Acts) Sep 30, 2014

Lord of Your Life? Sep 21, 2014

Search the NY Times Archive Jul 24, 2014

The Farthest Peak Jul 08, 2014

Red River (When You See Me) Jun 26, 2014

Mapping shapefile polygons May 10, 2014

Using git to publish a website Sep 05, 2013

Roads We Don't Choose Aug 30, 2013

Killswitch/Mute Pedal Jul 09, 2013

Hold Out Jun 23, 2013

Muddle-headed Tyranny Jun 11, 2013

Arduino Arcade Controller May 13, 2013

A Place We Have Not Met May 04, 2013

Remember the Little Things Mar 27, 2013