Skip to content

Commit daa0947

Browse files
authored
Merge pull request #1904 from adafruit/tva_time_baton
TVA time baton code and sounds
2 parents fa1a841 + 143554d commit daa0947

File tree

15 files changed

+138
-0
lines changed

15 files changed

+138
-0
lines changed

TVA_Time_Baton_PropMaker/code.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Prop-Maker based Light Up Prop.
6+
Adafruit invests time and resources providing this open source code.
7+
Please support Adafruit and open source hardware by purchasing
8+
products from Adafruit!
9+
Written by Kattni Rembor for Adafruit Industries
10+
Copyright (c) 2019 Adafruit Industries
11+
Licensed under the MIT license.
12+
All text above must be included in any redistribution.
13+
"""
14+
import time
15+
import random
16+
import digitalio
17+
import audioio
18+
import audiocore
19+
import busio
20+
import board
21+
import adafruit_rgbled
22+
import adafruit_lis3dh
23+
24+
# CUSTOMISE COLORS HERE:
25+
MAIN_COLOR = (20, 10, 0) # Default is red
26+
HIT_COLOR = (255, 100, 0) # Default is white
27+
28+
# CUSTOMISE SENSITIVITY HERE: smaller numbers = more sensitive to motion
29+
HIT_THRESHOLD = 650
30+
SWING_THRESHOLD = 125
31+
32+
# Set to the length in seconds of the "on.wav" file
33+
POWER_ON_SOUND_DURATION = 1.5
34+
35+
POWER_PIN = board.D10
36+
37+
enable = digitalio.DigitalInOut(POWER_PIN)
38+
enable.direction = digitalio.Direction.OUTPUT
39+
enable.value = False
40+
41+
# Pin the Red LED is connected to
42+
RED_LED = board.D11
43+
44+
# Pin the Green LED is connected to
45+
GREEN_LED = board.D12
46+
47+
# Pin the Blue LED is connected to
48+
BLUE_LED = board.D13
49+
50+
# Create the RGB LED object
51+
led = adafruit_rgbled.RGBLED(RED_LED, GREEN_LED, BLUE_LED)
52+
53+
audio = audioio.AudioOut(board.A0) # Speaker
54+
55+
# Set up accelerometer on I2C bus, 4G range:
56+
i2c = busio.I2C(board.SCL, board.SDA)
57+
accel = adafruit_lis3dh.LIS3DH_I2C(i2c)
58+
accel.range = adafruit_lis3dh.RANGE_4_G
59+
60+
COLOR_HIT = HIT_COLOR # "hit" color is HIT_COLOR set above
61+
COLOR_SWING = MAIN_COLOR # "swing" color is MAIN_COLOR set above
62+
63+
64+
def play_wav(name, loop=False):
65+
"""
66+
Play a WAV file in the 'sounds' directory.
67+
:param name: partial file name string, complete name will be built around
68+
this, e.g. passing 'foo' will play file 'sounds/foo.wav'.
69+
:param loop: if True, sound will repeat indefinitely (until interrupted
70+
by another sound).
71+
"""
72+
print("playing", name)
73+
try:
74+
wave_file = open('sounds/' + name + '.wav', 'rb')
75+
wave = audiocore.WaveFile(wave_file)
76+
audio.play(wave, loop=loop)
77+
except: # pylint: disable=bare-except
78+
return
79+
80+
81+
# List of swing wav files without the .wav in the name for use with play_wav()
82+
swing_sounds = [
83+
'swing1',
84+
'swing2',
85+
'swing3',
86+
'swing4',
87+
'swing5',
88+
'swing6'
89+
]
90+
91+
# List of hit wav files without the .wav in the name for use with play_wav()
92+
hit_sounds = [
93+
'hit1',
94+
'hit2',
95+
'hit3',
96+
'hit4',
97+
'hit5',
98+
'hit6'
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
21.6 KB
Binary file not shown.
21.6 KB
Binary file not shown.
21.6 KB
Binary file not shown.
21.6 KB
Binary file not shown.
16.2 KB
Binary file not shown.
18.9 KB
Binary file not shown.
129 KB
Binary file not shown.
30.5 KB
Binary file not shown.
20.7 KB
Binary file not shown.
20.7 KB
Binary file not shown.
20.7 KB
Binary file not shown.
20.7 KB
Binary file not shown.
36.8 KB
Binary file not shown.
35.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)