Traffic light cycle with a once-in-a-lifetime startup RED
Write a PLC program that controls the traffic lights for one direction according to a fixed sequence (RED → GREEN → YELLOW → RED). Provide an initial safety feature: when the signal system is powered up, it must always begin from RED — but only once over the entire lifetime of the code (the special initial RED runs only on the very first power-up). On that first power-up the initial RED phase must last 15 seconds and must run exactly once; thereafter the normal cycle repeats indefinitely.
ShowHide worked solution
Strategy
The phrase “once over the entire lifetime of the code” is the key — a normal first-scan bit (S:1/15) fires every time the PLC is powered up, which would re-run the 15-second RED on every restart. The fix is a retentive bit FIRST_RUN_DONE stored in non-volatile memory: on power-up the rung checks “is FIRST_RUN_DONE still zero?” — if yes, run the startup RED and then latch the bit forever; if no, skip straight to the normal cycle. The normal cycle is a three-state machine (RED 30 s → GREEN 25 s → YELLOW 5 s).
Rung 1 — special first-run 15-s RED, only when FIRST_RUN_DONE = 0: |---[ S:1/15 ]---[/FIRST_RUN_DONE]----+----( STARTUP_RED )----| | | |---[ STARTUP_RED ]---[/T_INIT.DN]----+ Rung 2 — 15-second initial timer: |---[ STARTUP_RED ]----(TON T_INIT, PRE = 15 s)----| Rung 3 — mark first-run as done, retentive latch: |---[ T_INIT.DN ]----(L FIRST_RUN_DONE)----| Rung 4 — normal sequence runs after STARTUP_RED drops: |---[ FIRST_RUN_DONE ]----( NORMAL )----| Rungs 5–7 — cyclic state machine (30 s RED → 25 s GREEN → 5 s YELLOW): |---[ NORMAL ]---[/T_R.DN]----( RED_PHASE )-----| |---[ NORMAL ]---[ T_R.DN ]---[/T_G.DN]----( GREEN_PHASE )---| |---[ NORMAL ]---[ T_G.DN ]---[/T_Y.DN]----( YELLOW_PHASE )--| Rung 8 — phase timers: |---[ RED_PHASE ]----(TON T_R, PRE = 30 s)----| |---[ GREEN_PHASE ]----(TON T_G, PRE = 25 s)----| |---[ YELLOW_PHASE ]----(TON T_Y, PRE = 5 s)----| Rung 9 — cycle restart at the end of YELLOW: |---[ T_Y.DN ]----(RES T_R)----| |---[ T_Y.DN ]----(RES T_G)----| |---[ T_Y.DN ]----(RES T_Y)----| Rung 10 — physical lights: |---[ STARTUP_RED ]----+----( RED )-----| |---[ RED_PHASE ]----+ |---[ GREEN_PHASE ]------( GREEN )-----| |---[ YELLOW_PHASE ]------( YELLOW )----|
FIRST_RUN_DONE is a retentive bit — it survives power cycles, so the startup RED never repeats.
Timing diagram (after first power-up)
On any subsequent power-up FIRST_RUN_DONE is already 1, so the startup row is bypassed entirely and the lights pick up at the start of the normal RED phase.