Traffic Light Controller

Let us all intone the mantra - " look at the blink without delay example ". Ommmmm.

Ommmmm.

OK, now that we have that out of the way, toss the Arduino in the corner for a while. YOU are going to take its place. You have a bunch of switches in front of you, a stopwatch, with the stop button broken off, and a pad of paper. How would YOU cycle the lights in the proper order?

The Arduino knows how to toggles pins, turning the lights on and off. The millis() function acts like the broken stopwatch, incrementing continuously, until it rolls around after 49+ days. The variables that the Arduino reads from/writes to take the place of paper.

The code that you write replaces you.

When YOU can define the steps you follow, you have a set of requirements that is easy to translate into code.

If you get stuck, think in terms of a state machine. There are a number of states - "Light is red", "Light is yellow", "Light is red", "Traffic waiting", "Pedestrian waiting", etc.

There are things that happen when a transition from one state to another occurs. Changing from "Light is yellow" to "Light is red" should cause certain (obvious) things to happen.

When to perform each transition is typically defined in terms of time (the light stays green for n seconds) or external activity (a pedestrian pushed a switch, a car triggered a sensor, etc.).

Having the Arduino implement that state machine is pretty easy, once you know all the possible states, which states can transition to other states (yellow to red is allowed; green to red is not), when to perform the transition, and what to do when the transition occurs.