Raspberry Pi Pico MIDI Box: Blog#4 Let's Code
So once we have put the hardware together, now we can start to code. For the coding, I have decided to use Circuit Python. And to test the MIDI I decided to use the FL studio. First, let's look at Circuit Python.
For the code, I used Circuit Python and I referred to this library written by Guitarman9119. Below is the snippet of the code.
# This code is adapted from Liz Clark - MIDIFIGHTER project for Adafruit Industries -
# https://learn.adafruit.com/raspberry-pi-pico-led-arcade-button-midi-controller-fighter
import time
import board
import terminalio
import busio
import digitalio
import usb_midi
import adafruit_midi
from adafruit_midi.note_on import NoteOn
from adafruit_midi.note_off import NoteOff
# MIDI setup as MIDI out device
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)
note_pins = [board.GP12, board.GP13,board.GP14,board.GP15,board.GP16,board.GP17,board.GP18,
board.GP19,board.GP20,board.GP21,board.GP22,board.GP26]
note_buttons = []
for pin in note_pins:
note_pin = digitalio.DigitalInOut(pin)
note_pin.direction = digitalio.Direction.INPUT
note_pin.pull = digitalio.Pull.UP
note_buttons.append(note_pin)
# note states
note0_pressed = False
note1_pressed = False
note2_pressed = False
note3_pressed = False
note4_pressed = False
note5_pressed = False
note6_pressed = False
note7_pressed = False
note8_pressed = False
note9_pressed = False
note10_pressed = False
note11_pressed = False
# array of note states
note_states = [note0_pressed, note1_pressed, note2_pressed, note3_pressed,
note4_pressed, note5_pressed, note6_pressed, note7_pressed,
note8_pressed, note9_pressed, note10_pressed, note11_pressed]
# array of default MIDI notes
midi_notes = [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71]
while True:
# MIDI input
for i in range(12):
buttons = note_buttons[i]
# if button is pressed...
if buttons.value and note_states[i] is True:
# send the MIDI note and light up the LED
midi.send(NoteOn(midi_notes[i], 120))
note_states[i] = False
print(midi_notes[i])
# if the button is released...
if not buttons.value and note_states[i] is False:
# stop sending the MIDI note and turn off the LED
midi.send(NoteOff(midi_notes[i], 120))
note_states[i] = True
I used Thonny as the intepreter. I have made a few minor changes to the code.
In the below video it is shown how to run the code and check if all the push buttons correspond when it is pressed.