Landing gear state machine in C: simulator and code checker
Fly the six-state landing gear controller from the classic embedded C project, then paste your own C program to see its state table and the bugs that make most versions crash or loop forever. Free, no sign-up, runs in your browser.
Gomez 2000 · FAA-H-8083-31B ch. 13 · 14 CFR 25.729 · C11 · 440 student programs compiled and run · Updated 20 Nov 2026
Cockpit and sensors
Try a scenario
The machine steps every 0.1 s of simulated time. All three legs move together; the up-lock and down-lock switches close at the ends of travel. Gear travel time is an example value; real aircraft differ.
Nothing is uploaded. The checker reads the code like a reviewer would; it does not compile or run it.
The same machine as the simulator in about 90 lines of C99: no recursion, every scanf checked, no enum read with %d. One input line per step: lever squat up-locks down-locks hydraulics dt.
/* Landing gear state machine: reference version for /tools/landing-gear-state-machine.
* One input line per step: lever squat up down hyd dt
* lever 1 = up, squat 1 = weight on wheels, up/down = up-locks / down-locks made (0..3),
* hyd 1 = hydraulic pressure available, dt = seconds since the last step.
* No recursion, every scanf result is checked, enums are never read with %d.
* Build: cc -std=c99 -Wall -Wextra -pedantic landing_gear.c */
#include <stdio.h>
typedef enum { GEAR_DOWN, CHECK_TAKEOFF, RAISING, GEAR_UP, CHECK_LANDING, LOWERING } State;
static const char *const NAME[] = { "GEAR_DOWN", "CHECK_TAKEOFF", "RAISING", "GEAR_UP", "CHECK_LANDING", "LOWERING" };
#define TAKEOFF_DELAY 3.0
typedef struct { int lever, squat, up, down, hyd; double dt; } Inputs;
typedef struct { State s; int prev_lever; double timer; int springs; int bounce; } Machine;
static void step(Machine *m, const Inputs *in)
{
int rising = (in->lever == 1) && (m->prev_lever == 0);
int next_prev = in->lever;
switch (m->s) {
case GEAR_DOWN:
if (rising && in->squat == 0) { m->s = CHECK_TAKEOFF; m->timer = TAKEOFF_DELAY; m->bounce = 0; }
else if (m->bounce && in->lever == 1) next_prev = 0; /* bounce: keep the edge pending */
else m->bounce = 0;
break;
case CHECK_TAKEOFF:
if (in->squat == 1 || in->lever == 0) { m->s = GEAR_DOWN; next_prev = 0; m->bounce = (in->lever == 1); }
else {
m->timer -= in->dt;
if (m->timer <= 1e-9) {
if (in->hyd == 1) m->s = RAISING;
else { m->s = GEAR_DOWN; next_prev = 0; }
}
}
break;
case RAISING:
if (in->up == 3) m->s = GEAR_UP;
else if (in->lever == 0) m->s = CHECK_LANDING;
break;
case GEAR_UP:
if (in->lever == 0) m->s = CHECK_LANDING;
break;
case CHECK_LANDING:
m->springs = (in->hyd == 1) ? 0 : 1; /* no pressure: gas springs, squat switch ignored */
m->s = LOWERING;
break;
case LOWERING:
if (in->down == 3) { m->s = GEAR_DOWN; m->springs = 0; }
else if (rising && m->springs == 0) { m->s = CHECK_TAKEOFF; m->timer = TAKEOFF_DELAY; }
break;
default: /* corrupted state: fail safe */
m->s = LOWERING; m->springs = (in->hyd == 1) ? 0 : 1;
break;
}
m->prev_lever = next_prev;
}
int main(void)
{
Machine m = { GEAR_DOWN, 0, 0.0, 0, 0 };
Inputs in;
int n;
while ((n = scanf("%d %d %d %d %d %lf", &in.lever, &in.squat, &in.up, &in.down, &in.hyd, &in.dt)) != EOF) {
if (n != 6) { /* not a number: drop the rest of the line */
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
fprintf(stderr, "skipped a bad line\n");
continue;
}
step(&m, &in);
{
State s = m.s;
const char *lights = (s == GEAR_DOWN || s == CHECK_TAKEOFF) ? "GREEN" : (s == GEAR_UP) ? "OFF" : "RED";
const char *valve = (s == RAISING) ? "UP" : (s == LOWERING && m.springs == 0) ? "DOWN" : "NONE";
int pump = ((s == RAISING) || (s == LOWERING && m.springs == 0)) && in.hyd == 1;
printf("%s %s %s %d %d\n", NAME[s], lights, valve, pump, m.springs);
}
}
return 0;
}- Gear down. Move the lever, take off, or pick a scenario.
- Bug, line 22: GearDown() calls itself. Every time the input does not change the state, the call nests one level deeper and never returns, so the stack grows until the program crashes (MISRA C:2012 Rule 17.2). Return to a loop instead.
- Bug, line 26: scanf %d writes an int into enum variables lever, squat_switch, hydraulics, limit_switch (lines 26, 36, 48, 60, 70, 77). An enum's size is up to the compiler (C11 6.7.2.2: char, signed or unsigned int); with a 1-byte enum (GCC -fshort-enums) scanf writes 4 bytes into 1. Read into an int, check the range, then assign.
- Bug, line 26: 6 scanf return values are not checked. Type a letter, or end the input (Ctrl-D, or a test file that runs out), and scanf returns 0 or EOF without reading anything: the variables keep their old values and a while(1) loop repeats forever.
- Bug, line 45: RaisingGear() calls itself. Every time the input does not change the state, the call nests one level deeper and never returns, so the stack grows until the program crashes (MISRA C:2012 Rule 17.2). Return to a loop instead.
- Bug, line 57: GearUp() calls itself. Every time the input does not change the state, the call nests one level deeper and never returns, so the stack grows until the program crashes (MISRA C:2012 Rule 17.2). Return to a loop instead.
- Bug, line 74: LoweringGear() calls itself. Every time the input does not change the state, the call nests one level deeper and never returns, so the stack grows until the program crashes (MISRA C:2012 Rule 17.2). Return to a loop instead.
- Brief: No timer or delay found. The brief waits 3 s after takeoff (Gomez 2000: 2 s) so that a bounce on the runway does not start a retraction.
- Brief: No previous lever value found. The brief reacts to the lever's rising and falling edges, not its level: a lever left UP while parked must not raise the gear after takeoff.
- Brief: No hydraulic failure path found (gas springs / emergency extension). The brief extends the gear with gas springs when hydraulics fail, ignoring the squat switch.
- Brief, line 42: CHECK_TAKEOFF → RAISING_GEAR is taken when the hydraulics have failed (hydraulics == FAILED). Gas springs or free fall can only extend the gear; with no hydraulic pressure the gear should stay down.
| From | To | When | Line |
|---|---|---|---|
| GEAR_DOWN | CHECK_TAKEOFF | lever == LEVER_UP && squat_switch == OFF | 28 |
| CHECK_TAKEOFF | GEAR_DOWN (call) | lever == LEVER_DOWN || squat_switch == ON | 38 |
| CHECK_TAKEOFF | RAISING_GEAR | hydraulics == WORKING | 40 |
| CHECK_TAKEOFF | RAISING_GEAR | hydraulics == FAILED | 42 |
| RAISING_GEAR | GEAR_UP | limit_switch == ON | 50 |
| RAISING_GEAR | CHECK_LANDING | lever == LEVER_DOWN | 52 |
| GEAR_UP | CHECK_LANDING | lever == LEVER_DOWN | 62 |
| CHECK_LANDING | LOWERING_GEAR | always | 71 |
| LOWERING_GEAR | GEAR_DOWN | limit_switch == ON | 79 |
| LOWERING_GEAR | CHECK_TAKEOFF | lever == LEVER_UP | 81 |
- Compiles cleanly with cc -std=c99 -Wall -Wextra -pedantic -Werror.
- Same answers as the simulator on the 24-step test flight below and on 5,000 random input steps (every output line identical).
- Bad input: empty input ends the program; a line with a letter is skipped with a message.
- The checker on this file: 6 states, 10 transitions, no findings.
Swap the scanf loop for your sensor reads and a periodic timer on a microcontroller; the step() function stays the same.
How the landing gear state machine works
The gear has three legs (nose, left, right), each moved by hydraulic pressure through a direction valve (UP or DOWN). The software sees the pilot's gear lever, a squat switch that senses weight on the wheels, and on each leg an up-lock and a down-lock limit switch. It drives the valve, the pump and one light per leg.
The project brief uses six states. It follows the example Martin Gomez published in Embedded Systems Programming in December 2000, with one state added before landing and a 3 s delay instead of Gomez's 2 s.
| State | Leaves when | Lights · valve |
|---|---|---|
| Gear down | The lever moves from DOWN to UP (a rising edge) and the aircraft is airborne → Checking before takeoff. | Green · none |
| Checking before takeoff | Weight back on the wheels, or lever down → Gear down. Airborne for the whole 3 s with pressure → Raising gear. | Green · none |
| Raising gear | All three up-locks made → Gear up. Lever down → Checking before landing. | Red · UP, pump on |
| Gear up | Lever down → Checking before landing. | Off · none |
| Checking before landing | Pressure available → Lowering gear with the pump; none → Lowering gear on the gas springs, squat switch ignored. | Red · none |
| Lowering gear | All three down-locks made → Gear down. Lever moved up again (go-around) → Checking before takeoff, unless the springs are extending it. | Red · DOWN, pump on |
The lights follow the FAA handbook's convention: off when up and locked, red when unsafe or in transit, green when down and locked. 14 CFR 25.729 requires an emergency way to extend the gear when the normal system or any single hydraulic or electric supply fails. Here that is the gas springs; real aircraft use free fall, a hand crank or a nitrogen bottle.
Three rules carry the safety logic. No retraction on the ground: the squat switch must say "airborne". Edges, not levels: a lever left UP while parked does nothing after takeoff until the pilot moves it again. A delay: a bounce on the runway restarts the wait, so the gear only comes up once the aircraft has been in the air for the full 3 s. The lever edge survives a bounce, so the pilot does not have to move the lever again.
The test flight, step by step
These 24 steps run through both the simulator's code and the C reference; every line matched. Inputs are lever (1 = up), squat switch (1 = weight on wheels), up-locks and down-locks made, hydraulics (1 = pressure) and the time since the last step.
| # | What happens | Inputs | State after | Lights |
|---|---|---|---|---|
| 1 | Lever moved up while parked | 1 1 0 3 1 0.5 | Gear down | green |
| 2 | Takes off with the lever still up | 1 0 0 3 1 0.5 | Gear down | green |
| 3 | Pilot moves the lever down | 0 0 0 3 1 0.5 | Gear down | green |
| 4 | Lever up in the air | 1 0 0 3 1 0.5 | Checking before takeoff (3.0 s left) | green |
| 5 | Bounce: wheels touch the runway | 1 1 0 3 1 0.5 | Gear down | green |
| 6 | Still on the runway | 1 1 0 3 1 0.5 | Gear down | green |
| 7 | Airborne again, lever still up | 1 0 0 3 1 0.5 | Checking before takeoff (3.0 s left) | green |
| 8 | 1 s airborne | 1 0 0 3 1 1 | Checking before takeoff (2.0 s left) | green |
| 9 | 2 s airborne | 1 0 0 3 1 1 | Checking before takeoff (1.0 s left) | green |
| 10 | 3 s airborne | 1 0 0 3 1 1 | Raising gear | red |
| 11 | Gear travelling, one up-lock made | 1 0 1 0 1 2 | Raising gear | red |
| 12 | All three up-locks made | 1 0 3 0 1 2 | Gear up | off |
| 13 | Approach: lever down | 0 0 3 0 1 60 | Checking before landing | red |
| 14 | Check before landing | 0 0 3 0 1 0.1 | Lowering gear | red |
| 15 | Gear travelling down | 0 0 0 1 1 2 | Lowering gear | red |
| 16 | Go-around: lever up while lowering | 1 0 0 1 1 0.5 | Checking before takeoff (3.0 s left) | green |
| 17 | 1.5 s | 1 0 0 1 1 1.5 | Checking before takeoff (1.5 s left) | green |
| 18 | 3 s: retract again | 1 0 0 1 1 1.5 | Raising gear | red |
| 19 | Up and locked | 1 0 3 0 1 2 | Gear up | off |
| 20 | Hydraulics fail, lever down | 0 0 3 0 0 30 | Checking before landing | red |
| 21 | Check before landing: no pressure | 0 0 3 0 0 0.1 | Lowering gear, springs | red |
| 22 | Lever up during spring extension: ignored | 1 0 0 2 0 1 | Lowering gear, springs | red |
| 23 | All three down-locks made | 0 0 0 3 0 1 | Gear down | green |
| 24 | Landed | 0 1 0 3 0 60 | Gear down | green |
Step 2 is the parked-lever case: the aircraft is airborne with the lever UP, and nothing happens because there was no rising edge in the air. Steps 5 and 6 are a bounce: the machine goes back to Gear down but keeps the lever edge, so the wait starts again once the aircraft is airborne (step 7). Steps 20 to 22 show the springs: no pump, no valve, and a lever moved up during the extension is ignored, because springs cannot raise the gear.
What 972 student programs do
Skill-Lync's Embedded Programming Essentials project asks for this machine in C. 972 reports are public; 748 contain a C program (544 different ones after removing exact copies) and 440 compile as printed. We ran each compiled program twice, once with no input (as when a test file ends or you press Ctrl-D) and once with a letter instead of a number, then read all 748 with the checker above (as of 20 Nov 2026).
| Finding | Programs | Why it matters |
|---|---|---|
| Crashed when the input ended (segmentation fault) | 213 of 440 | 211 of them have state functions that call themselves or each other. Each pass that does not change the state adds a stack frame, and the stack ran out in about half a second. |
| Looped forever, printing, when the input ended | 153 of 440 | scanf returns EOF at once, the variables keep their old values and while(1) repeats. A letter does the same: scanf never consumes it. |
| Stopped cleanly or waited for valid input | 74 of 440 | Some of these simply exit after one pass. |
| A state function calls itself or another state function | 470 | MISRA C:2012 Rule 17.2 forbids direct and indirect recursion; here the stack use has no bound at all. |
| scanf result never checked | 598 | The cause of the endless loops above. |
| scanf("%d") straight into an enum variable | 452 | Undefined behaviour in C. On a desktop compiler the enum is 4 bytes and it works by luck. Built with GCC's -fshort-enums the enum is 1 byte: in our test, typing 0 for the squat switch also cleared the two enum variables stored after it. |
| Raises the gear when the hydraulics have failed | 193 | A branch for "hydraulics not working" still goes to Raising gear. Gas springs or free fall can only extend the gear. |
| No timer or delay anywhere | 715 | The brief's 3 s check before takeoff becomes a single pass. |
| No previous lever value (no edge detection) | 730 | The parked-lever case retracts the gear after takeoff. |
| No hydraulic-failure path at all | 376 | No springs, backup or emergency extension anywhere in the code. |
One split is not a bug in any single program but is worth knowing. To leave Gear down, 162 programs require the squat switch to be open / off and 177 require it closed / on; 40 do not test it at all. The brief's diagram says "open". Both readings exist in real aircraft, which is why a signal named weight_on_wheels is safer than squat_switch == ON.
Of the 440 programs that compile, 53 have none of the four coding bugs the checker looks for (recursion, unchecked scanf, scanf into an enum, a missing break).
Three fixes that remove most of the bugs
- Return, don't call. Each state function sets current_state and returns. Only the loop in main() calls state functions: while (1) state_table[current_state]();. A function-pointer table is fine (Gomez uses one); calling GearDown() from inside GearDown() is not.
- Check scanf. if (scanf("%d %d", &lever, &squat) != 2) catches both a letter and the end of input. On a letter, read and drop the rest of the line; on EOF, stop.
- Read ints, then convert. Read into an int, check it is 0 or 1, then assign it to the enum. On a microcontroller the inputs come from GPIO pins instead, but the same rule holds: validate before you trust.
The reference code in the tool does all three, plus the edge detection, the delay and the springs path, in about 90 lines.
What this tool does not cover
- A real compiler. The checker reads patterns (enums, functions, assignments, if conditions, scanf calls). Macros, function pointers set at run time, or states spread across several files can hide transitions from it. It reports what it finds; it does not prove code correct.
- Per-leg faults. The simulator moves the three legs together. A real controller also handles one leg that will not lock, a gear-disagree warning when lever and gear differ (25.729(e)(7)), the landing warning horn and a pump timeout. Gomez's article suggests a 60 s pump timeout as an exercise.
- Certification. Airborne software is developed to DO-178C with far more than this. Use this machine to learn state machine structure, not as a design.
State machines, input handling and MISRA-style C are what embedded interviews test. Skill-Lync's embedded courses teach them on STM32 and Arduino hardware, with projects like this one.
How we checked the tool
The script scripts/verify/landing-gear.mjs runs 68 checks, and this page re-checks its results when the site is built.
- Simulator = C: the reference C is compiled with all warnings as errors and fed the 24-step flight and 5,000 random steps; every output line matches the simulator's state machine, and the flight's states match a hand-written expected list.
- Checker: small programs with one known bug each (recursion, scanf into an enum, a switch with a missing break and no default) give exactly the expected findings; the reference gives none; the example program in the tool really does crash with a segmentation fault on empty input.
- Against reality: on the 440 compiled student programs, the checker flagged recursion in 211 of the 213 that crashed.
Frequently asked questions
What are the states of an aircraft landing gear state machine?
The Skill-Lync project uses six: Gear down, Checking before takeoff (a 3 s delay), Raising gear, Gear up, Checking before landing and Lowering gear. It is based on Martin Gomez's 2000 example, which has five (no check before landing) and a 2 s delay. The lights follow the gear: green when down and locked, red while it moves, off when up and locked.
Why does my landing gear C program crash after a while?
Usually because each state function calls itself (or the next state's function) instead of returning. Every call that does not change the state nests one level deeper, so the stack keeps growing. With the input ended, 211 of the 213 compiled student programs that crashed did exactly that, after about half a second. Put the state functions in a loop in main() and let each one return.
What does the squat switch do in a landing gear system?
It senses whether the weight of the aircraft is on the wheels. The control logic uses it to stop the gear from retracting on the ground. Its contact polarity depends on the aircraft: in Gomez's example it closes when weight is on the nose gear; the FAA handbook describes one that is open while the strut is compressed. Name the signal for what it means (weight_on_wheels), not for its contact state.
Why should the gear react to the lever's edge and not its level?
If the pilot leaves the lever UP while parked, a level-triggered machine retracts the gear as soon as the aircraft is airborne. Reacting only to a change from DOWN to UP (a rising edge) means the pilot has to move the lever again in the air. Keep the previous lever value and compare.
Is it wrong to read an enum with scanf("%d")?
Yes, strictly. %d needs a pointer to int, and an enum's underlying type is chosen by the compiler (C11 6.7.2.2: char, a signed or an unsigned integer type). On a desktop GCC or Clang it is usually a 4-byte type, so it seems to work; with GCC's -fshort-enums the enum is 1 byte and scanf overwrites the next variables. Read into an int, check the range, then assign.
What does the checker do with my code?
It reads your C in the browser: it finds the function definitions and who calls whom (recursion), the enum whose constants are assigned as the state, the code for each state (a switch case or a function), and turns every state assignment or handler call into a row of a state table with the condition around it. It also looks at every scanf. It is a pattern reader, not a compiler, so it can miss things in unusual code.
Is this tool free?
Yes. It runs in your browser, needs no sign-up and does not send your code anywhere.
Related on Skill-Lync
- Adaptive cruise control calculator (a Stateflow transition checker)
- I2C calculator and Arduino sketch checker
- Ultrasonic distance and moving average calculator
- Embedded engineer salaries in India
- All free engineering tools
Sources
- Martin Gomez, "Embedded State Machine Implementation", Embedded Systems Programming, December 2000, pp. 40-50: the landing gear example (states, rising edge of the lever, 2 s wait after the squat switch opens, timer restart on a bounce, function-pointer state table).
- FAA, Aviation Maintenance Technician Handbook, Airframe (FAA-H-8083-31B), chapter 13: squat (safety) switch p. 13-22, position indicators "Off = gear up and locked / Red = unsafe / Green = down and locked" p. 13-23, emergency extension p. 13-21; nitrogen back-up in chapter 12.
- eCFR, 14 CFR 25.729 Retracting mechanism: (c) emergency extension after a failure of the normal system or of any single hydraulic or electric supply, (e) position indicator and warnings.
- ISO/IEC 9899:2011 (C11), draft N1570: 6.7.2.2p4 (an enum's compatible type is implementation-defined), 7.21.6.2p10 and p12 (%d needs a pointer to int, otherwise undefined behaviour).
- MISRA C:2012 rule headlines as listed by MathWorks Polyspace: Rule 17.2 functions shall not call themselves, directly or indirectly; Rule 16.4 every switch shall have a default label.
- GCC manual, Code generation options: -fshort-enums allocates to an enum type only as many bytes as it needs.