Built-in project · SparkFun Pro Micro

Fight stick on a Pro Micro

A four-way lever and six buttons on a Pro Micro, debounced with no added delay and reported as a numpad direction plus the buttons held. The whole build, a SparkFun Pro Micro and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $23. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • INPUT_PULLUP switches that read LOW when pressed
  • Debouncing with a short lock instead of a wait
  • Turning two switches into one axis value
  • Numpad notation for directions
  • Storing button states as bits in one byte
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: SparkFun Pro Micro
  • Parts: 7

How it works

Ten switches each connect one pin to ground: four lever directions on pins 2 to 5, and six buttons on pins 6, 7, 8, 9, 1 and 0. Every pin uses the internal pull-up, so a pressed switch reads LOW and needs no resistor.

The sketch reads all ten, then works out a direction in numpad notation. 5 is neutral, 6 is right, 2 is down, 3 is down-right, and so on. It also builds a list of held buttons.

Whenever the direction or buttons change, one line prints to the Serial Monitor. This sketch does not act as a USB game controller. It only reports the state.

Parts list

7 parts, plus the jumper wires. They retail for about $23; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Game console buttons pin ASparkFun Pro Micro pin 1
Game console buttons pin BSparkFun Pro Micro pin 0
Game console buttons pin GNDSparkFun Pro Micro pin GND
Arcade button (1) pin 1SparkFun Pro Micro pin GND
Arcade button (2) pin 1SparkFun Pro Micro pin GND
Arcade button (3) pin 1SparkFun Pro Micro pin GND
Arcade button (4) pin 1SparkFun Pro Micro pin GND
Game console buttons pin UPSparkFun Pro Micro pin 2
Game console buttons pin DOWNSparkFun Pro Micro pin 3
Game console buttons pin LEFTSparkFun Pro Micro pin 4
Game console buttons pin RIGHTSparkFun Pro Micro pin 5
Arcade button (1) pin 2SparkFun Pro Micro pin 6
Arcade button (2) pin 2SparkFun Pro Micro pin 7
Arcade button (3) pin 2SparkFun Pro Micro pin 8
Arcade button (4) pin 2SparkFun Pro Micro pin 9

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · SparkFun Pro Micro
// Fight stick: a four-way lever and six buttons on a Pro Micro.
//
// Wiring. Every switch has one leg on GND and the other on a pin with the
// internal pull-up on, so a pressed switch reads LOW and needs no resistor.
//
//   lever    UP 2    DOWN 3    LEFT 4    RIGHT 5
//   buttons  1 on 6  2 on 7    3 on 8    4 on 9    5 on 1    6 on 0
//
// In Mokxi the lever is the console pad's D-pad: the arrow keys work it, or
// click its arms. Buttons 1 to 4 are the arcade buttons: click and hold them.
// Buttons 5 and 6 are the pad's A and B, the Z and X keys, standing in for
// the two extra buttons many stick layouts have. Pins 0 and 1 are Serial1's,
// but this sketch talks over USB, so they are free for switches.
//
// The pad's rocker setting is on, which is what a lever's gate does: it
// cannot be pushed up and down at once.
//
// What the Serial Monitor shows is the controller's state each time it
// changes: the direction in numpad notation (5 is neutral, 6 is right, 2 is
// down, 3 is down-right, and so on) followed by the buttons held.
//
// Debounce without lag. The arcade buttons here have bounce turned on, so
// each press closes, opens again and closes within a couple of milliseconds.
// Waiting for a switch to settle would add that wait to every press. Instead
// a change counts the moment it arrives and the input is then locked for
// LOCK_MS, which throws the bounce away without delaying the press.
//
// What this sketch is not: a USB game controller. On a real Pro Micro you
// would hand the same state to a HID library, such as the Arduino Joystick
// library or Keyboard.press(). Mokxi models this chip's USB as a serial pipe
// and nothing more, so the report goes to the Serial Monitor instead.

const uint8_t INPUTS = 10;
const uint8_t PINS[INPUTS] = {2, 3, 4, 5, 6, 7, 8, 9, 1, 0};
enum { UP, DOWN, LEFT, RIGHT, BTN1 };
const uint8_t BUTTONS = INPUTS - BTN1;

// How long an input ignores its pin after a change has been accepted.
const unsigned long LOCK_MS = 5;

bool held[INPUTS];
unsigned long changedAt[INPUTS];

// The last state printed, so a line only goes out when something changed.
int lastDirection = -1;
uint8_t lastButtons = 0xFF;

void readInputs() {
  unsigned long now = millis();
  for (uint8_t i = 0; i < INPUTS; i++) {
    bool down = digitalRead(PINS[i]) == LOW;
    if (down == held[i]) {
      continue;
    }
    if (now - changedAt[i] < LOCK_MS) {
      continue;  // still inside the lock: this edge is the contact bouncing
    }
    held[i] = down;
    changedAt[i] = now;
  }
}

// One axis from its two switches. Both held gives 0, which is the "neutral"
// SOCD rule. A lever cannot make that state, but a worn gate or a wiring
// fault can, and this way the output is still a valid direction.
int axis(bool negative, bool positive) {
  return (positive ? 1 : 0) - (negative ? 1 : 0);
}

void report(int direction, uint8_t buttons) {
  Serial.print(millis());
  Serial.print(" ms  dir ");
  Serial.print(direction);
  Serial.print("  buttons");
  if (buttons == 0) {
    Serial.print(" -");
  }
  for (uint8_t b = 0; b < BUTTONS; b++) {
    if (buttons & (1u << b)) {
      Serial.print(' ');
      Serial.print(b + 1);
    }
  }
  Serial.println();
}

void setup() {
  for (uint8_t i = 0; i < INPUTS; i++) {
    pinMode(PINS[i], INPUT_PULLUP);
  }
  TX_RX_LED_INIT;
  RXLED0;
  TXLED0;
  Serial.begin(115200);
  Serial.println("Fight stick ready: arrow keys move the lever, click the buttons.");
  Serial.println("dir is numpad notation: 7 8 9 / 4 5 6 / 1 2 3, 5 is neutral.");
}

void loop() {
  readInputs();

  int x = axis(held[LEFT], held[RIGHT]);
  int y = axis(held[DOWN], held[UP]);
  int direction = 5 + x + 3 * y;

  uint8_t buttons = 0;
  for (uint8_t b = 0; b < BUTTONS; b++) {
    if (held[BTN1 + b]) {
      buttons |= (uint8_t)(1u << b);
    }
  }

  // The RX lamp is lit while any button is held (it lights on LOW).
  if (buttons != 0) {
    RXLED1;
  } else {
    RXLED0;
  }

  if (direction != lastDirection || buttons != lastButtons) {
    lastDirection = direction;
    lastButtons = buttons;
    report(direction, buttons);
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

readInputs sketch.ino

Loops over all ten pins. If a pin disagrees with the stored state, the change is accepted at once, unless it came within LOCK_MS (5 ms) of the last accepted change. That throws away contact bounce without making a real press wait.

void readInputs() {
  unsigned long now = millis();
  for (uint8_t i = 0; i < INPUTS; i++) {
    bool down = digitalRead(PINS[i]) == LOW;
    if (down == held[i]) {
      continue;
    }
    if (now - changedAt[i] < LOCK_MS) {
      continue;  // still inside the lock: this edge is the contact bouncing
    }
    held[i] = down;
    changedAt[i] = now;
  }
}

axis sketch.ino

Turns two switches into one number: -1, 0 or 1. Pressing both cancels out to 0, so the result is always a valid direction even if a gate is worn or a wire is faulty.

// One axis from its two switches. Both held gives 0, which is the "neutral"
// SOCD rule. A lever cannot make that state, but a worn gate or a wiring
// fault can, and this way the output is still a valid direction.
int axis(bool negative, bool positive) {
  return (positive ? 1 : 0) - (negative ? 1 : 0);
}

report sketch.ino

Prints the time in milliseconds, the direction number, and the list of buttons held, or a dash when none are. loop calls it only when something changed, so the monitor stays readable.

void report(int direction, uint8_t buttons) {
  Serial.print(millis());
  Serial.print(" ms  dir ");
  Serial.print(direction);
  Serial.print("  buttons");
  if (buttons == 0) {
    Serial.print(" -");
  }
  for (uint8_t b = 0; b < BUTTONS; b++) {
    if (buttons & (1u << b)) {
      Serial.print(' ');
      Serial.print(b + 1);
    }
  }
  Serial.println();
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • A button never shows up in the monitor This project
    Check that its other leg goes to GND and its pin matches the PINS list.
  • Direction numbers look mirrored This project
    Swap the UP, DOWN, LEFT and RIGHT wires, or the order of the first four values in PINS.
  • Buttons read the wrong way around, or float. Game console buttons
    These are switches to ground. Set each pin to INPUT_PULLUP and treat LOW as pressed. Without the pull-up the pin floats.
  • One press registers several times. Game console buttons
    Mechanical buttons bounce for a few milliseconds. Wait about 5 to 20 ms before reading again, or use a debounce library.
  • The button reads pressed all the time, or never changes, when wired to a pin. Arcade button
    Use the COM and NO terminals of the microswitch. Wire COM to ground, NO to the pin, and use INPUT_PULLUP. NC is the opposite and reads pressed when released.
  • One press counts as several presses in your code. Arcade button
    Microswitch contacts bounce for a few milliseconds. Ignore changes for 10 to 20 ms after the first one, or react only to the edge and wait before checking again.
  • The upload fails or the wrong board is selected. SparkFun Pro Micro
    Pick SparkFun Pro Micro and match the 5 V 16 MHz or 3.3 V 8 MHz version printed on your board. A wrong choice can make the board hard to program.
  • Serial output seems missing, or the sketch hangs at start. SparkFun Pro Micro
    Serial is on the native USB port, so use while(!Serial) only when you want to wait for a monitor. For Serial1 use pins TX and RX. Pin numbers differ from an Uno, so check the pinout.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.