Traffic Light Controller with Pedestrian Request

Medium · Counters & Timers · Finite State Machines

Design the controller for a single traffic light. The light cycles RED → GREEN → YELLOW → RED → ..., each color lasting a fixed number of clock cycles. A pedestrian push-button can cut the GREEN phase short, but never below a minimum green time.

Parameters

ParameterDefaultMeaning
G_CYCLES5Normal length of GREEN, in clock cycles.
Y_CYCLES2Length of YELLOW.
R_CYCLES4Length of RED.
MIN_GREEN2Minimum length of GREEN when a pedestrian request shortens it.

You may assume 1 <= MIN_GREEN <= G_CYCLES and all lengths are between 1 and 255. Your design must work for any such values: the hidden tests also instantiate it with other legal values, including the smallest and the largest. The examples below use the defaults.

Ports

PortDirWidthMeaning
clkinput1Clock.
rstinput1Synchronous, active-high reset (takes effect only at a rising edge of clk).
ped_reqinput1Pedestrian request (level), sampled at each rising edge.
red, yellow, greenoutput1 eachThe lamps. Exactly one is 1 in every cycle (one-hot).

The outputs depend only on the controller's state registers (Moore outputs, no combinational path from ped_req): they change only right after a rising edge of clk.

Behavior

Define the age of the current phase as the number of clock cycles the current color has been shown, including the current cycle (the first cycle of a phase has age 1).

At each rising edge with rst = 1: the light becomes RED with age 1 (the cycle right after the reset edge is the first RED cycle). While rst is held, the light stays RED with age 1.

At each rising edge with rst = 0, given the current color and age k:

ColorMoves to the next color at this edge when...Next color
REDk == R_CYCLESGREEN
GREENk == G_CYCLES, or ped_req == 1 && k >= MIN_GREENYELLOW
YELLOWk == Y_CYCLESRED

Otherwise the color stays and the age increases by one. So without pedestrian requests, RED lasts exactly R_CYCLES cycles, GREEN G_CYCLES, YELLOW Y_CYCLES.

Pedestrian request details:

YELLOW.

MIN_GREEN has no effect (unless ped_req is still 1 at a later edge).

Example (default parameters)

Cycle 0 is the cycle in which rst = 1; the reset edge closes it. Cycle n is the n-th cycle after the reset edge.

ScenarioREDGREENYELLOWREDGREEN
No requests1–45–910–1112–1516–
ped_req = 1 during cycle 6 only (GREEN age 2 = MIN_GREEN)1–45–67–89–1213–
ped_req = 1 during cycle 5 only (GREEN age 1 < MIN_GREEN): ignored1–45–910–1112–1516–
ped_req = 1 held from cycle 1 onwards1–45–67–89–1213–14 ...

Sign up free to solve Traffic Light Controller with Pedestrian Request in your browser.

More Counters & Timers problems