Simvik

Traffic light with a fault mode

Write the transition function for a light that cycles, flashes on a fault and resets. An easy embedded C problem (State machines), run and graded in the…

easy · State machines · fsm · switch

Return the next state of a traffic light given its current state and an event:

  • EV_FAULT switches to FLASHING from any state.
  • EV_RESET switches to RED from any state.
  • EV_TIMER cycles RED to GREEN to YELLOW and back to RED, and does nothing while FLASHING.

Starter code

light_t light_next(light_t s, event_t e) {
  switch (s) {
    case RED:    return GREEN;
    case GREEN:  return YELLOW;
    case YELLOW: return RED;
    default:     return s;
  }
}

Example tests

Normal cycle

run(RED, "ttt");

Expected output:

GREEN YELLOW RED

Fault and reset

run(GREEN, "ftr");

Expected output:

FLASHING FLASHING RED

Learn it first

More State machines problems