Skip to content
Tactiverge

School/□ Systems Engineer/Week 24

A state machine in software

The same traffic-light controller can be twelve gates and four flip-flops, or twenty lines of code in a loop. When would you choose each?

About 40 min · 3 goals · Checked by automated tests

1.Why should I care?

Most embedded firmware is a handful of state machines running in a loop. Writing them explicitly, with one variable for the state and a table-like switch, is what keeps safety-critical controllers like traffic lights auditable.

  • ◇ Implement an FSM as a super-loop with a switch on the current state.
  • ◇ Use timed states and guard conditions safely.
  • ◇ Compare hardware and software implementations of the same machine.

2.Discover

Open the traffic system at Level 2 and build the junction controller as a cycle of timed states. Before running, write the state table on paper. Then run the safety audit and fix anything it flags.

Can't use the interactive? Read a description instead

The traffic build's Level 2 lets you list timed states (for example north–south green 30 s, amber 3 s, all-red 2 s, east–west green 30 s…) and audits them for conflicts, missing ambers and missing all-red gaps. The same list can be written as a switch statement in a loop.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

Why avoid long blocking delays inside a state machine's loop?

Practice 2

A super-loop pass takes at most 5 ms. What is the worst-case time, in ms, from an event to the start of the pass that acts on it?

ms

4.Apply it

Write pseudocode for a pedestrian crossing with states GREEN_CARS, AMBER, RED_ALL, WALK, FLASHING. Include the button as an event and every timeout. Then list two input sequences you would test and the states each visits.

Open BUILD 005: Build a Traffic Management System →

5.Understand

The super-loop pattern

One variable holds the state. A loop runs forever: read inputs, then switch (state); each case sets outputs, checks its exit conditions and, if one is true, assigns the next state and records the entry time. Nothing blocks, so the loop runs many times per second and every input is noticed promptly.

Timed states and guards

Most real machines leave a state on a timeout or an event. Store entered = now() when entering a state and test now() − entered ≥ duration. Guard conditions (only go to WALK if the button was pressed AND the all-red has finished) are the software version of AND gates in next-state logic.

Hardware or software?

Hardware FSMs react within one clock cycle and keep running if the processor crashes; they are fixed once built. Software FSMs are easy to change and log but react only as fast as the loop and depend on the code being correct. Safety systems often use both: software for normal control, a simple hardware or independent monitor that forces a safe state.

Explain it another way

The state variable is a bookmark. Each time round the loop you open the book at the bookmark, follow that page's instructions, and maybe move the bookmark.

Worked example 1

Sketch the super-loop for a two-state blinker: LED_ON for 200 ms, LED_OFF for 800 ms.

loop: if state == ON: led(1); if now − t0 ≥ 200: state = OFF; t0 = now. elif state == OFF: led(0); if now − t0 ≥ 800: state = ON; t0 = now. No delays, so a button could be read in the same loop.

Common mix-ups

  • A state machine in code needs a special library.
    ✓ A variable, a loop and a switch are all it takes.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

Which is a strength of a hardware FSM over a software one?

Practice 4 · a fresh version every time

A Moore machine outputs 1 for one clock whenever the last 3 input bits were 101 (overlaps allowed). Input, first bit first: 000101110111. How many clocks have output 1?

Practice 5

A junction controller passes a safety audit 'no conflicting greens in any reachable state'. Why check every reachable state, not just a normal cycle?

7.Predict

A software traffic controller is in state GREEN_NS for 30 s, then must go to AMBER_NS. Where should the 30 s timer be checked?

Guess first. Being wrong here is how the surprise works.

8.Reflect

Concepts: Super-loops and finite state machines · Real-time computing on robots