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. Three LEDs and resistors on the left and right  represent the two traffic lights managing a crossing.

raspiTrafficLight

 

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.setmode(GPIO.BOARD)
# 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.

NORMAL_PROGRAM[0]=[
        [[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
    ]
NORMAL_PROGRAM_PHASE_RED[0]=0
NORMAL_PROGRAM_PHASE_GREEN[0]=len(NORMAL_PROGRAM[0])/2

Semantic:

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.

 

Variables

DEBUG=False                        # threading debug
Thread_CTORDTOR=False

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

Spam Kommentare sind sinnlos !
Vor der Publizierung wird jeder Beitrag auf Spam geprüft. Leider dauert es deswegen bis ein Kommentar sichtbar wird. Dafür erhält aber kein Subscriber Spam eMails.
Die eMail ist optional und nicht öffentlich sichtbar. Sie ist notwendig um eMail Benachrichtigungen zu Antworten auf Kommentare zu erhalten. Sie wird auch u.U. auch vom Seitenbetreiber genutzt um offline Kontakt aufzunehmen.

Spam comments are useless !
Any comment will be reviewed first and checked for spam. Unfortunately this delays the publishing of comments but will protect subscribers from spam.
eMail is optional and hidden and is required to get update notifications for any comments. In addition your eMail may be used by the website owner to get in contact with you offline.