User manual
38
time.sleep(2)
GPIO.output(light[green],False); GPIO.output(light[yelllow],True)
time.sleep(0.6)
GPIO.output(light[yellow],False); GPIO.output(light[red],True)
time.sleep(0.6)
for i in range(10):
GPIO.output(light[blue],True); time.sleep(0.05)
GPIO.output(light[blue],False); time.sleep(0.05)
time.sleep(0.6)
GPIO.output(light[yellow],True); time.sleep(0.6)
GPIO.output(light[red],False)
GPIO.output(light[yellow],False)
GPIO.output(lightl[green],True)
except KeyboardInterrupt:
GPIO.cleanup()
4.1.1 How does it work?
The programme sequence is pretty much known. During the red phase, now slightly prolonged, the blue
pedestrian light should blink rapidly.
blue = 4 A new variable in the list defines the LED for the pedestrian lights.
Light=[4,18,23,24] The list will add four elements in order to control the four LEDs.
GPIO.setup(light[blue], GPIO.OUT, initial=False) The new LED is initialized and at first switched
off. This is the default setting during the traffic lights’ green phase.
time.sleep(0.6)
for i in range(10):
GPIO.output(light[blue],True); time.sleep(0.05)
GPIO.output(light[blue],False); time.sleep(0.05)
time.sleep(0.6)
The traffic light‘s cycle starts a loop during which the blue LED will flash, 0.6 seconds after the start of the
red phase. To realize this, we use a
for loop here, which contrary to the while loops used in the previous
experiments always uses a certain number of loop runs until a certain termination condition is met.
for i in range(10): Each for loop needs a loop counter, that is, a variable, that adopts a new value with
each loop run. All programming languages have accepted the variable name
i for simple loop counters.
Optionally, any other name is also possible, of course. This value can be queried like any other variable
within the loop, but in our case this is not necessary. The parameter
range in the loop indicates how many
times the loop runs through, or more precisely, the values of the loop counter. In our example, the loop runs
ten times. The loop counter
i will have a value from 0 to 9. Within the loop cycle the new blue LED is
switched on and then off after 0.05 seconds. More 0.05 seconds and a loop run comes to an end; the next
one starts again when the LED is switched on. In this manner it flashes ten times; overall it lasts for a total of
one second.
time.sleep(0.6) The normal switching cycle of the traffic light is continued with a delay of 0.6 seconds
after the last loop run by switching on the yellow LED in addition to the already lit red LED. Nothing new