Built-in project · SparkFun Pro Micro

Autopilot and landing gear panel on a SparkFun Pro Micro

An autopilot knob for heading, altitude and vertical speed on a four-digit display, an AP button, and a gear lever with three green lights and a red one in transit. The whole build, a SparkFun Pro Micro and 14 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.

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

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

Autopilot and landing gear panel · SparkFun Pro Microlive0.000 s 0.00x
Press Run, then turn the knob and flip the gear lever
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Making a board appear as a USB game controller
  • Decoding a rotary encoder with a lookup table
  • Turning one event into a short, timed button press
  • Driving a TM1637 display with hand-built segments
  • Running several jobs in one loop without delay
  • Difficulty: Advanced
  • Time: About 45 minutes
  • Board: SparkFun Pro Micro
  • Parts: 16

How it works

The Pro Micro shows up on a PC as a game controller with nine buttons. Turning the knob, flipping the gear switch or holding the AP button each presses one of those buttons, which is how flight sims bind autopilot knobs and gear levers.

The knob has three modes: heading, altitude and vertical speed. Pushing it cycles the mode, and turning it changes that mode's value. The four-digit display shows a letter (H, A or V) and the number. Turn faster than 60 ms per click and the step gets bigger.

The gear switch drives the lights. Flip it down and the red light comes on for 4 seconds while the three green lights lock one by one. Flip it up and the greens go out. The AP button toggles its own yellow light.

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
Four-digit display, TM1637 pin GNDSparkFun Pro Micro pin GND
Rotary encoder, KY-040 pin GNDSparkFun Pro Micro pin GND
Slide switch pin 3SparkFun Pro Micro pin GND
Arcade button pin 1SparkFun Pro Micro pin GND
Resistor, 220 Ω (1) pin 2SparkFun Pro Micro pin GND
Resistor, 220 Ω (2) pin 2SparkFun Pro Micro pin GND
Resistor, 220 Ω (3) pin 2SparkFun Pro Micro pin GND
Resistor, 220 Ω (4) pin 2SparkFun Pro Micro pin GND
Resistor, 220 Ω (5) pin 2SparkFun Pro Micro pin GND
Rotary encoder, KY-040 pin CLKSparkFun Pro Micro pin 3
Rotary encoder, KY-040 pin DTSparkFun Pro Micro pin 4
Rotary encoder, KY-040 pin SWSparkFun Pro Micro pin 5
Arcade button pin 2SparkFun Pro Micro pin 6
Slide switch pin 2SparkFun Pro Micro pin 7
Four-digit display, TM1637 pin DIOSparkFun Pro Micro pin 8
Four-digit display, TM1637 pin CLKSparkFun Pro Micro pin 9
Four-digit display, TM1637 pin VCCSparkFun Pro Micro pin VCC
Rotary encoder, KY-040 pin VCCSparkFun Pro Micro pin VCC
LED, yellow pin ASparkFun Pro Micro pin A0
LED, red pin ASparkFun Pro Micro pin 15
LED, green (3) pin ASparkFun Pro Micro pin 14
LED, green (2) pin ASparkFun Pro Micro pin 16
LED, green (1) pin ASparkFun Pro Micro pin 10
Resistor, 220 Ω (1) pin 1LED, green (1) pin C
Resistor, 220 Ω (2) pin 1LED, green (2) pin C
Resistor, 220 Ω (3) pin 1LED, green (3) pin C
Resistor, 220 Ω (4) pin 1LED, yellow pin C
Resistor, 220 Ω (5) pin 1LED, red pin C

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
// Flight sim autopilot and landing gear panel on a Pro Micro.
//
// One knob sets the autopilot: push it to pick heading, altitude or vertical
// speed, turn it to change that value, and the four-digit display shows it.
// A gear lever drives three green "down and locked" lights and a red "in
// transit" light the way a real gear panel does, and the AP button latches
// its own light.
//
//   gear lever     toggle on 7 (a slide switch here)
//   AP button      momentary on 6           AP light      A0
//   knob           encoder, A on 3, B on 4, push on 5
//   display        TM1637, CLK on 9 and DIO on 8
//   gear lights    nose 10, left 16, right 14 (green), in transit 15 (red)
//
// What the PC sees, as one game controller. Every knob click and every flip
// of the lever is a short press of its own button, which is how autopilot
// knobs and gear levers are bound in flight sims:
//
//   button 0 / 1    gear up / gear down
//   button 2        AP master, held while the button is
//   button 3 / 4    heading + / -        button 5 / 6   altitude + / -
//   button 7 / 8    vertical speed + / -
//
// Honest about the display. On a real panel the value shown comes back from
// the sim, through a tool such as MobiFlight, SimConnect or DCS-BIOS, so it
// always agrees with the cockpit. This sketch keeps its own count instead,
// starting from the values below, so it stays in step only while every click
// reaches the sim.
//
// In Mokxi: turn the knob by dragging or scrolling, click it to change mode,
// click the slide switch to move the gear lever and hold the round button for
// the autopilot.

