Skip to content

Commit

Permalink
feat: Set midi note number
Browse files Browse the repository at this point in the history
  • Loading branch information
madskjeldgaard committed May 9, 2024
1 parent c42589f commit 0f592fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
[![PlatformIO CI](https://github.com/madskjeldgaard/raspberry-pi-pico-usbmidi-platformio-template/actions/workflows/build.yml/badge.svg)](https://github.com/madskjeldgaard/raspberry-pi-pico-usbmidi-platformio-template/actions/workflows/build.yml)

# Raspberry Pi Pico platformio template
# Sustain2Midi

This is a simple template for creating an Arduino-based project for the Raspberry Pi Pico using platformio.
This project is a simple Sustain pedal to midi note converter based on a raspberry pi pico board.

It includes a simple example of using USB midi in this environment to easily create midi controllers from the Pico.
## Features

Also, it's set up to use c++17 instead of the default c++11 version.

The advantage of working with the Pico like this is that you are able to use it like any other Arduino enabled board basically. For more information, [see this link](https://arduino-pico.readthedocs.io/en/latest/index.html).

Best of all though? You can upload to the board without having to plug/unplug the device while pressing the boot button.
- Dirt cheap (all you need is a mono jack socket and a raspberry pi pico)
- Sends note on/off
- Note number can be changed easily by sending a midi note to the device.

## Dependencies

Expand Down
26 changes: 22 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Bounce2::Button sustainPedal1 = Bounce2::Button();

constexpr auto SUSTAIN_PIN = 0;
constexpr auto MIDI_NOTE = 48;
constexpr auto MIDI_VELOCITY = 127;
constexpr auto TO_CHANNEL = 1;

Expand All @@ -19,22 +18,35 @@ Adafruit_USBD_MIDI usbMidi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usbMidi, MIDI);

static bool toggle_state1 = false;
static int output_note = 48; // Default output note

void handle_sustain() {
sustainPedal1.update();

if (sustainPedal1.pressed()) {

if (toggle_state1) {
MIDI.sendNoteOn(MIDI_NOTE, MIDI_VELOCITY, TO_CHANNEL);
MIDI.sendNoteOn(output_note, MIDI_VELOCITY, TO_CHANNEL);
} else {
MIDI.sendNoteOff(MIDI_NOTE, 0, TO_CHANNEL);
MIDI.sendNoteOff(output_note, 0, TO_CHANNEL);
}

toggle_state1 = !toggle_state1;
}
}

void handle_midi_note_on(byte channel, byte note, byte velocity) {

// Send note off and reset toggle state before changing note

if (toggle_state1) {
MIDI.sendNoteOff(output_note, 0, TO_CHANNEL);
}

toggle_state1 = false;
output_note = note;
}

void setup() {
Serial.begin(115200);

Expand All @@ -44,10 +56,16 @@ void setup() {
usbMidi.begin();
MIDI.begin();

// Set the MIDI note on handling function
MIDI.setHandleNoteOn(handle_midi_note_on);

// Setup button
sustainPedal1.attach(PIN, PIN_MODE);
sustainPedal1.interval(INTERVAL_IN_MS);
sustainPedal1.setPressedState(LOW);
}

void loop() { handle_sustain(); }
void loop() {
handle_sustain();
MIDI.read(); // Add this line to read incoming MIDI messages
}

0 comments on commit 0f592fa

Please sign in to comment.