|
| 1 | +# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import time |
| 6 | +import random |
| 7 | +import board |
| 8 | +import audiocore |
| 9 | +import audiobusio |
| 10 | +import audiomixer |
| 11 | +from digitalio import DigitalInOut, Direction |
| 12 | +from adafruit_ticks import ticks_ms, ticks_add, ticks_diff |
| 13 | +import neopixel |
| 14 | +import adafruit_lis3dh |
| 15 | + |
| 16 | +# CUSTOMIZE SENSITIVITY HERE: smaller numbers = more sensitive to motion |
| 17 | +SWING_1_THRESHOLD = 130 |
| 18 | +SWING_2_THRESHOLD = 170 |
| 19 | + |
| 20 | +pixel_time = 10 |
| 21 | +Y_THRESHOLD = -9.0 |
| 22 | + |
| 23 | +fx_1 = audiocore.WaveFile(open("/sounds/swing1.wav", "rb")) |
| 24 | +fx_2 = audiocore.WaveFile(open("/sounds/swing1b.wav", "rb")) |
| 25 | +fx_3 = audiocore.WaveFile(open("/sounds/swing2.wav", "rb")) |
| 26 | +fx_4 = audiocore.WaveFile(open("/sounds/swing3.wav", "rb")) |
| 27 | + |
| 28 | +tracks = [fx_1, fx_2, fx_3, fx_4] |
| 29 | +audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) |
| 30 | +mixer = audiomixer.Mixer(voice_count=3, sample_rate=22050, channel_count=1, |
| 31 | + bits_per_sample=16, samples_signed=True) |
| 32 | +audio.play(mixer) |
| 33 | + |
| 34 | +# external neopixels |
| 35 | +num_pixels = 50 |
| 36 | +pixels = neopixel.NeoPixel(board.EXTERNAL_NEOPIXELS, num_pixels, auto_write=False) |
| 37 | +pixels.brightness = 0.2 |
| 38 | + |
| 39 | +# Gradient colors (start and end) |
| 40 | +pale_green = (50, 255, 50) |
| 41 | +yellow = (255, 255, 0) |
| 42 | +# Pulse parameters |
| 43 | +pulse_speed = 0.01 # Speed of the pulse (lower is slower) |
| 44 | +brightness_step = 5 # Amount by which to step the brightness |
| 45 | + |
| 46 | +# onboard LIS3DH |
| 47 | +i2c = board.I2C() |
| 48 | +int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT) |
| 49 | +lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) |
| 50 | +# Accelerometer Range (can be 2_G, 4_G, 8_G, 16_G) |
| 51 | +lis3dh.range = adafruit_lis3dh.RANGE_2_G |
| 52 | + |
| 53 | +# enable external power pin |
| 54 | +# provides power to the external components |
| 55 | +external_power = DigitalInOut(board.EXTERNAL_POWER) |
| 56 | +external_power.direction = Direction.OUTPUT |
| 57 | +external_power.value = True |
| 58 | + |
| 59 | +mode = 0 |
| 60 | +brightness = 0 |
| 61 | +increasing = True |
| 62 | +pixel_clock = ticks_ms() |
| 63 | +VERTICAL_TIME = 2.0 # Time in seconds the sensor needs to be vertical to trigger |
| 64 | +# Variables to track time |
| 65 | +vertical_start_time = None |
| 66 | + |
| 67 | +while True: |
| 68 | + # default |
| 69 | + if mode == 0: |
| 70 | + if ticks_diff(ticks_ms(), pixel_clock) >= pixel_time: |
| 71 | + for i in range(num_pixels): |
| 72 | + # Calculate gradient between pale_green and yellow |
| 73 | + ratio = i / (num_pixels - 1) |
| 74 | + r = int((yellow[0] - pale_green[0]) * ratio + pale_green[0]) |
| 75 | + g = int((yellow[1] - pale_green[1]) * ratio + pale_green[1]) |
| 76 | + b = int((yellow[2] - pale_green[2]) * ratio + pale_green[2]) |
| 77 | + |
| 78 | + # Adjust brightness by scaling the RGB values |
| 79 | + scaled_r = int(r * (brightness / 255)) |
| 80 | + scaled_g = int(g * (brightness / 255)) |
| 81 | + scaled_b = int(b * (brightness / 255)) |
| 82 | + |
| 83 | + pixels[i] = scaled_r, scaled_g, scaled_b |
| 84 | + pixels.show() |
| 85 | + # Adjust brightness up and down |
| 86 | + if increasing: |
| 87 | + brightness += brightness_step |
| 88 | + if brightness >= 255: |
| 89 | + brightness = 255 |
| 90 | + increasing = False |
| 91 | + else: |
| 92 | + brightness -= brightness_step |
| 93 | + if brightness <= 0: |
| 94 | + brightness = 0 |
| 95 | + increasing = True |
| 96 | + pixel_clock = ticks_add(pixel_clock, pixel_time) |
| 97 | + x, y, z = lis3dh.acceleration |
| 98 | + #print(x, y, z) |
| 99 | + if abs(y) > Y_THRESHOLD and abs(x) < 2.0 and abs(z) < 2.0: |
| 100 | + if vertical_start_time is None: |
| 101 | + vertical_start_time = time.monotonic() # Start timing |
| 102 | + elif time.monotonic() - vertical_start_time >= VERTICAL_TIME: |
| 103 | + print("vertical") |
| 104 | + mode = "vertical" |
| 105 | + else: |
| 106 | + vertical_start_time = None # Reset if the sensor is not vertical |
| 107 | + accel_total = x * x + z * z |
| 108 | + if accel_total >= SWING_2_THRESHOLD: |
| 109 | + print("swing 2") |
| 110 | + mode = "swing 2" |
| 111 | + elif accel_total >= SWING_1_THRESHOLD: |
| 112 | + print("swing 1") |
| 113 | + mode = "swing 1" |
| 114 | + elif mode == "swing 1": |
| 115 | + mixer.voice[0].play(tracks[random.randint(0, 1)]) |
| 116 | + while mixer.voice[0].playing: |
| 117 | + pixels.fill(pale_green) |
| 118 | + pixels.show() |
| 119 | + mode = 0 |
| 120 | + # clash or move |
| 121 | + elif mode == "swing 2": |
| 122 | + mixer.voice[1].play(tracks[2]) |
| 123 | + while mixer.voice[1].playing: |
| 124 | + pixels.fill(yellow) |
| 125 | + pixels.show() |
| 126 | + mode = 0 |
| 127 | + elif mode == "vertical": |
| 128 | + mixer.voice[2].play(tracks[3]) |
| 129 | + while mixer.voice[2].playing: |
| 130 | + pixels.fill(yellow) |
| 131 | + pixels.show() |
| 132 | + vertical_start_time = None |
| 133 | + mode = 0 |
0 commit comments