#include <Joystick.h>
#include <TM1637Display.h>

// ---------------------------------------------------------------------------
// Your build.
// ---------------------------------------------------------------------------

const uint8_t GEAR_PIN = 7;
const uint8_t AP_PIN = 6;
const uint8_t AP_LIGHT = A0;
const uint8_t KNOB_A = 3;
const uint8_t KNOB_B = 4;
const uint8_t KNOB_PUSH = 5;
const uint8_t DISPLAY_CLK = 9;
const uint8_t DISPLAY_DIO = 8;
const uint8_t GREEN[3] = {10, 16, 14};  // nose, left, right
const uint8_t RED = 15;

// How long the gear takes, and when each leg locks after the lever moves.
const unsigned long GEAR_TRAVEL_MS = 4000;
const unsigned long LEG_LOCKS_MS[3] = {3000, 3400, 3800};

const unsigned long PULSE_MS = 100;
const unsigned long GAP_MS = 40;
const unsigned long LOCK_MS = 10;

// Where the values start, and how far one click moves them. Turn the knob
// faster than FAST_MS a click and each click moves FAST_STEP instead, the way
// a real autopilot knob speeds up.
struct Mode {
  char letter;
  int value;
  int minimum;
  int maximum;
  int step;
  int fastStep;
  bool wraps;
  uint8_t upButton;
  uint8_t downButton;
};

Mode modes[] = {
  {'H', 360, 1, 360, 1, 10, true, 3, 4},    // heading, degrees
  {'A', 50, 0, 450, 1, 10, false, 5, 6},    // altitude, hundreds of feet
  {'V', 0, -60, 60, 1, 5, false, 7, 8},     // vertical speed, hundreds of feet a minute
};
const unsigned long FAST_MS = 60;

// ---------------------------------------------------------------------------

const uint8_t MODE_COUNT = sizeof(modes) / sizeof(modes[0]);
const uint8_t BUTTON_COUNT = 9;
enum { GEAR_UP_BUTTON = 0, GEAR_DOWN_BUTTON = 1, AP_BUTTON = 2 };

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, BUTTON_COUNT, 0,
                   false, false, false, false, false, false, false, false, false, false, false);
TM1637Display display(DISPLAY_CLK, DISPLAY_DIO);

// ---- pulses, as on the button box -----------------------------------------

uint8_t waiting[BUTTON_COUNT];
uint8_t phase[BUTTON_COUNT];  // 0 idle, 1 held, 2 gap
unsigned long phaseAt[BUTTON_COUNT];

void pulse(uint8_t b) {
  if (waiting[b] < 20) waiting[b]++;
}

