User manual

50
The program waits until the user has typed a character and has pressed the
[Enter] key. Depending on the
number the user has entered, the LEDs should now perform according to certain pattern. We will query this
with the use of the construct
if...elif...else.
Pattern 1
If the entry was 1, the indented part of the programme that comes after this line is executed.
if e == "1": Note that indents in Python are not used as optical effects. As we do with loops, we also start
those queries with an indent.
Equal is not equal equal
Python uses two types of the equal sign. The simple = is used to assign a specific value to a variable. The
double equal sign == is used in queries, and verifies whether two values are really the same.
If the user has entered 1 on the keyboard, a loop starts which produces a cyclical chaser light. These loops
basically have for all LED patterns used the same structure.
for i in range(w): The outer loop repeats the pattern as many times as is indicated for the above-defined
variable
w. Within this loop lies another, which generates the respective pattern. This differs with each
pattern.
for j in range(z):
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)
In the case of the simple cyclical chaser this loop runs one after the other through each LED in the list once.
The number of the LEDs has been saved to the variable
zat the beginning of the program. The LED with the
number of the current state of the loop counter is turned on. The program waits now for the time frame that
was initially stored in the variable
t and then turns the LED off. The next loop run starts with the next LED.
The outer loop repeats the entire inner loop five times.
Pattern 2
A similar loop is started when the user enters the input 2. The LEDs here are not only counted in one
direction, instead at the end of the chase the counting order is reversed. The light alternates backward and
forward.
elif e == "2": After the first query any queries that follow are using the query stringelif, , which means
that they will only be executed if the previous query has returned as a
Falseresult.
for i in range(w):
for j in range(z):
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)
for j in range(z-1, -1, -1):
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)