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