Midi Latch for Bass Pedal

I am working on a bass pedal keyboard project, and I’m looking for some coding help with
adding a Note Hold or Latching feature to a sketch.

I’d like the ability to step on a note to turn it on,
then, either turn it off by stepping on the same key again, or stepping on a different key (then activating the new key).

To elaborate on how I would like this to work:
I step on the C note (MIDI Note number 36 - for example), sending a noteOn message,
The note continues until:
a: I step on the same note again, sending a noteOff message
OR
b: I step on a different note, sending a noteOff command for the “C”, and sending a noteOn message for the different note

I did a search for MIDI latching or “note hold”, and only found this:
https://forum.arduino.cc/t/untztrument-how-to-prog-buttons-w-midi-note-number-velocity-ch-latch-mode/622356/2

I have looked into example of “State Changing” sketches, but they are all geared toward turning an LED on or off,
and I have not been able to break down the syntax and apply it to the MIDI library commands.

The only information that seems to be available is for a pushbutton type controller, not an existing MIDI keyboard.

Here is the basic sketch I am using, that currently works with my Teensy 3.2 board and my MIDI controller:

#include <MIDI.h>
#include <Bounce.h>

MIDI_CREATE_DEFAULT_INSTANCE();

int noteArray[] = {36,37,38,39,40,41,42,43,44,45,46,47,48}; // My bass pedal has 13 keys

int hold = 0; // hold button ON, I'll add the toggle button later to turn the latch ON and OFF

void myHandleNoteOn(byte channel, byte note, byte velocity) {
  MIDI.sendNoteOn(note, velocity, channel);
}
  
void myHandleNoteOff(byte channel, byte note, byte velocity) { 
  MIDI.sendNoteOff(note, velocity, channel);
}

void setup()
{
  Serial.begin(9600);
  Serial.println("OK - 9600");

  //Connect the callback functions to the MIDI library
  MIDI.setHandleNoteOn(myHandleNoteOn);
  MIDI.setHandleNoteOff(myHandleNoteOff);

  MIDI.begin(MIDI_CHANNEL_OMNI);   // Listen on all channels
  MIDI.turnThruOff(); // Turns off incoming note form keyboard, only allowing processed notes
}

void loop()
{
  //Call MIDI.read(). MIDI class will automatically call callback
  //functions as needed.
  MIDI.read();
}

Any help is appreciated!

It makes no difference if you are turning an LED on and off, or sending MIDI note on / off messages. Just replace the LED on instruction with one to send a note on message.

The code you posted uses a baud rate of 9600, that is not going to work, it needs to be faster but the actual speed depends on how you are connecting to the MIDI system and what Arduino you are using.

1 Like

Thanks Mike! I’ll give it a go!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.