Skip to content

Extended Traffic Light

An extended traffic light state machine that adds pedestrian crossing functionality, demonstrating how to model more complex systems with additional states and transitions.

This example builds on the basic Traffic Light example by adding:

  • A new state for pedestrian crossing (RedWithPedestrian)
  • Additional state data for pedestrian signals
  • A pedestrian button transition to request crossing
  • Visual indication of pedestrian crossing status
  • Special states for emergencies (flashing yellow) and malfunctions (flashing red)

The extended machine shows how to model a system that responds to external input (pedestrian button) and has different behaviors based on its current state.

import { defineStates } from "matchina";
const pedestrianStates = defineStates({
Walk: undefined,
DontWalk: undefined,
Error: undefined,
});
export interface CommonStateProps {
key: string;
crossingRequested?: boolean;
walkWarningDuration?: number;
}
export const sharedStates = defineStates({
State: (
props: CommonStateProps = { key: "State", crossingRequested: false }
) => props,
});
export const states = defineStates({
Green: () => ({
message: "Go",
duration: 4000, // 4 seconds
pedestrian: pedestrianStates.Walk(),
}),
Yellow: () => ({
message: "Prepare to stop",
duration: 2000, // 2 seconds
pedestrian: pedestrianStates.Walk(),
}),
Red: () => ({
message: "Stop",
duration: 4000, // 4 seconds
pedestrian: pedestrianStates.DontWalk(),
}),
RedWithPedestrianRequest: () => ({
message: "Stop with pedestrian requesting crossing",
duration: 2000, // 2 seconds
pedestrian: pedestrianStates.DontWalk(),
}),
FlashingYellow: () => ({
message: "Proceed with caution",
duration: 0, // No automatic transition
pedestrian: pedestrianStates.DontWalk(),
isFlashing: true,
}),
FlashingRed: () => ({
message: "Stop and proceed when safe",
duration: 0, // No automatic transition
pedestrian: pedestrianStates.Error(),
isFlashing: true,
}),
Broken: () => ({
message: "Broken (flashing red)",
duration: 0,
pedestrian: pedestrianStates.Error(),
}),
});

The traffic light normally cycles through Green → Yellow → Red → Green, but when a pedestrian presses the button:

  1. If the light is already Red, it transitions to RedWithPedestrian to allow crossing
  2. If the light is Green or Yellow, it continues its normal cycle until reaching Red
  3. From RedWithPedestrian, the light returns to Green after pedestrians have crossed

Additionally, the light can enter special states:

  • Emergency Mode (Flashing Yellow): Indicates caution, proceed if safe
  • Malfunction Mode (Flashing Red): Treat as a stop sign, stop and then proceed when safe

These special states demonstrate how state machines can model exceptional conditions and transitions between normal and exceptional operation.

This pattern demonstrates how state machines can handle external inputs differently based on their current state, providing a clean way to model complex interactive systems.

Now that you’ve seen a more complex state machine example, you might want to explore: