Skip to content

Commit 3ae4406

Browse files
authored
Merge pull request adafruit#1198 from adafruit/robot_drummer
Adding code for MIDI Solenoid Drum Kit
2 parents 79786dc + 365f253 commit 3ae4406

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

MIDI_Solenoid_Drum_Kit/code.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import time
2+
import board
3+
import digitalio
4+
import usb_midi
5+
import adafruit_midi
6+
from adafruit_midi.note_on import NoteOn
7+
8+
# pins for the solenoid output signals
9+
noid_pins = [board.D5, board.D6, board.D9, board.D10]
10+
11+
# array for the solenoids
12+
noids = []
13+
14+
# setup for the solenoid pins to be outputs
15+
for pin in noid_pins:
16+
noid = digitalio.DigitalInOut(pin)
17+
noid.direction = digitalio.Direction.OUTPUT
18+
noids.append(noid)
19+
20+
# MIDI note array
21+
notes = [60, 61, 62, 63]
22+
23+
# MIDI in setup
24+
midi = adafruit_midi.MIDI(midi_in=usb_midi.ports[0], in_channel=0)
25+
26+
# delay for solenoids
27+
speed = 0.03
28+
retract = 0
29+
30+
while True:
31+
32+
# msg holds MIDI messages
33+
msg = midi.receive()
34+
35+
for i in range(4):
36+
# states for solenoid on/off
37+
noid_output = noids[i]
38+
39+
# states for MIDI note recieved
40+
notes_played = notes[i]
41+
42+
# if NoteOn msg comes in and the MIDI note # matches with predefined notes:
43+
if isinstance(msg, NoteOn) and msg.note is notes_played:
44+
print(time.monotonic(), msg.note)
45+
46+
# solenoid is triggered
47+
noid_output.value = True
48+
# quick delay
49+
retract = time.monotonic()
50+
51+
# retracts solenoid using time.monotonic() to avoid delays between notes activating
52+
if (retract + speed) < time.monotonic():
53+
noid_output.value = False

0 commit comments

Comments
 (0)