Arduino uno midi system exclusive

You could try something like this:

#include <Control_Surface.h> // https://github.com/tttapa/Control-Surface

USBDebugMIDI_Interface midi;

constexpr uint8_t Coff[] {0xF0,0x42,0x7F,0x60,0x01,0x01,0x10,0x7D,0x00,0x4E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF7};
constexpr uint8_t Con[]  {0xF0,0x42,0x7F,0x60,0x01,0x01,0x00,0x7D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF7};

constexpr uint8_t ledPin = 13;
bool ledState = false;

void updateLed(bool state) {
  ledState = state;
  digitalWrite(ledPin, ledState ? HIGH : LOW);
}

// Custom MIDI callback to handle incoming SysEx messages.
struct MyMIDI_Callbacks : MIDI_Callbacks {

  // This function is called when a SysEx message is received.
  void onSysExMessage(MIDI_Interface &, SysExMessage sysex) override {
    if (sysex.length == sizeof(Coff)
     && std::equal(std::begin(Coff), std::end(Coff), sysex.data)) {
      updateLed(false);
    } else if (sysex.length == sizeof(Con)
     && std::equal(std::begin(Con), std::end(Con), sysex.data)) {
      updateLed(true);
    }
    // Note that the std::equal calls could be optimized because the
    // Coff and Conn messages are largely the same, but this is just
    // a proof of concept
  }

} callback;

// Push button connected between pin 12 and ground. 
// Internal pull-up is enabled.
Button pushbutton {12};

void setup() {
  pushbutton.begin(); // enables internal pull-up
  pinMode(ledPin, OUTPUT);
  midi.begin();
  midi.setCallbacks(callback);
}

void loop() {
  if (pushbutton.update() == Button::Falling) {
    updateLed(!ledState);
    midi.sendSysEx(ledState ? Con : Coff);
  }

  // Read incoming MIDI data and call the callback if a new
  // SysEx message has been received.
  midi.update();
}

Upload, open the serial monitor (115200 baud) and press the button on pin 12 (connected to ground, using internal pull-up resistor).
You should see something like this:

System Exclusive [23]	F0 42 7F 60 01 01 00 7D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F7
System Exclusive [23]	F0 42 7F 60 01 01 10 7D 00 4E 00 00 00 00 00 00 00 00 00 00 00 00 F7
System Exclusive [23]	F0 42 7F 60 01 01 00 7D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F7
System Exclusive [23]	F0 42 7F 60 01 01 10 7D 00 4E 00 00 00 00 00 00 00 00 00 00 00 00 F7

Now enter F0 42 7F 60 01 01 00 7D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F7 and press enter. The LED should turn on.
Enter F0 42 7F 60 01 01 10 7D 00 4E 00 00 00 00 00 00 00 00 00 00 00 00 F7 and the LED should turn off.

To test it with your keyboard, change USBDebugMIDI_Interface midi to HardwareSerialMIDI_Interface midi = Serial. Then use MIDI firmware such as USBMidiKlik.

1 Like