Skip to content

Commit 557db82

Browse files
authored
Merge branch 'adafruit:main' into bls_cpx_ir_data
2 parents 1058399 + 6252a41 commit 557db82

File tree

36 files changed

+8549
-8
lines changed

36 files changed

+8549
-8
lines changed

Drum_Track_Sequencer/code.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# SPDX-FileCopyrightText: 2024 John Park for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Drum Track Sequencer
6+
Feather RP2040, Motor FeatherWing, stepper motor,
7+
four reflection sensors, USB MIDI out
8+
"""
9+
import asyncio
10+
import busio
11+
import board
12+
from adafruit_motorkit import MotorKit
13+
from adafruit_motor import stepper
14+
import keypad
15+
import usb_midi
16+
17+
# Tempo setup
18+
BPM = 100 # user set value
19+
tempo_table = { # motor speed seems non-linear, so we'll use a lookup table
20+
110: 0.0004,
21+
100: 0.001,
22+
90: 0.002,
23+
80: 0.003,
24+
75: 0.004,
25+
65: 0.005,
26+
60: 0.006,
27+
50: 0.008
28+
}
29+
def get_nearest_tempo(given_bpm):
30+
nearest_table_item = min(tempo_table.keys(), key=lambda k: abs(k - given_bpm))
31+
return tempo_table[nearest_table_item]
32+
motor_pause = get_nearest_tempo(BPM)
33+
34+
i2c=busio.I2C(board.SCL, board.SDA, frequency=400_000)
35+
36+
# Motor setup
37+
kit = MotorKit(i2c=i2c)
38+
motor_run=True
39+
40+
# Sensor setup
41+
optical_pins = (board.D6, board.D9, board.D10, board.D12)
42+
optical_sensors = keypad.Keys(optical_pins, value_when_pressed=False, pull=True)
43+
44+
# MIDI setup
45+
midi = usb_midi.ports[1]
46+
midi_notes = (36, 37, 38, 39) # typical drum voice notes
47+
48+
def play_drum(note):
49+
midi_msg_on = bytearray([0x99, note, 120]) # 0x90 noteOn ch1, 0x99 noteOn ch10
50+
midi_msg_off = bytearray([0x89, note, 0])
51+
midi.write(midi_msg_on)
52+
midi.write(midi_msg_off)
53+
54+
async def check_sensors():
55+
while True:
56+
optical_sensor = optical_sensors.events.get()
57+
if optical_sensor:
58+
if optical_sensor.pressed:
59+
track_num = optical_sensor.key_number
60+
# print("tripped", track_num)
61+
play_drum(midi_notes[track_num])
62+
await asyncio.sleep(0.008) # don't check sensors constantly or motor speed reduced
63+
64+
async def run_motor():
65+
while True:
66+
kit.stepper1.onestep(
67+
direction=stepper.BACKWARD,
68+
style=stepper.DOUBLE
69+
)
70+
await asyncio.sleep(motor_pause) # motor speed-- smaller numbers are faster
71+
72+
async def main():
73+
motor_task = asyncio.create_task(run_motor())
74+
sensor_task = asyncio.create_task(check_sensors())
75+
await asyncio.gather(motor_task, sensor_task)
76+
77+
asyncio.run(main())

Dune_Thumper/code.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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
12+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
13+
from adafruit_motor import servo
14+
import adafruit_lis3dh
15+
16+
time.sleep(2)
17+
18+
# enable external power pin
19+
# provides power to the external components
20+
external_power = DigitalInOut(board.EXTERNAL_POWER)
21+
external_power.direction = Direction.OUTPUT
22+
external_power.value = True
23+
24+
# i2s playback
25+
wave_file = open("dune_thumper_sfx.wav", "rb")
26+
wave = audiocore.WaveFile(wave_file)
27+
audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
28+
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1,
29+
bits_per_sample=16, samples_signed=True)
30+
audio.play(mixer)
31+
mixer.voice[0].play(wave, loop=True)
32+
mixer.voice[0].level = 0
33+
34+
# servo control
35+
pwm = pwmio.PWMOut(board.EXTERNAL_SERVO, frequency=10)
36+
prop_servo = servo.ContinuousServo(pwm)
37+
servo_move = False
38+
39+
i2c = board.I2C()
40+
int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT)
41+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
42+
lis3dh.range = adafruit_lis3dh.RANGE_2_G
43+
44+
clock = ticks_ms()
45+
prop_time = 4000
46+
47+
while True:
48+
if not servo_move:
49+
mixer.voice[0].level = 0.0
50+
prop_servo.throttle = 0.0
51+
else:
52+
prop_servo.throttle = 0.5
53+
mixer.voice[0].level = 0.5
54+
if ticks_diff(ticks_ms(), clock) >= prop_time:
55+
servo_move = False
56+
if lis3dh.shake(shake_threshold=20):
57+
servo_move = True
58+
clock = ticks_ms()
59+
clock = ticks_add(clock, prop_time)

Dune_Thumper/dune_thumper_sfx.wav

41.3 KB
Binary file not shown.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_DotStar.h>
6+
#include <Adafruit_NeoPixel.h>
7+
8+
#define NUMPIXELS 64
9+
Adafruit_DotStar dotstrip(NUMPIXELS, PIN_DATA, PIN_CLOCK, DOTSTAR_BRG);
10+
Adafruit_NeoPixel neostrip(NUMPIXELS, PIN_DATA, NEO_GRB + NEO_KHZ800);
11+
Adafruit_NeoPixel pixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); // internal board pixel
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
16+
dotstrip.begin();
17+
dotstrip.setBrightness(25);
18+
dotstrip.show();
19+
20+
neostrip.begin();
21+
neostrip.setBrightness(25);
22+
neostrip.show();
23+
24+
pixel.begin();
25+
pixel.setBrightness(25);
26+
pixel.show();
27+
}
28+
29+
uint16_t firstPixelHue = 0;
30+
bool which_strip = false;
31+
32+
void loop() {
33+
firstPixelHue += 256;
34+
35+
if (which_strip == true) {
36+
// neopixel
37+
for(int i=0; i<neostrip.numPixels(); i++) {
38+
int pixelHue = firstPixelHue + (i * 65536L / neostrip.numPixels());
39+
neostrip.setPixelColor(i, neostrip.gamma32(neostrip.ColorHSV(pixelHue)));
40+
}
41+
neostrip.show();
42+
} else {
43+
// dotstar
44+
for(int i=0; i<dotstrip.numPixels(); i++) {
45+
int pixelHue = firstPixelHue + (i * 65536L / dotstrip.numPixels());
46+
dotstrip.setPixelColor(i, dotstrip.gamma32(dotstrip.ColorHSV(pixelHue)));
47+
}
48+
dotstrip.show();
49+
}
50+
pixel.setPixelColor(0, pixel.gamma32(pixel.ColorHSV(firstPixelHue)));
51+
pixel.show();
52+
delay(10);
53+
54+
if (firstPixelHue == 0) {
55+
which_strip = !which_strip;
56+
}
57+
58+
float powerVoltage;
59+
powerVoltage = analogRead(A0) / 1023.0 * 3.3;
60+
powerVoltage *= 2; // resistor divider by 2, so * by 2
61+
Serial.print("Power voltage: ");
62+
Serial.print(powerVoltage);
63+
Serial.println(" V");
64+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
7+
Adafruit_NeoPixel pixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
8+
9+
const int buttonPin = MISO;
10+
11+
int buttonState = 0;
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
pinMode(buttonPin, INPUT);
16+
pixel.begin();
17+
pixel.setBrightness(10);
18+
pixel.show();
19+
}
20+
21+
void loop() {
22+
buttonState = digitalRead(buttonPin);
23+
24+
if (buttonState == HIGH) {
25+
pixel.setPixelColor(0, 0x0);
26+
pixel.show();
27+
} else {
28+
pixel.setPixelColor(0, 0xFF0000);
29+
pixel.show();
30+
}
31+
delay(100);
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_DotStar.h>
6+
7+
#define NUMPIXELS 64
8+
Adafruit_DotStar dotstrip(NUMPIXELS, PIN_DATA, PIN_CLOCK, DOTSTAR_BRG);
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
13+
dotstrip.begin();
14+
dotstrip.setBrightness(25);
15+
dotstrip.show();
16+
17+
}
18+
19+
uint16_t firstPixelHue = 0;
20+
21+
void loop() {
22+
firstPixelHue += 256;
23+
24+
for(int i=0; i<dotstrip.numPixels(); i++) {
25+
int pixelHue = firstPixelHue + (i * 65536L / dotstrip.numPixels());
26+
dotstrip.setPixelColor(i, dotstrip.gamma32(dotstrip.ColorHSV(pixelHue)));
27+
}
28+
dotstrip.show();
29+
delay(10);
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
7+
#define NUMPIXELS 64
8+
Adafruit_NeoPixel neostrip(NUMPIXELS, PIN_DATA, NEO_GRB + NEO_KHZ800);
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
13+
neostrip.begin();
14+
neostrip.setBrightness(25);
15+
neostrip.show();
16+
17+
}
18+
19+
uint16_t firstPixelHue = 0;
20+
21+
void loop() {
22+
firstPixelHue += 256;
23+
for(int i=0; i<neostrip.numPixels(); i++) {
24+
int pixelHue = firstPixelHue + (i * 65536L / neostrip.numPixels());
25+
neostrip.setPixelColor(i, neostrip.gamma32(neostrip.ColorHSV(pixelHue)));
26+
}
27+
neostrip.show();
28+
29+
delay(10);
30+
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""CircuitPython Digital Input example
4+
Blinking a built-in NeoPixel LED using a button switch.
5+
6+
"""
7+
import board
8+
import digitalio
9+
import neopixel
10+
11+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
12+
13+
button = digitalio.DigitalInOut(board.D4)
14+
button.switch_to_input(pull=digitalio.Pull.UP)
15+
16+
while True:
17+
if not button.value:
18+
pixel.fill((255, 0, 0))
19+
else:
20+
pixel.fill((0, 0, 0))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
from rainbowio import colorwheel
7+
import adafruit_dotstar as dotstar
8+
9+
clock_pin = board.CLOCK
10+
data_pin = board.DATA
11+
num_dots = 30
12+
speed = 0.01
13+
brightness = 0.2
14+
order = "PGBR" # PGBR, PGRB, PRBG or PRGB
15+
dots = dotstar.DotStar(clock_pin, data_pin, num_dots,
16+
brightness=brightness, auto_write=True,
17+
pixel_order=order)
18+
19+
hue = 0
20+
dots.fill(colorwheel(hue))
21+
22+
while True:
23+
hue = (hue + 1) % 256
24+
dots.fill(colorwheel(hue))
25+
time.sleep(speed)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
from rainbowio import colorwheel
7+
import neopixel
8+
9+
pixel_pin = board.DATA
10+
num_pixels = 30
11+
speed = 0.01
12+
brightness = 0.5
13+
order = "GRB" # "GRBW" for RGBW NeoPixels
14+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels,
15+
brightness=brightness, auto_write=True,
16+
pixel_order=order)
17+
hue = 0
18+
pixels.fill(colorwheel(hue))
19+
20+
while True:
21+
hue = (hue + 1) % 256
22+
pixels.fill(colorwheel(hue))
23+
time.sleep(speed)

0 commit comments

Comments
 (0)