Infinite Playground Logo

Analog Pixel

midi foot pedal(s) v1.0
img

Next prototype of my midi foot pedals. Built using an rp2040 feather board from adafruit. It is able to advertise itself as a midi device which makes all of this very easy.

Looking at the code below, you can see that when the button is activated it sends a midi control signal at "100" and when it is deactivated it sends the same signal at "0". At least in Ableton, this works for activating and deactivating buttons in the interface. So If I attach one of the switches to the record button, then activating will start the record, and deactivating will stop the record.

Next up is soldering all of this to a real board and putting it in a box.

import time
import board
import busio
import digitalio
from simpleio import map_range
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction
import usb_midi
import adafruit_midi  # MIDI protocol encoder/decoder library
from adafruit_midi.control_change import ControlChange
from adafruit_midi.note_on import NoteOn

class Button:
    def __init__(self, name, pin, midi_io, active_function=False, deactive_function=False):
        """
        name(str): human name for the pin
        pin: pin definition 
        midi_io: control signal number to use 
        active_function = function to call when activated; calls with object, and state(active/deactive)
        deactive_function = function to call when deactivated; calls with object, and state(active/deactive)
        """

        self.name = name
        self.pin = digitalio.DigitalInOut(pin)
        self.pin.direction = digitalio.Direction.INPUT
        self.midi_io = midi_io

        self.active = False

        if active_function:
            self.on_active_function = active_function
        else:
            self.active_function = False

        if deactive_function:
            self.on_deactive_function = deactive_function
        else:
            self.on_deactive_function = False


    def poll(self):
        if self.pin.value and self.active == False:
            self.active = True
            if self.on_active_function:
                self.on_active_function(self, "activate")

        if not self.pin.value and self.active==True:
            self.active = False
            if self.on_deactive_function:
                self.on_deactive_function(self, "deactivate")


# pick your USB MIDI out channel here, 1-16
MIDI_USB_channel = 1
# pick your classic MIDI channel for sending over UART serial TX/RX
CLASSIC_MIDI_channel = 2

midi_usb = adafruit_midi.MIDI(
    midi_out=usb_midi.ports[1], out_channel=MIDI_USB_channel - 1
)

def update_button(button_object, e):
    print("button called", button_object.name,e)

    if e == "activate":
        midi_usb.send(ControlChange(button_object.midi_io, 100))

    if e == "deactivate":
        midi_usb.send(ControlChange(button_object.midi_io, 0))


button_group = [
    Button( "Button 12", board.D12, 12, update_button, update_button),
    Button( "Button 11", board.D11, 11, update_button, update_button),
    Button( "Button 10", board.D10, 10, update_button, update_button),
    Button( "Button 9", board.D9, 9, update_button, update_button)
    ]

## poll all the buttons for change
while True:
    for b in button_group:
        b.poll()
    time.sleep(0.2)


Home