Skip to content

Commit aadbcd1

Browse files
authored
Merge pull request #732 from adafruit/propmaker-lightup-prop
Propmaker lightup prop
2 parents 1f472c6 + c97606c commit aadbcd1

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

PropMaker_Light_Up_Prop/code.py

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

0 commit comments

Comments
 (0)