Skip to content

Commit 4d00b5f

Browse files
committed
prop maker light code
prop maker light code
1 parent 1f472c6 commit 4d00b5f

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

PropMaker_Light_Up_Prop/code.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import time
2+
import random
3+
import digitalio
4+
import audioio
5+
import busio
6+
import board
7+
import adafruit_rgbled
8+
import adafruit_lis3dh
9+
10+
# CUSTOMISE COLORS HERE:
11+
MAIN_COLOR = (255, 0, 0) # Default is red
12+
HIT_COLOR = (255, 255, 255) # Default is white
13+
14+
# CUSTOMISE SENSITIVITY HERE: smaller numbers = more sensitive to motion
15+
HIT_THRESHOLD = 650
16+
SWING_THRESHOLD = 125
17+
18+
# Set to the length in seconds of the "on.wav" file
19+
POWER_ON_SOUND_DURATION = 1.7
20+
21+
POWER_PIN = board.D10
22+
23+
enable = digitalio.DigitalInOut(POWER_PIN)
24+
enable.direction = digitalio.Direction.OUTPUT
25+
enable.value = False
26+
27+
# Pin the Red LED is connected to
28+
RED_LED = board.D11
29+
30+
# Pin the Green LED is connected to
31+
GREEN_LED = board.D12
32+
33+
# Pin the Blue LED is connected to
34+
BLUE_LED = board.D13
35+
36+
# Create the RGB LED object
37+
led = adafruit_rgbled.RGBLED(RED_LED, GREEN_LED, BLUE_LED)
38+
39+
audio = audioio.AudioOut(board.A0) # Speaker
40+
41+
# Set up accelerometer on I2C bus, 4G range:
42+
i2c = busio.I2C(board.SCL, board.SDA)
43+
accel = adafruit_lis3dh.LIS3DH_I2C(i2c)
44+
accel.range = adafruit_lis3dh.RANGE_4_G
45+
46+
COLOR_HIT = HIT_COLOR # "hit" color is HIT_COLOR set above
47+
COLOR_SWING = MAIN_COLOR # "swing" color is MAIN_COLOR set above
48+
49+
50+
def play_wav(name, loop=False):
51+
"""
52+
Play a WAV file in the 'sounds' directory.
53+
:param name: partial file name string, complete name will be built around
54+
this, e.g. passing 'foo' will play file 'sounds/foo.wav'.
55+
:param loop: if True, sound will repeat indefinitely (until interrupted
56+
by another sound).
57+
"""
58+
print("playing", name)
59+
try:
60+
wave_file = open('sounds/' + name + '.wav', 'rb')
61+
wave = audioio.WaveFile(wave_file)
62+
audio.play(wave, loop=loop)
63+
except: # pylint: disable=bare-except
64+
return
65+
66+
67+
# List of swing wav files without the .wav in the name for use with play_wav()
68+
swing_sounds = [
69+
'swing1',
70+
'swing2',
71+
'swing3',
72+
'swing4',
73+
'swing5',
74+
'swing6',
75+
'swing7',
76+
'swing8',
77+
]
78+
79+
# List of hit wav files without the .wav in the name for use with play_wav()
80+
hit_sounds = [
81+
'hit1',
82+
'hit2',
83+
'hit3',
84+
'hit4',
85+
'hit5',
86+
'hit6',
87+
'hit7',
88+
'hit8',
89+
]
90+
91+
mode = 0 # Initial mode = OFF
92+
93+
# Main loop
94+
while True:
95+
if mode == 0: # If currently off...
96+
enable.value = True
97+
play_wav('on') # Power up!
98+
led.color = MAIN_COLOR
99+
time.sleep(POWER_ON_SOUND_DURATION)
100+
play_wav('idle', loop=True) # Play idle sound now
101+
mode = 1 # Idle mode
102+
103+
elif mode >= 1: # If not OFF mode...
104+
x, y, z = accel.acceleration # Read accelerometer
105+
accel_total = x * x + z * z
106+
# (Y axis isn't needed, due to the orientation that the Prop-Maker
107+
# Wing is mounted. Also, square root isn't needed, since we're
108+
# comparing thresholds...use squared values instead.)
109+
if accel_total > HIT_THRESHOLD: # Large acceleration = HIT
110+
play_wav(random.choice(hit_sounds)) # Start playing 'hit' sound
111+
COLOR_ACTIVE = COLOR_HIT # Set color to fade from
112+
mode = 3 # HIT mode
113+
elif mode == 1 and accel_total > SWING_THRESHOLD: # Mild = SWING
114+
play_wav(random.choice(swing_sounds)) # Randomly choose from available swing sounds
115+
led.color = MAIN_COLOR # Set color to main color
116+
mode = 2 # SWING mode
117+
elif mode == 1:
118+
# Idle color
119+
led.color = MAIN_COLOR
120+
elif mode > 1: # If in SWING or HIT mode...
121+
if audio.playing: # And sound currently playing...
122+
if mode == 2: # If SWING,
123+
led.color = MAIN_COLOR
124+
else:
125+
led.color = HIT_COLOR # Set color to hit color
126+
else: # No sound now, but still SWING or HIT modes
127+
play_wav('idle', loop=True) # Resume idle sound
128+
mode = 1 # Return to idle mode

0 commit comments

Comments
 (0)