← All exercises

Traffic Light Simple

medium

Drive a one-way traffic light through red, green, and amber phases on a timer.

What it teaches

Move through a fixed sequence of steps — red, then green, then amber, then back to red — each step held for a set time. A 'state machine': the program remembers which step it's on and switches to the next when the timer runs out. The skeleton behind every wash cycle, batch process, and motion routine.

Inputs and outputs

TagTypeDefaultDescription
In_EnableBOOL1
Cfg_RedTimeDINT5000
Cfg_GreenTimeDINT5000
Cfg_AmberTimeDINT2000
Out_RedBOOL
Out_GreenBOOL
Out_AmberBOOL

Required behavior

  • When In_Enable is 0, the sequence is disabled and outputs revert to Out_Red=1, Out_Green=0, Out_Amber=0.
  • When In_Enable is 1, outputs cycle endlessly through Red, Green, and Amber states.
  • Red phase lasts for Cfg_RedTime milliseconds, asserting Out_Red.
  • Green phase lasts for Cfg_GreenTime milliseconds, asserting Out_Green.
  • Amber phase lasts for Cfg_AmberTime milliseconds, asserting Out_Amber.