Skip to content

Commit 88fd010

Browse files
authored
Merge pull request #2552 from adafruit/kamek_wand
Adding code and sound file for Kamek magic wand
2 parents f0c0af9 + 0e7c08a commit 88fd010

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Kamek_Magic_Wand_Prop/code.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import audiocore
8+
import audiobusio
9+
import audiomixer
10+
import pwmio
11+
from digitalio import DigitalInOut, Direction, Pull
12+
import neopixel
13+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
14+
from adafruit_led_animation.animation.pulse import Pulse
15+
from adafruit_led_animation.animation.rainbow import Rainbow
16+
from adafruit_led_animation.color import RED
17+
from adafruit_motor import servo
18+
import adafruit_lis3dh
19+
20+
time.sleep(2)
21+
22+
# enable external power pin
23+
# provides power to the external components
24+
external_power = DigitalInOut(board.EXTERNAL_POWER)
25+
external_power.direction = Direction.OUTPUT
26+
external_power.value = True
27+
28+
# i2s playback
29+
wave_file = open("wand-mix-sfx.wav", "rb")
30+
wave = audiocore.WaveFile(wave_file)
31+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
32+
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1,
33+
bits_per_sample=16, samples_signed=True)
34+
audio.play(mixer)
35+
mixer.voice[0].play(wave, loop=True)
36+
mixer.voice[0].level = 0
37+
38+
# servo control
39+
pwm = pwmio.PWMOut(board.EXTERNAL_SERVO, frequency=50)
40+
prop_servo = servo.ContinuousServo(pwm)
41+
servo_move = False
42+
43+
# external button
44+
switch = DigitalInOut(board.EXTERNAL_BUTTON)
45+
switch.direction = Direction.INPUT
46+
switch.pull = Pull.UP
47+
switch_state = False
48+
49+
# external neopixels
50+
num_pixels = 24
51+
pixels = neopixel.NeoPixel(board.EXTERNAL_NEOPIXELS, num_pixels)
52+
pixels.brightness = 0.3
53+
rainbow = Rainbow(pixels, speed=0.05, period=2)
54+
pulse = Pulse(pixels, speed=0.1, color=RED, period=3)
55+
56+
i2c = board.I2C()
57+
int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT)
58+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
59+
lis3dh.range = adafruit_lis3dh.RANGE_2_G
60+
61+
clock = ticks_ms()
62+
prop_time = 3000
63+
64+
while True:
65+
if not servo_move:
66+
pulse.animate()
67+
mixer.voice[0].level = 0.0
68+
prop_servo.throttle = 0.0
69+
else:
70+
prop_servo.throttle = 0.5
71+
rainbow.animate()
72+
mixer.voice[0].level = 0.5
73+
if ticks_diff(ticks_ms(), clock) >= prop_time:
74+
servo_move = False
75+
if lis3dh.shake(shake_threshold=20) or not switch.value and switch_state is False:
76+
servo_move = True
77+
clock = ticks_ms()
78+
clock = ticks_add(clock, prop_time)
79+
switch_state = True
80+
if switch.value and switch_state is True:
81+
switch_state = False
126 KB
Binary file not shown.

0 commit comments

Comments
 (0)