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

Gear lever
Aircraft
Hydraulic pressure

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.

StateGear downwaiting for the lever
Valve · pumpNONE · offtimer –
Geardownweight on wheels
Gear downChecking beforetakeoffRaising gearGear upChecking beforelandingLowering gear
Gear lightsNoseLeftRightgreen: down and locked
  1. Gear down. Move the lever, take off, or pick a scenario.

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.

StateLeaves whenLights · valve
Gear downThe lever moves from DOWN to UP (a rising edge) and the aircraft is airborne → Checking before takeoff.Green · none
Checking before takeoffWeight back on the wheels, or lever down → Gear down. Airborne for the whole 3 s with pressure → Raising gear.Green · none
Raising gearAll three up-locks made → Gear up. Lever down → Checking before landing.Red · UP, pump on
Gear upLever down → Checking before landing.Off · none
Checking before landingPressure available → Lowering gear with the pump; none → Lowering gear on the gas springs, squat switch ignored.Red · none
Lowering gearAll 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 happensInputsState afterLights
1Lever moved up while parked1 1 0 3 1 0.5Gear downgreen
2Takes off with the lever still up1 0 0 3 1 0.5Gear downgreen
3Pilot moves the lever down0 0 0 3 1 0.5Gear downgreen
4Lever up in the air1 0 0 3 1 0.5Checking before takeoff (3.0 s left)green
5Bounce: wheels touch the runway1 1 0 3 1 0.5Gear downgreen
6Still on the runway1 1 0 3 1 0.5Gear downgreen
7Airborne again, lever still up1 0 0 3 1 0.5Checking before takeoff (3.0 s left)green
81 s airborne1 0 0 3 1 1Checking before takeoff (2.0 s left)green
92 s airborne1 0 0 3 1 1Checking before takeoff (1.0 s left)green
103 s airborne1 0 0 3 1 1Raising gearred
11Gear travelling, one up-lock made1 0 1 0 1 2Raising gearred
12All three up-locks made1 0 3 0 1 2Gear upoff
13Approach: lever down0 0 3 0 1 60Checking before landingred
14Check before landing0 0 3 0 1 0.1Lowering gearred
15Gear travelling down0 0 0 1 1 2Lowering gearred
16Go-around: lever up while lowering1 0 0 1 1 0.5Checking before takeoff (3.0 s left)green
171.5 s1 0 0 1 1 1.5Checking before takeoff (1.5 s left)green
183 s: retract again1 0 0 1 1 1.5Raising gearred
19Up and locked1 0 3 0 1 2Gear upoff
20Hydraulics fail, lever down0 0 3 0 0 30Checking before landingred
21Check before landing: no pressure0 0 3 0 0 0.1Lowering gear, springsred
22Lever up during spring extension: ignored1 0 0 2 0 1Lowering gear, springsred
23All three down-locks made0 0 0 3 0 1Gear downgreen
24Landed0 1 0 3 0 60Gear downgreen

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).

FindingProgramsWhy it matters
Crashed when the input ended (segmentation fault)213 of 440211 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 ended153 of 440scanf 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 input74 of 440Some of these simply exit after one pass.
A state function calls itself or another state function470MISRA C:2012 Rule 17.2 forbids direct and indirect recursion; here the stack use has no bound at all.
scanf result never checked598The cause of the endless loops above.
scanf("%d") straight into an enum variable452Undefined 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 failed193A branch for "hydraulics not working" still goes to Raising gear. Gas springs or free fall can only extend the gear.
No timer or delay anywhere715The brief's 3 s check before takeoff becomes a single pass.
No previous lever value (no edge detection)730The parked-lever case retracts the gear after takeoff.
No hydraulic-failure path at all376No 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

  1. 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.
  2. 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.
  3. 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.
Learn embedded C

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

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.
Free counselling

From a state machine project to embedded jobs.

Embedded C, MISRA rules and testing against requirements are what embedded and automotive software interviews ask about. A Skill-Lync counsellor can map the program that fits the role you want.

Talk to a course counsellor

Free. We call you back, usually the same day.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.