Best practices for treating MIDI as non-musical data?


Working on a commission, was suggested via the forums here to use MIDI instead of a straight serial connection from the Arduino. This is due to serial’s habit of dropping a connection randomly, which is only rectified by restarting the app or implementing a heartbeat system.

In any case, I’ve got my Leonardo with the MIDIUSB library and it seems to be working. I’m curious if there is a best method to send simple data. All I need my Arduino to do is send a couple bytes depending on how some switches are at any moment. Is it okay to simply use NoteOn, without ever doing a NoteOff? Is there a different method altogether that makes more sense? And I’m asking this in terms of what OpenFrameworks would like to interpret.


For now what I have done is simply pass the switch states as the pitch (0 = switch off, 1 = switch is on, grounded), and velocity.

void ofApp::newMidiMessage(ofxMidiMessage& msg) {
        // im just passing the A switch values in pitch and B switch
        // values in velocity
        if(msg.pitch == 0) {
            A = '0';
        } else {
            A = '1';
        if(msg.velocity == 0) {
            B = '0';
        } else {
            B = '1';
        cout << A << ", " << B << endl;
        // for logging, how many times has each been flipped on?
        if(prevA != A && A == '1') A_Count++;
        if(prevB != B && B == '1') B_Count++;

        prevA = A;
        prevB = B;
        ofLog() << ofGetTimestampString("%m-%d-%H-%M-%S")
        << " current states: " << A << ", " << B
        << " | A count: " << A_Count
        << " B count: " << B_Count
        << endl;

then my existing code is untouched.


I’ve never seen any issues with serial as the one you describe, although I’ve had a lot of different ones.
Where were you suggested to use midi instead of serial?
There are a few addons that give you a lot more flexibility for working with serial than the default ofSerial. Check
If you are going for midi, which I think it is a bit overkill for what you want, it shouldn’t matter what kind of message you send. It is up to your app how to interpret the midi messages. At the end, MIDI is also a kind of serial data, where its messages usually have the same amount of bytes. There are some exceptions, like pitchbend, but if you go with the regular messages, like notes and CC it really doesn’t matter which one you chose. Just keep track of what each message means.



You should try with ofxSerial, I used it a bit and is working fine


The MIDI abstraction has some yummy things that standard serial doesn’t, that I would have to rebuild. The main problem is that serial sometimes straight up drops the connection, which doesn’t have anything to do with ofxSerial specifically. MIDI libs have some nice capabilities to pick up dropped connections, re-handshake, etc. without me having to lift a god darned finger. I have since switched the installation over to MIDI and it’s working flawlessly, in case that helps anyone in the future!