-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenius.py
161 lines (139 loc) · 3.57 KB
/
genius.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
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
from time import sleep
from RPi import GPIO
import random
GPIO.setmode(GPIO.BCM)
# GPIO.setwarnings(False)
### Constants ###
LED1 = 6
LED2 = 11
LED3 = 10
LED4 = 17
LED_LIST = [LED1,LED2,LED3,LED4]
BUTTON1 = 12
BUTTON2 = 8
BUTTON3 = 24
BUTTON4 = 18
N = 2
MICRO_TIME = .1
HALF_TIME = .5
### Variables ###
selectedColor = None
colors = []
### GPIO Setup ###
GPIO.setup(BUTTON1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BUTTON4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)
GPIO.setup(LED4, GPIO.OUT)
GPIO.setup(N, GPIO.OUT)
# Check all outputs and start off
GPIO.output(N,1)
sleep(MICRO_TIME)
GPIO.output(LED1,1)
sleep(MICRO_TIME)
GPIO.output(LED2,1)
sleep(MICRO_TIME)
GPIO.output(LED3,1)
sleep(MICRO_TIME)
GPIO.output(LED4,1)
sleep(HALF_TIME)
GPIO.output(LED4,0)
sleep(MICRO_TIME)
GPIO.output(LED3,0)
sleep(MICRO_TIME)
GPIO.output(LED2,0)
sleep(MICRO_TIME)
GPIO.output(LED1,0)
sleep(MICRO_TIME)
GPIO.output(N,0)
### Functions ###
# gets the color pressed by the user
def getColor():
while True:
if(GPIO.input(BUTTON1) == 0):
return LED1
elif(GPIO.input(BUTTON2) == 0):
return LED2
elif(GPIO.input(BUTTON3) == 0):
return LED3
elif(GPIO.input(BUTTON4) == 0):
return LED4
# plays the note for a given time
def playNote(t):
GPIO.output(N,1)
sleep(t)
GPIO.output(N,0)
# lights a given LED for a given time
def lightLED(led,t):
GPIO.output(led,1)
sleep(t)
GPIO.output(led,0)
# plays note and lights LED
def playColor(led,t):
GPIO.output(led,1)
GPIO.output(N,1)
sleep(t)
GPIO.output(N,0)
GPIO.output(led,0)
# gets a random color/LED
def getRandColor():
randomInt = random.randint(1,4)
if(randomInt==1):
return LED1
elif(randomInt==2):
return LED2
elif(randomInt==3):
return LED3
else:
return LED4
# plays the sequence for the user
def playSequence():
for led in colors:
playColor(led,0.3)
sleep(0.3)
# adds new random color to the colors array
def addColor():
randomColor = getRandColor()
colors.append(randomColor)
# terminates the game in an amazing way
def playEndGame():
print ("\n\n*** THE USER HAS LOST! ***\n\n")
sleep(0.1)
GPIO.output(LED_LIST,1)
for i in range(0,7):
playNote(0.15)
sleep(0.15)
GPIO.output(LED_LIST,0)
GPIO.cleanup() # cleans up the "mess"
exit()
# gets all the sequence, dealing with failure
def getColorSequence():
selectedColor = None
for color in colors:
selectedColor = getColor()
playColor(selectedColor,0.3)
if(selectedColor != color):
playEndGame()
# Buzz sound warning the program started
def beepToStart():
for i in range(0,5):
playNote(0.15)
sleep(0.15)
sleep(1)
# sleep and beep before starts
sleep(1)
beepToStart()
# Run
try:
while True:
addColor()
playSequence()
getColorSequence()
sleep(.5)
except KeyboardInterrupt:
playNote(0.5)
GPIO.cleanup()
print ("\nProgram is over!")