Video summary

If Red Then Stop - Jon Learns SFC/GRAFCET/OMGHELPIMCONFUSED

Main summary

Key takeaways

Gaming

Storyline

  • The player, “John,” tries to improve coding skills in If Red Then Stop, a game about programming time-dependent processes using realistic state machines (sequential function charts / SFC/GRAFCET).
  • Through tutorial scenarios, the game tasks him with designing control logic for:
    • A level crossing train signal
    • Road works with two traffic directions blocked down to one lane
    • Pedestrian crossings coordinated with vehicle traffic
  • Each scenario is tested under different traffic conditions (high/low traffic; one-sided vs two-sided traffic), with the goal of avoiding crashes and minimizing driver/pedestrian waiting/anger.

Gameplay / Mechanics Highlights

  • State machines (SFC/GRAFCET concepts):

    • Use Steps as states (e.g., light off vs light on; “B40 moving” vs “B41 moving”).
    • Use Transitions to move between steps based on inputs and/or timers.
    • Use jump/loop behavior to return to starting steps.
  • Inputs/Outputs are modeled explicitly:

    • Examples:
      • Train approaching/leaving inputs trigger the level-crossing light output.
      • Road works inputs indicate whether a queue exists on each side; outputs set the two traffic lights.
      • Pedestrian button input and acknowledgements coordinate pedestrian and car phases.
  • Timing is crucial:

    • Timers use milliseconds and are often required to insert:
      • Changeover delays
      • “Queue cleared” waits
      • Minimum green/red durations
  • Debugging is part of the learning:

    • He encounters issues like missed transitions, looping mistakes, and “crashes” when logic/timing is wrong.
    • He repeatedly resets and reruns tests to converge on a working solution.

Key Solutions / Strategies / Tips Used

1) Level Crossing: Train Signal

Goal: Keep the light off by default, turn it on when a train approaches, then return to off after the train passes so traffic can continue.

  • Implemented a simple loop:
    • Start step: light off
    • Transition: when “approach button/input” is active → go to train step
    • Output: light on while train state is active
    • Transition: when “off button/input” indicates train has left → jump back to start

Tip reflected in his approach: don’t brute-force; use state transitions so the system reacts automatically.


2) Road Works: Two-Way Queues Reduced to One Lane

Goal: Alternate which side has green while ensuring neither side waits too long; prevent deadlocks; add timing so only one side goes at once.

  • Base pattern (two-direction alternation):

    • Define steps like B40 moving (green on H40) and B41 moving (green on H41)
    • Use transitions like:
      • If the current side queue condition is cleared → switch to the other side
      • Add timers so switching doesn’t happen immediately when the signal first changes
  • Improvements through iterative tuning:

    • Add extra waiting time after a queue clears before granting green to the other side.
    • Enforce maximum waiting limits so one side doesn’t wait too long under edge cases.
    • Reduce “dead time” (periods where no one is moving) by tuning how long each side remains blocked during transitions.

Testing-driven tuning:

  • Under high traffic, his solution performed well.
  • Under low traffic/one-sided traffic, people waited “for no reason,” so he introduced logic to avoid wasting time when the other side is empty.

3) Pedestrians: Button Press Acknowledgement + Cars Stop/Go

Goal: When pedestrians press a button, stop traffic briefly, let pedestrians cross, and then resume cars—while acknowledging repeated/late button pushes.

Key ideas he implemented:

  • Assume pedestrians keep holding the button until they see acknowledgement.
  • Use outputs to:
    • Keep traffic flowing when no request exists
    • Turn traffic red when a crossing request is recognized
    • Signal pedestrians when their request is acknowledged

Pedestrian Timing Approach

  • Initial blunt timing:
    • When button is detected → start a timer (e.g., ~8 seconds), then:
      • traffic stops briefly for cars to stop
      • pedestrians cross
      • cars remain stopped/then reset to return traffic to green
  • He found it works but doesn’t account for pedestrian volume (it’s basically “finger-in-the-air timing”).

“Counting” Pedestrians via Branching States

  • He couldn’t find a true counting function, so he simulated counting using sequential states triggered by repeated button presses.
  • Branching logic:
    • If enough button pushes happen (or a max wait time elapses) → immediately transition to traffic-stopping / pedestrian crossing
    • Otherwise, continue letting traffic flow

Outcome tradeoff:

  • Works for deciding when pedestrians are “worth” triggering a stop.
  • But with only one pedestrian, it might delay crossing too long (because the “counting” threshold isn’t reached).

Testing Results / Final Adjustments

  • He used the game’s testing tools to evaluate:
    • Crash-free operation
    • Waiting time thresholds
    • Driver satisfaction/anger signals
    • Pedestrian satisfaction/acknowledgement correctness
  • He then tried to balance competing goals:
    • Faster changeovers reduce driver irritation (especially individual drivers).
    • Shorter timings can reduce throughput or increase “dead time” in other traffic patterns.
  • He concluded that fully optimizing for all traffic patterns at once is hard, and settled for a “good enough” solution after many iterations.

Gamers / Sources Featured (as named in the subtitles)

  • John (the streamer/host; “many a true nerds”)
  • Steam page (referenced for the game description/quote)

Original video