Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

To learn to use threading in Python you have to have a concrete problem to solve. Just use Raspberry Pi to connet 6 LEDs and manage them via GIO. On the following pages I describe how to customize my sample program to simulate tarffic lights. You also find a video which shows the program in action and a download link for the sample program.



On the following picture (Click picture to see a video) you see a breadboard an a T-Cobbler. The three LEDs and resistors on the left and right side represent the two traffic lights managing a crossing.



The program has three traffic light programs. First of all all LEDs will flash to test the LEDs. Now the traffic light programs are started. All of them are separated by a traffic fallback program which displays the yellow LEDs. This sequence is executed in an endlessloop. You can see a the program in action on this link.  Download the sample code.

In order to use the sample program you have to update following lines to define the GPIO pins to use.

# GPIO pins for red yellow green
trafficLight1=[18,22,7]         # <=== Adapt to local environment
trafficLight2=[13,15,16]        # <=== Adapt to local environment

First entry is the pin for the red LED. The last entry ist the pin used by the green LED.

Following code defines a normal traffic light program where red/green, red-yellow/green and green/red flashes and can be used as a sample to create your own traffic light programs.

        [[1,0,0],SLEEP_MAIN],        # red
        [[1,1,0],SLEEP_TRANSITION],  # red yellow
        [[0,0,1],SLEEP_MAIN],        # green
        [[0,1,0],SLEEP_TRANSITION]   # yellow


Every traffic light program has to have 3 variables which are defined in a list.

First element in list NORMAL_PROGRAM is a list where the first element defines which LEDs should flash in this state (e.g. [1,0,0]).  [1,0,0] just flashes the red LEDS only. SLEEP_... is a constant which defines how long the LED should flash. It's a factor of the main clock CONDUCTOR_TICK_TIME. The whole traffic light program consists of a sequence of these states. NORMAL_PROGRAM_PHASE_RED and NORMAL_PROGRAM_PHASE_GREEN define the index in the list of the program which represent the red and green traffic light state. e.g. NORMAL_PROGRAM_PHASE_RED=0 because the first line in the program represents the red state of the traffic light whereas NORMAL_PROGRAM_PHASE_GREEN[0]=len(NORMAL_PROGRAM[0])/2, which is 3, represents the green state of the traffic light.



DEBUG=False                        # threading debug

can be set to True gesetzt to enable debug statements which allow to understand and debug the code flow and threading.

The sample program was published under the GPL. The git repository is here.

Add comment

*** Note ***

Comments are welcome. But in order to reject spam posts please consider following rules:
  1. Comments with string http are rejected with message You have no rights to use this tag
  2. All comments are reviewed by hand and thus it usually takes one day until a comment will be published.