-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton6.py
88 lines (58 loc) · 1.55 KB
/
button6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import RPi.GPIO as GPIO
import random
import time
button_to_light_dict={
25: { "channel": 18 },
24: { "channel": 23 },
16: { "channel": 12 }
}
index=0
sequence=[25, 24, 16]
def all_lights(value):
GPIO.output(18, value)
GPIO.output(23, value)
GPIO.output(12, value)
def button_pressed(channel):
global button_to_light_dict
global index
global sequence
expected=sequence[index]
if channel == expected and index == len(sequence) - 1:
print("you win!")
for step in range(10):
all_lights(True)
time.sleep(0.2)
all_lights(False)
time.sleep(0.2)
print("play again?")
setup_game()
elif channel == expected:
GPIO.output(button_to_light_dict[channel]["channel"], True)
index += 1
else:
print("wrong sequence! try again")
setup_game()
def setup_game():
global index
global sequence
index=0
random.shuffle(sequence)
all_lights(True)
time.sleep(0.5)
all_lights(False)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# buttons
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(25, GPIO.RISING, callback=button_pressed, bouncetime=500)
GPIO.add_event_detect(24, GPIO.RISING, callback=button_pressed, bouncetime=500)
GPIO.add_event_detect(16, GPIO.RISING, callback=button_pressed, bouncetime=500)
# lights
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
setup_game()
input("press enter to quit\n\n")
GPIO.cleanup()