Once you've finished setting up your Circuit Playground Express with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.

To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.

# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
from digitalio import DigitalInOut, Direction, Pull
import neopixel
import usb_midi
import adafruit_midi
from adafruit_midi.note_on          import NoteOn
from adafruit_midi.note_off         import NoteOff

#  USB MIDI setup
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)

#  NeoPixel setup
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.02, auto_write=False)

#  setup for digital inputs
inputs = []
cpx_pins = [board.A1, board.A2, board.A3, board.A4]

for pin in cpx_pins:
    cpx_pin = DigitalInOut(pin)
    cpx_pin.direction = Direction.INPUT
    cpx_pin.pull = Pull.UP
    inputs.append(cpx_pin)

#  NeoPixel colors
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)

#  debounce states for inputs
state_A1 = False
state_A2 = False
state_A3 = False
state_A4 = False

#  array of NeoPixel colors
colors = [GREEN, CYAN, BLUE, PURPLE]
#  array of MIDI notes
midi_notes = [60, 64, 67, 72]
#  array of input states
input_states = [state_A1, state_A2, state_A3, state_A4]

while True:

    #  iterate through colors, inputs and MIDI notes
    for i in range(4):
        #  reset the state of the input and send NoteOff msg
        #  after the input is released
        if inputs[i].value and input_states[i]:
            input_states[i] = False
            midi.send(NoteOff(midi_notes[i], 120))

        #  if an input is pressed...
        if not inputs[i].value and not input_states[i]:
            #  change thhe colors of the NeoPixels
            pixels.fill(colors[i])
            pixels.show()
            #  send the NoteOn msg
            midi.send(NoteOn(midi_notes[i], 120))
            #  update input state
            input_states[i] = True

Upload the Code and Libraries to the Circuit Playground Express

After downloading the Project Bundle, plug your Circuit Playground Express into the computer's USB port. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Circuit Playground Express's CIRCUITPY drive. 

  • lib folder
  • code.py

Your Circuit Playground Express CIRCUITPY drive should look like this after copying the lib folder and the code.py file.

How the CircuitPython Code Works

There are arrays of digital input pins, pin states, MIDI note numbers and NeoPixel colors setup in the code before the loop. Each of these arrays contain four indexes that correspond with each other. This will allow you to iterate through these arrays in the loop.

To customize your setup, you can change the defined NeoPixel colors in the colors array and the MIDI note numbers in the midi_notes array.

#  setup for digital inputs
inputs = []
cpx_pins = [board.A1, board.A2, board.A3, board.A4]

for pin in cpx_pins:
    cpx_pin = DigitalInOut(pin)
    cpx_pin.direction = Direction.INPUT
    cpx_pin.pull = Pull.UP
    inputs.append(cpx_pin)

#  NeoPixel colors
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)

#  debounce states for inputs
state_A1 = False
state_A2 = False
state_A3 = False
state_A4 = False

#  array of NeoPixel colors
colors = [GREEN, CYAN, BLUE, PURPLE]
#  array of MIDI notes
midi_notes = [60, 64, 67, 72]
#  array of input states
input_states = [state_A1, state_A2, state_A3, state_A4]

In the loop, i represents the selected index in each of the arrays. The for statement lets you setup the code so that you can check for the state of the different digital inputs. When a state change is detected, aka when a stomp pad is stomped, the NeoPixel color will change and a MIDI note message will be sent accordingly.

#  iterate through colors, inputs and MIDI notes
    for i in range(4):
        #  reset the state of the input and send NoteOff msg
        #  after the input is released
        if inputs[i].value and input_states[i]:
            input_states[i] = False
            midi.send(NoteOff(midi_notes[i], 120))

        #  if an input is pressed...
        if not inputs[i].value and not input_states[i]:
            #  change thhe colors of the NeoPixels
            pixels.fill(colors[i])
            pixels.show()
            #  send the NoteOn msg
            midi.send(NoteOn(midi_notes[i], 120))
            #  update input state
            input_states[i] = True

This guide was first published on Dec 14, 2021. It was last updated on 2021-12-14 21:57:48 -0500.

This page (Coding the MIDI Stomping Pads) was last updated on Jan 07, 2022.

Text editor powered by tinymce.