void runPulses(unsigned long now) {
  for (uint8_t b = 0; b < BUTTON_COUNT; b++) {
    if (phase[b] == 0 && waiting[b] > 0) {
      waiting[b]--;
      phase[b] = 1;
      phaseAt[b] = now;
    } else if (phase[b] == 1 && now - phaseAt[b] >= PULSE_MS) {
      phase[b] = 2;
      phaseAt[b] = now;
    } else if (phase[b] == 2 && now - phaseAt[b] >= GAP_MS) {
      phase[b] = 0;
    }
  }
}

// ---- the knob -------------------------------------------------------------

const int8_t QUARTER[16] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
uint8_t knobState;
int8_t knobSteps;
unsigned long lastClickAt;
uint8_t mode = 0;
bool pushed = false;
unsigned long pushedAt = 0;

void click(int direction, unsigned long now) {
  Mode &m = modes[mode];
  int step = (now - lastClickAt < FAST_MS) ? m.fastStep : m.step;
  lastClickAt = now;
  int v = m.value + direction * step;
  if (m.wraps) {
    int span = m.maximum - m.minimum + 1;
    while (v < m.minimum) v += span;
    while (v > m.maximum) v -= span;
  } else {
    if (v < m.minimum) v = m.minimum;
    if (v > m.maximum) v = m.maximum;
  }
  m.value = v;
  // One press per click whatever the step: the sim does its own speeding up.
  pulse(direction > 0 ? m.upButton : m.downButton);
  Serial.print(m.letter);
  Serial.print(' ');
  Serial.println(m.value);
}

void readKnob(unsigned long now) {
  uint8_t state = (uint8_t)((digitalRead(KNOB_A) == HIGH ? 2 : 0) | (digitalRead(KNOB_B) == HIGH ? 1 : 0));
  if (state != knobState) {
    knobSteps += QUARTER[(knobState << 2) | state];
    knobState = state;
    if (state == 3) {
      if (knobSteps >= 2) click(1, now);
      else if (knobSteps <= -2) click(-1, now);
      knobSteps = 0;
    }
  }
  bool down = digitalRead(KNOB_PUSH) == LOW;
  if (down != pushed && now - pushedAt >= LOCK_MS) {
    pushed = down;
    pushedAt = now;
    if (down) {
      mode = (uint8_t)((mode + 1) % MODE_COUNT);
      Serial.print("mode ");
      Serial.println(modes[mode].letter);
    }
  }
}

// ---- the gear -------------------------------------------------------------

bool leverDown = false;
unsigned long leverAt = 0;
unsigned long leverChangedAt = 0;
bool travelling = false;

void readLever(unsigned long now) {
  bool down = digitalRead(GEAR_PIN) == LOW;
  if (down == leverDown || now - leverChangedAt < LOCK_MS) return;
  leverDown = down;
  leverChangedAt = now;
  leverAt = now;
  travelling = true;
  pulse(down ? GEAR_DOWN_BUTTON : GEAR_UP_BUTTON);
  Serial.println(down ? "gear down" : "gear up");
}

void showGear(unsigned long now) {
  unsigned long t = now - leverAt;
  if (travelling && t >= GEAR_TRAVEL_MS) {
    travelling = false;
    Serial.println(leverDown ? "three green" : "gear up and locked");
  }
  for (uint8_t leg = 0; leg < 3; leg++) {
    bool locked = leverDown && (!travelling || t >= LEG_LOCKS_MS[leg]);
    digitalWrite(GREEN[leg], locked ? HIGH : LOW);
  }
  digitalWrite(RED, travelling ? HIGH : LOW);
}

// ---- the autopilot button -------------------------------------------------

bool apHeld = false;
bool apOn = false;
unsigned long apAt = 0;

void readAp(unsigned long now) {
  bool down = digitalRead(AP_PIN) == LOW;
  if (down == apHeld || now - apAt < LOCK_MS) return;
  apHeld = down;
  apAt = now;
  if (down) {
    // The light is the panel's own latch: the sim's AP state is the truth,
    // and a real panel would take the light from it.
    apOn = !apOn;
    Serial.println(apOn ? "AP on" : "AP off");
  }
  digitalWrite(AP_LIGHT, apOn ? HIGH : LOW);
}

// ---- the display ----------------------------------------------------------

uint8_t shown[4] = {0xFF, 0xFF, 0xFF, 0xFF};

uint8_t letter(char c) {
  switch (c) {
    case 'H': return SEG_B | SEG_C | SEG_E | SEG_F | SEG_G;
    case 'A': return SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G;
    case 'V': return SEG_C | SEG_D | SEG_E;  // a lower-case u, the nearest a 7-segment gets
    default: return 0;
  }
}

void showValue() {
  const Mode &m = modes[mode];
  uint8_t digits[4];
  digits[0] = letter(m.letter);
  // The value right-aligned in the last three digits, a minus sign just left
  // of it when it is negative.
  int v = m.value < 0 ? -m.value : m.value;
  int8_t at = 3;
  digits[1] = digits[2] = 0;
  do {
    digits[at--] = display.encodeDigit((uint8_t)(v % 10));
    v /= 10;
  } while (v > 0 && at >= 1);
  if (m.value < 0 && at >= 1) digits[at] = SEG_G;
  bool same = true;
  for (uint8_t i = 0; i < 4; i++) same = same && digits[i] == shown[i];
  if (same) return;
  for (uint8_t i = 0; i < 4; i++) shown[i] = digits[i];
  display.setSegments(digits);
}

void setup() {
  pinMode(GEAR_PIN, INPUT_PULLUP);
  pinMode(AP_PIN, INPUT_PULLUP);
  pinMode(KNOB_A, INPUT_PULLUP);
  pinMode(KNOB_B, INPUT_PULLUP);
  pinMode(KNOB_PUSH, INPUT_PULLUP);
  pinMode(AP_LIGHT, OUTPUT);
  pinMode(RED, OUTPUT);
  for (uint8_t leg = 0; leg < 3; leg++) pinMode(GREEN[leg], OUTPUT);
  knobState = (uint8_t)((digitalRead(KNOB_A) == HIGH ? 2 : 0) | (digitalRead(KNOB_B) == HIGH ? 1 : 0));

  Serial.begin(115200);
  Serial.println("Autopilot panel ready: push the knob for HDG, ALT or VS.");
  display.setBrightness(5);

  // The lever as it is at power-up is where the gear already is: no pulse,
  // no transit, just the lights for that state.
  delay(20);
  leverDown = digitalRead(GEAR_PIN) == LOW;
  Joystick.begin(false);
}

void loop() {
  unsigned long now = millis();
  readKnob(now);
  readLever(now);
  readAp(now);
  runPulses(now);
  showGear(now);
  showValue();
  for (uint8_t b = 0; b < BUTTON_COUNT; b++) Joystick.setButton(b, phase[b] == 1);
  Joystick.setButton(AP_BUTTON, apHeld);
  Joystick.sendState();
}

Code walkthrough

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

pulse sketch.ino

Every knob click and gear flip needs to reach the PC as a short button press, not a held button. pulse just adds one to a waiting count for that button, capped at 20. It does nothing else, so the rest of the sketch never has to wait for a press to finish.

void pulse(uint8_t b) {
  if (waiting[b] < 20) waiting[b]++;
}

runPulses sketch.ino

This turns the waiting counts into real presses. For each button it holds the press for PULSE_MS (100 ms), then leaves a gap of GAP_MS (40 ms) before starting the next one. Fast knob turns queue up and play out as clean, separate presses the sim can count.

void runPulses(unsigned long now) {
  for (uint8_t b = 0; b < BUTTON_COUNT; b++) {
    if (phase[b] == 0 && waiting[b] > 0) {
      waiting[b]--;
      phase[b] = 1;
      phaseAt[b] = now;
    } else if (phase[b] == 1 && now - phaseAt[b] >= PULSE_MS) {
      phase[b] = 2;
      phaseAt[b] = now;
    } else if (phase[b] == 2 && now - phaseAt[b] >= GAP_MS) {
      phase[b] = 0;
    }
  }
}

readKnob sketch.ino

The two knob pins form a two-bit state. A lookup table called QUARTER says whether each state change is a step forward, back, or noise. When the state returns to rest and two steps have added up, it calls click. The push switch changes the mode, with a 10 ms lockout.

void readKnob(unsigned long now) {
  uint8_t state = (uint8_t)((digitalRead(KNOB_A) == HIGH ? 2 : 0) | (digitalRead(KNOB_B) == HIGH ? 1 : 0));
  if (state != knobState) {
    knobSteps += QUARTER[(knobState << 2) | state];
    knobState = state;
    if (state == 3) {
      if (knobSteps >= 2) click(1, now);
      else if (knobSteps <= -2) click(-1, now);
      knobSteps = 0;
    }
  }
  bool down = digitalRead(KNOB_PUSH) == LOW;
  if (down != pushed && now - pushedAt >= LOCK_MS) {
    pushed = down;
    pushedAt = now;
    if (down) {
      mode = (uint8_t)((mode + 1) % MODE_COUNT);
      Serial.print("mode ");
      Serial.println(modes[mode].letter);
    }
  }
}

showGear sketch.ino

Runs every loop. After the lever moves, travelling stays true for GEAR_TRAVEL_MS. While gear is going down, each green light turns on once its own time in LEG_LOCKS_MS passes, and the red light stays on until travel ends. Gear up simply leaves the greens off.

void showGear(unsigned long now) {
  unsigned long t = now - leverAt;
  if (travelling && t >= GEAR_TRAVEL_MS) {
    travelling = false;
    Serial.println(leverDown ? "three green" : "gear up and locked");
  }
  for (uint8_t leg = 0; leg < 3; leg++) {
    bool locked = leverDown && (!travelling || t >= LEG_LOCKS_MS[leg]);
    digitalWrite(GREEN[leg], locked ? HIGH : LOW);
  }
  digitalWrite(RED, travelling ? HIGH : LOW);
}

Troubleshooting

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

  • Turning the knob changes the value backward This project
    Swap the wires on pins 3 and 4, or swap KNOB_A and KNOB_B in the sketch.
  • An LED never lights This project
    LEDs only work one way. The anode (A) goes to the Pro Micro pin and the cathode (C) goes through its 220 ohm resistor to ground.
  • The display value drifts from the sim This project
    The sketch keeps its own count and never hears back from the sim, so any click that does not reach the sim leaves them out of step.
  • The display does nothing. Four-digit display
    Connect CLK and DIO to two digital pins, plus VCC and GND. It is not I2C, so no address and no Wire library. Use a TM1637 library and give it the same two pins.
  • Digits look wrong or in a strange order. Four-digit display
    Check the library's segment mapping and digit order. The colon is bit 7 of the second digit. Brightness is set separately and can be 0 to 7.
  • Turning the knob counts twice, skips, or goes the wrong way. The contacts bounce and polling is too slow. Rotary encoder
    Use interrupts or a proper encoder library on pins CLK and DT. Use INPUT_PULLUP, since the contacts are open and shut to ground. Swap CLK and DT to flip direction.
  • The KY-040 pins read wrong or noisy. The board has pull-ups to its + pin, so + must be powered, and its switch needs debounce. Rotary encoder
    Connect + to 5 V or 3.3 V to match your board's logic level and GND to ground. Debounce SW for about 20 ms. One click often equals four state changes.
  • The input reads random values in one position. Slide switch
    A switch only connects or disconnects. Add a pull-up or pull-down resistor, or use INPUT_PULLUP, so the pin has a defined level when the switch is open.

Change it and keep it

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