Once you've finished setting up your Feather M4 Express with CircuitPython, you can access the code, audio files and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Prop-Maker based Light Up Prop.
Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!
Written by Kattni Rembor for Adafruit Industries
Copyright (c) 2019 Adafruit Industries
Licensed under the MIT license.
All text above must be included in any redistribution.
"""
import time
import random
import digitalio
import audioio
import audiocore
import busio
import board
import adafruit_rgbled
import adafruit_lis3dh
# CUSTOMISE COLORS HERE:
MAIN_COLOR = (20, 10, 0) # Default is red
HIT_COLOR = (255, 100, 0) # Default is white
# CUSTOMISE SENSITIVITY HERE: smaller numbers = more sensitive to motion
HIT_THRESHOLD = 650
SWING_THRESHOLD = 125
# Set to the length in seconds of the "on.wav" file
POWER_ON_SOUND_DURATION = 1.5
POWER_PIN = board.D10
enable = digitalio.DigitalInOut(POWER_PIN)
enable.direction = digitalio.Direction.OUTPUT
enable.value = False
# Pin the Red LED is connected to
RED_LED = board.D11
# Pin the Green LED is connected to
GREEN_LED = board.D12
# Pin the Blue LED is connected to
BLUE_LED = board.D13
# Create the RGB LED object
led = adafruit_rgbled.RGBLED(RED_LED, GREEN_LED, BLUE_LED)
audio = audioio.AudioOut(board.A0) # Speaker
# Set up accelerometer on I2C bus, 4G range:
i2c = busio.I2C(board.SCL, board.SDA)
accel = adafruit_lis3dh.LIS3DH_I2C(i2c)
accel.range = adafruit_lis3dh.RANGE_4_G
COLOR_HIT = HIT_COLOR # "hit" color is HIT_COLOR set above
COLOR_SWING = MAIN_COLOR # "swing" color is MAIN_COLOR set above
def play_wav(name, loop=False):
"""
Play a WAV file in the 'sounds' directory.
:param name: partial file name string, complete name will be built around
this, e.g. passing 'foo' will play file 'sounds/foo.wav'.
:param loop: if True, sound will repeat indefinitely (until interrupted
by another sound).
"""
print("playing", name)
try:
wave_file = open('sounds/' + name + '.wav', 'rb')
wave = audiocore.WaveFile(wave_file)
audio.play(wave, loop=loop)
except: # pylint: disable=bare-except
return
# List of swing wav files without the .wav in the name for use with play_wav()
swing_sounds = [
'swing1',
'swing2',
'swing3',
'swing4',
'swing5',
'swing6'
]
# List of hit wav files without the .wav in the name for use with play_wav()
hit_sounds = [
'hit1',
'hit2',
'hit3',
'hit4',
'hit5',
'hit6'
]
mode = 0 # Initial mode = OFF
# Main loop
while True:
if mode == 0: # If currently off...
enable.value = True
play_wav('on') # Power up!
led.color = MAIN_COLOR
time.sleep(POWER_ON_SOUND_DURATION)
play_wav('idle', loop=True) # Play idle sound now
mode = 1 # Idle mode
elif mode >= 1: # If not OFF mode...
x, y, z = accel.acceleration # Read accelerometer
accel_total = x * x + z * z
# (Y axis isn't needed, due to the orientation that the Prop-Maker
# Wing is mounted. Also, square root isn't needed, since we're
# comparing thresholds...use squared values instead.)
if accel_total > HIT_THRESHOLD: # Large acceleration = HIT
play_wav(random.choice(hit_sounds)) # Start playing 'hit' sound
COLOR_ACTIVE = COLOR_HIT # Set color to fade from
mode = 3 # HIT mode
elif mode == 1 and accel_total > SWING_THRESHOLD: # Mild = SWING
play_wav(random.choice(swing_sounds)) # Randomly choose from available swing sounds
led.color = MAIN_COLOR # Set color to main color
mode = 2 # SWING mode
elif mode == 1:
# Idle color
led.color = MAIN_COLOR
elif mode > 1: # If in SWING or HIT mode...
if audio.playing: # And sound currently playing...
if mode == 2: # If SWING,
led.color = MAIN_COLOR
else:
led.color = HIT_COLOR # Set color to hit color
else: # No sound now, but still SWING or HIT modes
play_wav('idle', loop=True) # Resume idle sound
mode = 1 # Return to idle mode
After downloading the Project Bundle, plug your Feather M4 Express into the computer USB port. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Feather M4's CIRCUITPY drive.
- lib folder
- sounds folder
- code.py
Your Feather M4 CIRCUITPY drive should look like this after copying the lib folder, sounds folder and code.py file.
Audio Files
The sounds used in this project are from the Motion Pulse BlackBox pack from VideoCopilot. The pack contains various sounds that are already in the supported audio format.
Adafruit CircuitPython supports 16-bit, Mono, 22.050kHz .wav audio format. See this guide to help format any audio files you might want to use in this project besides the files provided.
In the main loop, the swing and hit modes randomly choose from a list of sounds. For example, swing1.wav, swing2.wav, swing3, etc. This makes the motion effects feel much more varied and sounds less repetitive.
- Power on – on.wav
- Idle looping noise – idle.wav
- Swing whoosh – swing.wav
- Crash strike – hit.wav
Page last edited January 21, 2025
Text editor powered by tinymce.