The basic capabilities for controlling the color of the LED’s can be used to simulate a traffic light. The colors green, yellow, and red can be defined for the simulation. Yellow is a mix of red and green.

Defining the Colors Green, Yellow, and Red

Once the functions to produce these three colors have been defined, the traffic light simulation can be written in the following way:

  1. Turn on the green light.
  2. Wait a second.
  3. Turn on the yellow light.
  4. Wait a second.
  5. Turn on the red light.
  6. Wait a second.

The function defined in Figure 28 cycles through green, yellow, and red on a single LED (i.e., LED 1).

Turning on Green, Yellow, and Red Lights

With minor adjustments, LED 1 can be defined as the green light, LED 2 can be defined as the yellow light, and LED 3 can be defined as the red light.

Turning on Three Separate LED Lights Sequentially

This adjustment turns on all three LEDs simultaneously.

An actual traffic light would turn off the green light before turning on the yellow light, etc. To incorporate this behavior into the simulation, a function to turn off each light is needed. This can be accomplished by setting the power levels of the red, green, and blue LEDs to 0. A quirk of the WS2812B LED strip is that the LED must be briefly turned on immediately before setting the power levels to 0 in order to turn off the light. The function Turn_of_LED is shown below.

A Function to Turn Off an LED

The revised procedure turns off each light before turning on the next light.

  1. Turn on the green light (LED 1).
  2. Wait a second.
  3. Turn off the green light.
    —————————————–
  4. Turn on the yellow light (LED 2).
  5. Wait a second.
  6. Turn off the yellow light.
    —————————————–
  7. Turn on the red light (LED 3).
  8. Wait a second.
  9. Turn off the red light
The Updated Traffic Light Procedure

This process can repeated an infinite number of times by placing it in a Forever loop.

A Continuously Cycling Traffic Light Program

This final version emulates an actual traffic light for use in a diorama or model train layout.