User manual
94
LED = [23,24,25,8]
for i in LED:
GPIO.setup(i, GPIO.OUT, initial=False)
The GPIO ports for LEDs are set up as outputs in a list LED following the familiar procedure and all LEDs are
set to off.
TAST = [4,17,21,22]
for i in TAST:
GPIO.setup(i, GPIO.IN)
Now following the same principle, the GPIO ports for the four buttons are set up in a list TAST as inputs.
We have created the foundation, and will now define two more functions that are needed several times in
the programme.
def LEDOn(n, z):
GPIO.output(LED[n], True); time.sleep(z)
GPIO.output(LED[n], False); time.sleep(0.15)
The function LEDOn() turns on an LED for a certain period of time. The function uses two parameters: The
first parameter,
n specifies the number of the LED between 0 and 3, the second parameter, z, specifies the
time during which the LED will glow. After the LED has been turned off again, the function waits for 0.15
seconds until it is terminates in order to facilitate short breaks between the illumination of the LEDs upon
multiple calls. This is particularly important if a LED is repeatedly illuminated. Otherwise it would not get
noticed.
def Push():
while True:
if(GPIO.input(TAST[0])):
return 0
if(GPIO.input(TAST[1])):
return 1
if(GPIO.input(TAST[2])):
return 2
if(GPIO.input(TAST[3])):
return 3
The function Druecken() consists of a continuous loop, which waits for the user to push one of the buttons.
Then the number of the button is returned to the main programme.
ok = True Following the definition of the functions the actual main programme starts and as a first task it
sets a variable
ok to True. As soon as the player makes a mistake, ok is set to False. If the variable after the
designated number of rounds is still
True, the player has won.
for round in range(1, rnumberl +1):
The game runs defined by the variable rnumber over a fixed number of rounds. The round counter is shifted
by 1 upwards so the game starts with Round 1 and not with Round 0.
print “Round", round