Arduino midi problem

I have a strange problem with using Arduino MIDI library (not ardumidi) + hairless midi to serial converter:

I have two sketches, almost identical in what they do, but one works and one doesn’t:

First one, just sends one note on channel 1 ~every second:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(115200);
}

void loop() {
      MIDI.sendNoteOn(42, 127, 1);
      delay(10);
      MIDI.sendNoteOff(42, 127, 1);
      delay(1000);
}

And it works okay, one note plays every second. But when i upload second sketch, hairless serial-midi converter says +17.597 - Warning: got a status byte when we were expecting 1 more data bytes, sending possibly incomplete MIDI message 0xc0 Note, that the code sending midi messages is the same, only difference is how it is executed:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const int trs = 10;
void setup() {
  pinMode(A0, INPUT);
  Serial.begin(115200);
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
if (analogRead(A0) > trs)
    {
      //Serial.print(i);
      MIDI.sendNoteOn(42, 127, 1);
      delay(10);
      MIDI.sendNoteOff(42, 127, 1);
      
    }
}

Also, in second variant i have a piezo connected to A0 with it’s middle contact and outer one connected to GND on ArduinoMEGA

Help me, i am confused

the same, only difference is how it is executed:

and the fact that in the second one the 1 second delay is missing, of course

Oh, yeah. And now it works... Strange. But in final product i'll have to have a samlles delay possible. Why does just a delay changes everything so much?

It might have something to do with the library using running status messages when you send messages continuously, or nearly so. The default setting is that running status is used. Try turning that off (override UseRunningStatus) and then use the loop without the long delay.

Pete