User manual

84
]
root = Tk(); root.title("LED"); v = IntVar(); v.set(1)
def LedOn():
e = v.get()
if e == 1:
for i in range(w):
for j in range(4):
GPIO.output(LED[j], True)
time.sleep(t)
GPIO.output(LED[j], False)
elif e == 2:
for i in range(w):
for j in range(4):
GPIO.output(LED[j], True)
time.sleep(t)
for j in range(4):
GPIO.output(LED[j], False)
time.sleep(t)
else:
for i in range(w):
for j in range(4):
GPIO.output(LED[3-j], True); time.sleep(t)
GPIO.output(LED[3-j], False)
Label(root,
text=”Please click a button to start the chaser
").pack()
for txt, m in pattern:
Radiobutton(root, text = txt,
variable = v, value = m).pack(anchor=W)
Button(root, text="Start", command=LedOn).pack(side=LEFT)
root.mainloop()
GPIO.cleanup()
10.2.1 How does it work?
To start with, we will first import libraries. In addition to the last program, the time library is also required
and needed for the waiting times for the LED flashing effects.
GPIO.setmode(GPIO.BCM); LED = [4,18,23,24]
for i in LED:
GPIO.setup(i, GPIO.OUT, initial=0)
A list for the four LEDs is then defined. The corresponding GPIO ports are defined as outputs and set to 0, so
that all LEDs are turned initially off.
w = 5; t = 0.2
Two variables define two values in the programme: the number of repetitions w of a pattern w and the
flashing time
t. Both values could also be entered as they happen to occur in the programme. However, as
they are defined in only one place, they are much more easily adjusted that way.
pattern = [
("Chasing to the left",1), ("Flashing",2), ("Chasing to the right”,3)
]