User manual
52
Pattern 4
If the user has entered 4, a different flashing pattern starts, in which all LEDs are flashing simultaneously and
are not called up in sequence.
elif e == "4":
for i in range(w):
for j in range(z):
GPIO.output(LED[j], True)
time.sleep(2*t)
for j in range(z):
GPIO.output(LED[j], False)
time.sleep(t)
Not all GPIO ports can be switched on or off at once with a single statement, that is why also here loops are
used, however, without a time lag within the loop. The four LEDs are turned on instantly one after the other.
For the human eye, it seems to happen simultaneously. At the end of the first inner loop the program waits
for the lapse of the doubled delay time before all LEDs are turned off again.
The flashing lights generate different effects of light and dark through the use of different time intervals.
Flashes are noticed more when the flash duration is longer than the dark period. Very short flashes with a
relatively long dark period produce a photoflash effect.
Pattern 5
If the user has entered 5, the LEDs flash totally at random.
elif e == "5":
for i in range(w*z):
j = random.randint(0,z-1)
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)
No nested loops are used here, so we have the loop run through more often. An LED will blink roughly as
often as in the first pattern, when the variables
w and z are multiplied.
The function
random.randint() writes a random number in the variable j. This random number is greater
than or equal to the first parameter and less or equal to the second parameter. In our case it can have the
values
0, 1, 2, 3.
The randomly selected LED is turned on and then after lapse of the delay time turns off again. After that, the
loop restarts and a new LED is selected at random.
Invalid entry
Programs that require an user input must be able to intercept incorrect entries. If the user enters something
that was not intended, the programme must respond.
else:
print (“Invalid entry")