Skip to content

Instantly share code, notes, and snippets.

@idriszmy
Last active Jul 13, 2021
Embed
What would you like to do?
'''
References:
- Adafruit MIDI Library
https://circuitpython.readthedocs.io/projects/midi/en/latest/
- MatGyver DIY Electric MiDi Drum
https://matgyver.my/2021/06/29/diy-electric-midi-drum-piezo-electric/
CircuitPython libraries bundle: https://circuitpython.org/libraries
Additional libraries:
- adafruit_midi
Last Modified
- 13 July 2021
'''
import time
import board
from analogio import AnalogIn
import usb_midi
import adafruit_midi
from adafruit_midi.note_on import NoteOn
from adafruit_midi.note_off import NoteOff
from adafruit_midi.pitch_bend import PitchBend
from adafruit_midi.control_change import ControlChange
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)
piezo_1 = AnalogIn(board.A0)
piezo_2 = AnalogIn(board.A1)
snare_start = 5000
snare_stop = 2000
kick_start = 5000
kick_stop = 2000
snare_trigger = False
kick_trigger = False
print("Midi test")
print("Default output MIDI channel:", midi.out_channel + 1)
timer_1 = 0
timer_2 = 0
while True:
adc_piezo_1 = piezo_1.value
adc_piezo_2 = piezo_2.value
if adc_piezo_1 > kick_start and \
kick_trigger == False:
print((adc_piezo_1, adc_piezo_2))
timer_1 = time.monotonic()
midi.send([NoteOn(37, 120),
PitchBend(10000)])
midi.send([NoteOn(38, 120),
PitchBend(10000)])
kick_trigger = True
if adc_piezo_1 < kick_stop and \
kick_trigger == True and \
time.monotonic() - timer_1 > 0.1:
timer_1 = 0
midi.send([NoteOff("C#1", 120),
ControlChange(3, 44)])
midi.send([NoteOff("D1", 120),
ControlChange(3, 44)])
kick_trigger = False
if adc_piezo_2 > snare_start and \
snare_trigger == False:
print((adc_piezo_1, adc_piezo_2))
timer_2 = time.monotonic()
midi.send([NoteOn(36, 120),
PitchBend(10000)])
midi.send([NoteOn(38, 120),
PitchBend(10000)])
snare_trigger = True
if adc_piezo_2 < snare_stop and \
snare_trigger == True and \
time.monotonic() - timer_2 > 0.1:
timer_2 = 0
midi.send([NoteOff("C1", 120),
ControlChange(3, 44)])
midi.send([NoteOff("D1", 120),
ControlChange(3, 44)])
snare_trigger = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment