Skip to content

Commit c408781

Browse files
authored
Merge pull request #2916 from adafruit/sensor_bat
code and sounds for motion sensor bat
2 parents d7b50d4 + e4d189b commit c408781

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

Motion_Sensor_Bat/bat0.wav

119 KB
Binary file not shown.

Motion_Sensor_Bat/bat1.wav

252 KB
Binary file not shown.

Motion_Sensor_Bat/bat2.wav

270 KB
Binary file not shown.

Motion_Sensor_Bat/code.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import board
6+
import audiocore
7+
import audiobusio
8+
import audiomixer
9+
import pwmio
10+
from digitalio import DigitalInOut, Direction, Pull
11+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
12+
from adafruit_motor import servo
13+
import adafruit_vl53l1x
14+
15+
distance_delay = 4 # how often vl53 is read
16+
servo_delays = [2.0, 1.5, 1.0, 0.5] # servo spin delay
17+
distances = [70, 50, 40, 50] # in centimeters
18+
max_audio = 1
19+
# audio files
20+
music = audiocore.WaveFile(open("music-loop-1.wav", "rb"))
21+
fx_1 = audiocore.WaveFile(open("bat0.wav", "rb"))
22+
fx_2 = audiocore.WaveFile(open("bat1.wav", "rb"))
23+
fx_3 = audiocore.WaveFile(open("bat2.wav", "rb"))
24+
25+
i2c = board.STEMMA_I2C()
26+
vl53 = adafruit_vl53l1x.VL53L1X(i2c)
27+
28+
tracks = [music, fx_1, fx_2, fx_3]
29+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
30+
mixer = audiomixer.Mixer(voice_count=4, sample_rate=22050, channel_count=1,
31+
bits_per_sample=16, samples_signed=True)
32+
audio.play(mixer)
33+
mixer.voice[0].play(tracks[0], loop=True)
34+
mixer.voice[0].level = 0.0
35+
36+
# enable external power pin
37+
# provides power to the external components
38+
external_power = DigitalInOut(board.EXTERNAL_POWER)
39+
external_power.direction = Direction.OUTPUT
40+
external_power.value = True
41+
42+
switch = DigitalInOut(board.EXTERNAL_BUTTON)
43+
switch.direction = Direction.INPUT
44+
switch.pull = Pull.UP
45+
46+
# servo control
47+
pwm = pwmio.PWMOut(board.EXTERNAL_SERVO, duty_cycle=2 ** 15, frequency=50)
48+
servo = servo.ContinuousServo(pwm, min_pulse=750, max_pulse=2250)
49+
50+
vl53.start_ranging()
51+
52+
vl53_clock = ticks_ms()
53+
vl53_time = distance_delay * 1000
54+
servo_clock = ticks_ms()
55+
servo_time = int(servo_delays[0] * 1000)
56+
prop_time = False
57+
servo_throttle = 0
58+
59+
while True:
60+
if switch.value:
61+
external_power.value = True
62+
if prop_time:
63+
if ticks_diff(ticks_ms(), servo_clock) >= servo_time:
64+
print(servo_throttle)
65+
servo.throttle = servo_throttle
66+
servo_throttle = not servo_throttle
67+
servo_clock = ticks_add(servo_clock, servo_time)
68+
if ticks_diff(ticks_ms(), vl53_clock) >= vl53_time:
69+
if vl53.data_ready:
70+
print(f"Distance: {vl53.distance} cm")
71+
vl53.clear_interrupt()
72+
if vl53.distance is None:
73+
prop_time = False
74+
mixer.voice[0].level = 0.0
75+
servo_time = int(servo_delays[0] * 1000)
76+
servo.throttle = 1.0
77+
else:
78+
closest_distance = min(distances, key=lambda x: abs(vl53.distance - x))
79+
# print(closest_distance)
80+
if vl53.distance <= distances[0]:
81+
prop_time = True
82+
mixer.voice[0].level = max_audio
83+
else:
84+
prop_time = False
85+
mixer.voice[0].level = 0.0
86+
servo.throttle = 1.0
87+
if closest_distance == distances[1]:
88+
mixer.voice[1].play(tracks[1], loop=False)
89+
servo_time = int(servo_delays[1] * 1000)
90+
elif closest_distance == distances[2]:
91+
mixer.voice[2].play(tracks[2], loop=False)
92+
servo_time = int(servo_delays[2] * 1000)
93+
elif closest_distance == distances[3]:
94+
mixer.voice[3].play(tracks[3], loop=False)
95+
servo_time = int(servo_delays[3] * 1000)
96+
vl53_clock = ticks_add(vl53_clock, vl53_time)
97+
else:
98+
external_power.value = False

Motion_Sensor_Bat/music-loop-1.wav

419 KB
Binary file not shown.

0 commit comments

Comments
 (0)