Introduction
A traffic light sequence controls the lights at intersections to keep cars and people safe by telling them when to stop and go.
PROGRAM TrafficLightSequence VAR RedLight : BOOL := TRUE; YellowLight : BOOL := FALSE; GreenLight : BOOL := FALSE; Timer : TON; State : INT := 0; END_VAR // Timer preset times in milliseconds CONST RedTime : TIME := T#10s; GreenTime : TIME := T#15s; YellowTime : TIME := T#3s; END_CONST // Main program cycle CASE State OF 0: // Red light on RedLight := TRUE; YellowLight := FALSE; GreenLight := FALSE; Timer(IN := TRUE, PT := RedTime); IF Timer.Q THEN Timer(IN := FALSE); State := 1; END_IF 1: // Green light on RedLight := FALSE; YellowLight := FALSE; GreenLight := TRUE; Timer(IN := TRUE, PT := GreenTime); IF Timer.Q THEN Timer(IN := FALSE); State := 2; END_IF 2: // Yellow light on RedLight := FALSE; YellowLight := TRUE; GreenLight := FALSE; Timer(IN := TRUE, PT := YellowTime); IF Timer.Q THEN Timer(IN := FALSE); State := 0; END_IF END_CASE END_PROGRAM
RedLight := TRUE; YellowLight := FALSE; GreenLight := FALSE;
Timer(IN := TRUE, PT := T#10s);
IF Timer.Q THEN
// Timer finished
END_IFCASE State OF 0: // Red light 1: // Green light 2: // Yellow light END_CASE
PROGRAM TrafficLightSequence VAR RedLight : BOOL := TRUE; YellowLight : BOOL := FALSE; GreenLight : BOOL := FALSE; Timer : TON; State : INT := 0; END_VAR CONST RedTime : TIME := T#5s; GreenTime : TIME := T#5s; YellowTime : TIME := T#2s; END_CONST // Main cycle CASE State OF 0: // Red light RedLight := TRUE; YellowLight := FALSE; GreenLight := FALSE; Timer(IN := TRUE, PT := RedTime); IF Timer.Q THEN Timer(IN := FALSE); State := 1; END_IF 1: // Green light RedLight := FALSE; YellowLight := FALSE; GreenLight := TRUE; Timer(IN := TRUE, PT := GreenTime); IF Timer.Q THEN Timer(IN := FALSE); State := 2; END_IF 2: // Yellow light RedLight := FALSE; YellowLight := TRUE; GreenLight := FALSE; Timer(IN := TRUE, PT := YellowTime); IF Timer.Q THEN Timer(IN := FALSE); State := 0; END_IF END_CASE // Output status (simulated print) IF RedLight THEN // print("Red Light ON") ELSIF GreenLight THEN // print("Green Light ON") ELSIF YellowLight THEN // print("Yellow Light ON") END_IF END_PROGRAM
timer = 0 state = 'RED' if timer < 10: light = 'RED' elif timer < 15: light = 'GREEN' else: light = 'YELLOW'
light when timer is 12?state = 'RED' if state == 'RED': state = 'GREEN' elif state == 'GREEN': state = 'YELLOW' elif state == 'YELLOW': state = 'RED'
Pass the test and this pattern moves unknown