-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·225 lines (169 loc) · 7.27 KB
/
test.py
File metadata and controls
executable file
·225 lines (169 loc) · 7.27 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import pygame, time
import RPi.GPIO as GPIO
# # re-map the bins on the board using bcm mode
GPIO.setmode(GPIO.BCM)
lightningPin = 17
#
# # set up each channel as an input or output
GPIO.setup(lightningPin, GPIO.OUT, initial=GPIO.LOW)
# define the strike objects that specify the sound file and light pattern
pygame.init()
# Duration is measured in seconds, where .1 = one tenth of one second.
def pause(duration):
GPIO.output(lightningPin, GPIO.LOW)
time.sleep(duration)
def flash(duration):
GPIO.output(lightningPin, GPIO.HIGH)
time.sleep(duration)
GPIO.output(lightningPin, GPIO.LOW)
def pulse(duration, length):
pulseCount = duration / length
i = 0
while i < pulseCount:
halfLength = length * .5
GPIO.output(lightningPin, GPIO.HIGH)
time.sleep(halfLength)
GPIO.output(lightningPin, GPIO.LOW)
time.sleep(halfLength)
i += 1
strikes = [
{
'description' : "Steady rumble with a few sort of peaks.",
'thunderFile' : "thunder-01.mp3",
'lightningPattern' : [
# 1
{ 'action' : "pause", 'duration' : .1 },
{ 'action' : "flash", 'duration' : .3 },
{ 'action' : "crackle", 'duration' : .4 },
{ 'action' : "flicker", 'duration' : .2 },
# 2
{ 'action' : "flicker", 'duration' : .2 },
{ 'action' : "flash", 'duration' : .3 },
{ 'action' : "crackle", 'duration' : .2 },
{ 'action' : "flicker", 'duration' : .3 },
# 3
{ 'action' : "flash", 'duration' : .2 },
{ 'action' : "crackle", 'duration' : .2 },
{ 'action' : "flicker", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .3 },
# 4
{ 'action' : "flicker", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .3 },
{ 'action' : "flicker", 'duration' : .4 },
# 5
{ 'action' : "flash", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .4 },
{ 'action' : "flicker", 'duration' : .3 },
# 6
{ 'action' : "pause", 'duration' : .5 },
{ 'action' : "flicker", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .2 },
# 7
{ 'action' : "flicker", 'duration' : .2 },
{ 'action' : "flash", 'duration' : .4 },
{ 'action' : "pause", 'duration' : .1 },
{ 'action' : "flicker", 'duration' : .2 },
{ 'action' : "pause", 'duration' : .1 },
# 8
{ 'action' : "crackle", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .2 },
{ 'action' : "flicker", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .2 },
# 9
{ 'action' : "flicker", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .5 },
{ 'action' : "flicker", 'duration' : .1 },
# 10
{ 'action' : "pause", 'duration' : .1 }
]
},
{
'description' : "Starts with a boom and then flickers and fades.",
'thunderFile' : "thunder-02.mp3",
'lightningPattern' : [
# 1, 2, & 3 Hesitates for nearly a second and then there's a big flash
{ 'action' : "pause", 'duration' : .8 },
{ 'action' : "flash", 'duration' : 2.2 },
# 4 Crackles a bit and pauses
{ 'action' : "crackle", 'duration' : .4 },
{ 'action' : "pause", 'duration' : .6 },
# 5 Tiny flash and then pauses
{ 'action' : "flash", 'duration' : .2 },
{ 'action' : "pause", 'duration' : .8 },
# 6 Crackles a bit more briefly and then pauses
{ 'action' : "crackle", 'duration' : .4 },
{ 'action' : "pause", 'duration' : .6 },
# 7 Tiny flash and then pauses
{ 'action' : "flash", 'duration' : .1 },
{ 'action' : "pause", 'duration' : .9 },
# 8 Flickers a bit more briefly and then pauses
{ 'action' : "flicker", 'duration' : .2 },
{ 'action' : "pause", 'duration' : .6 },
]
},
{
'description' : "Starts with a boom and then flickers and fades.",
'thunderFile' : "thunder-03.mp3",
'lightningPattern' : [
# 1 Hesitates for half a second and then flickers a bit
{ 'action' : "pause", 'duration' : .5 },
{ 'action' : "flicker", 'duration' : .5 },
# 2 Builds to a crackle and pauses
{ 'action' : "flicker", 'duration' : .25 },
{ 'action' : "crackle", 'duration' : .5 },
{ 'action' : "flicker", 'duration' : .25 },
# 3 flicker pause flicker
{ 'action' : "pause", 'duration' : .4 },
{ 'action' : "flicker", 'duration' : .2 },
{ 'action' : "pause", 'duration' : .2 },
{ 'action' : "flicker", 'duration' : .2 },
# 4 shorter flickers that fade out
{ 'action' : "pause", 'duration' : .5 },
{ 'action' : "flicker", 'duration' : .1 },
{ 'action' : "pause", 'duration' : .3 },
{ 'action' : "flicker", 'duration' : .1 },
# 5 Barely anything, maybe a short flicker
{ 'action' : "pause", 'duration' : .3 },
{ 'action' : "flicker", 'duration' : .1 },
{ 'action' : "pause", 'duration' : .6 },
# 6. basically a long pause, maybe a tiny flash
{ 'action' : "crackle", 'duration' : .2 },
{ 'action' : "pause", 'duration' : .8 },
# 6. basically a long pause, maybe a tiny flash
{ 'action' : "flash", 'duration' : .3 },
{ 'action' : "pause", 'duration' : .7 },
]
},
]
# loop through each strike object
while True:
for strike in strikes:
secondsPassed = 0
startTime = time.time()
# play the sound file and trigger the relay according to the strike object's light pattern
file = "Sounds/" + strike['thunderFile']
print("playing thunder track: " + file)
secondsPassed = time.time() - startTime
print("seconds passed: " + str(round(secondsPassed,2)))
print("flashing light pattern: " + strike['description'])
pygame.mixer.music.load(file)
pygame.mixer.music.play()
for pattern in strike['lightningPattern']:
if (pattern['action'] == "flash"):
flash(pattern['duration'])
elif (pattern['action'] == "crackle"):
pulse(pattern['duration'], .3)
elif (pattern['action'] == "flicker"):
pulse(pattern['duration'], .1)
else:
pause(pattern['duration'])
secondsPassed = time.time() - startTime
print("seconds passed: " + str(round(secondsPassed,2)))
thunderDuration = 10
timeRemaining = thunderDuration - secondsPassed
if(timeRemaining < thunderDuration):
if (timeRemaining > 0):
time.sleep(timeRemaining)
pygame.mixer.music.stop()
secondsPassed = 0
GPIO.cleanup()