-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimon_gpio.py
More file actions
164 lines (142 loc) · 3.59 KB
/
simon_gpio.py
File metadata and controls
164 lines (142 loc) · 3.59 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import random
import time
import RPi.GPIO as GPIO
# buttons
start_button = 5
green_button = 6
red_button = 13
yellow_button = 19
blue_button = 26
# LEDs
green_led = 25
red_led = 8
yellow_led = 7
blue_led = 1
def read_button():
while (not (GPIO.input(green_button) or GPIO.input(red_button) or GPIO.input(yellow_button) or GPIO.input(blue_button))):
pass
if GPIO.input(green_button):
return 1
if GPIO.input(red_button):
return 2
if GPIO.input(yellow_button):
return 3
if GPIO.input(blue_button):
return 4
def leds_off():
GPIO.output(green_led, GPIO.LOW)
GPIO.output(red_led, GPIO.LOW)
GPIO.output(yellow_led, GPIO.LOW)
GPIO.output(blue_led, GPIO.LOW)
def sort_led(value):
if (value == 1):
GPIO.output(green_led, GPIO.HIGH)
return value
if (value == 2):
GPIO.output(red_led, GPIO.HIGH)
return value
if (value == 3):
GPIO.output(yellow_led, GPIO.HIGH)
return value
if (value == 4):
GPIO.output(blue_led, GPIO.HIGH)
return value
def blink_game_start():
for i in range(5):
GPIO.output(green_led, GPIO.HIGH)
GPIO.output(red_led, GPIO.HIGH)
GPIO.output(yellow_led, GPIO.HIGH)
GPIO.output(blue_led, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(green_led, GPIO.LOW)
GPIO.output(red_led, GPIO.LOW)
GPIO.output(yellow_led, GPIO.LOW)
GPIO.output(blue_led, GPIO.LOW)
time.sleep(0.2)
leds_off()
time.sleep(0.5)
def blink_next_stage():
leds_off()
for i in range(5):
GPIO.output(green_led, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(green_led, GPIO.LOW)
GPIO.output(red_led, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(red_led, GPIO.LOW)
GPIO.output(yellow_led, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(yellow_led, GPIO.LOW)
GPIO.output(blue_led, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(blue_led, GPIO.LOW)
leds_off()
time.sleep(0.5)
def setup():
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
# Buttons setup as INPUT with pull-down resistors
GPIO.setup(start_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(green_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(red_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(yellow_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(blue_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# LEDs setup as OUTPUT
GPIO.setup(green_led, GPIO.OUT)
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(yellow_led, GPIO.OUT)
GPIO.setup(blue_led, GPIO.OUT)
leds_off()
def main():
setup()
while(True):
blinks = 1
to_press = []
finish = False
# Waits for start button to be pressed and released
while(not GPIO.input(start_button)):
pass
while(GPIO.input(start_button)):
pass
blink_game_start()
while(finish == False):
# If the start button is pressed at any time, resets the game
if(GPIO.input(start_button)):
finish = True
blink_game_start()
break
for i in range(blinks):
to_press.append(sort_led(random.randint(1,4)))
time.sleep(0.5)
leds_off()
time.sleep(0.5)
j = 0
for i in range(blinks):
# If the start button is pressed at any time, resets the game
if(GPIO.input(start_button)):
finish = True
blink_game_start()
break
pressed = read_button()
print('to_press:')
print(to_press)
print('pressed:')
print(pressed)
if (pressed != to_press[j]):
finish = True
blink_game_start()
break
else:
j += 1
while (GPIO.input(green_button) or GPIO.input(red_button) or GPIO.input(yellow_button) or GPIO.input(blue_button)):
pass
else:
blink_next_stage()
to_press = []
blinks += 1
if __name__ == '__main__':
try:
main()
except:
print("Program aborted")
finally:
GPIO.cleanup()