Skip to content

Commit b36c2b6

Browse files
authored
Merge pull request #853 from adafruit/lego-cpx-lamp
Adding files lego head
2 parents 050c1e2 + a6d118a commit b36c2b6

6 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Shake Audio Lamp
2+
# for Adafruit Circuit Playground express
3+
# with CircuitPython
4+
import time
5+
import audioio
6+
import board
7+
8+
from adafruit_circuitplayground.express import cpx
9+
10+
# External Audio Stuff
11+
audio = audioio.AudioOut(board.A0) # Speaker
12+
wave_file = None
13+
14+
def play_wav(name, loop=False):
15+
"""
16+
Play a WAV file in the 'sounds' directory.
17+
:param name: partial file name string, complete name will be built around
18+
this, e.g. passing 'foo' will play file 'sounds/foo.wav'.
19+
:param loop: if True, sound will repeat indefinitely (until interrupted
20+
by another sound).
21+
"""
22+
global wave_file # pylint: disable=global-statement
23+
print("playing", name)
24+
if wave_file:
25+
wave_file.close()
26+
try:
27+
wave_file = open('sounds/' + name + '.wav', 'rb') # using wave files from sounds folder
28+
wave = audioio.WaveFile(wave_file)
29+
audio.play(wave, loop=loop)
30+
except OSError:
31+
pass # we'll just skip playing then
32+
33+
# flash neopixel effects
34+
def party_flash(duration):
35+
cpx.pixels.fill((255, 255, 255))
36+
cpx.pixels.show()
37+
time.sleep(duration)
38+
cpx.pixels.fill((255, 0, 0))
39+
cpx.pixels.show()
40+
time.sleep(duration)
41+
42+
def led_flash(duration):
43+
cpx.pixels.fill((255, 255, 255))
44+
cpx.pixels.show()
45+
time.sleep(duration)
46+
cpx.pixels.fill((0, 0, 0))
47+
cpx.pixels.show()
48+
time.sleep(duration)
49+
50+
# make a counter variable
51+
counter = 0
52+
53+
while True:
54+
# Listen for shakes
55+
if cpx.shake(shake_threshold=15): # adjust sensitivity - low number is more sensitive
56+
print("Shake detected!") # Let us know there was a shake
57+
counter = counter + 1 # Start a counter
58+
if counter == 2: # On second shake
59+
play_wav("awe-a") # play audio
60+
for _ in range(3): # loop x times
61+
party_flash(0.4) # neopixel flash
62+
elif counter == 3: # On third shake
63+
play_wav("awe-b")
64+
for _ in range(3): # loop x times
65+
party_flash(0.4) # neopixel flash
66+
elif counter == 4: # On fourth shake
67+
play_wav("awe-c")
68+
for _ in range(3): # loop x times
69+
party_flash(0.4) # neopixel flash
70+
elif counter == 5: # on fifth shake
71+
counter = 0 # Reset the counter back to zero
72+
play_wav("untz") #play audio
73+
for _ in range(3): # loop x times
74+
led_flash(.18) # faster pixel flash
75+
cpx.pixels.fill((255,255,255)) # solid pixel
76+
time.sleep(1) # light it for one second
77+
else: # On first shake
78+
play_wav("haha") # play audio
79+
cpx.pixels.fill((255,255,255)) # white color
80+
time.sleep(1) # for one second
81+
else: # When there's no shakyness to be had
82+
cpx.pixels.fill((0, 0, 0)) # keep pixels off when not shaking
99.1 KB
Binary file not shown.
112 KB
Binary file not shown.
108 KB
Binary file not shown.

LEGO_Head_CPX_Lamp/sounds/haha.wav

58.3 KB
Binary file not shown.

LEGO_Head_CPX_Lamp/sounds/untz.wav

97.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)