Problem Receiving Midi via USB on Arduino Due

Hi Everybody!

I’m programming a little sound generator on my Arduino Due and want to control parameters like pitch, volume and envelope times via Midi. So, I’m using Native Instruments Reaktor to send Midi Control Change messages and Hairless Midi-Serial-Bridge to transmit the Midi data to the USB programming port of the Arduino.
The Arduino, however, seems to receive only the first byte (status byte) correctly. The two following data bytes seem to get lost.
I set the baud rate on both the Hairless Midi-Serial-Bridge and the Arduino to 115200 (also tried other values) but it doesn’t work.

For testing I wrote this little sketch where I simply tried to change the LED brightness on pin 13 via midi control change. But after sending a midi CC message the LED is turned fully on and doesn’t react to further changes.

Can anybody help me with this? I would appreciate any tips.

Thanks,

Philipp

Test_Receive_Midi.ino (905 Bytes)

Ooops, here's the code again.

const byte noteOff = B10000000;
const byte noteOn = B10010000;
const byte cc = B10110000;
const byte pc = B11000000;
const byte pb = B11100000;

byte Channel = 0; //Midi Channel 1
byte Status;      //Midi Status Byte
byte Data1;       //first data byte
byte Data2;       //second data byte

void setup() {
  Serial.begin(115200);
  analogWrite(13, 0);                    //Switch LED off

}

void CheckMidi() {
  //do {
    if (Serial.available()) {
      Status = Serial.read();
      Data1 = Serial.read();
      Data2 = Serial.read();
      }
  //}
  //while (Serial.available() > 2);      // 2 bytes available
}


void loop() {
  CheckMidi();
  if (Status == (cc | Channel)) {        // check if it's a Midi CC on channel 1
    analogWrite(13,Data2);               // set LED brightness corresponding to CC value
  }
  //  if (Data2 > 63) {analogWrite(13, 255);}
  //  else {analogWrite(13, 0);}
}
    if (Serial.available()) {
      Status = Serial.read();
      Data1 = Serial.read();
      Data2 = Serial.read();

So if at least one byte is waiting in the buffer read three bytes. Can you see what is wrong with that?. The first byte comes along and you read that and the. Two more. Then you wonder where the others went, they never arrived before you read them.

Hi Mike,
Thanks for your reply. I understand what you mean. So I tried the following but it still doesn’t work.

if (Serial.available() > 2) {
      Status = Serial.read();
      Data1 = Serial.read();
      Data2 = Serial.read();
      }

And even with the code I found here http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/10/?lang=de it’s the same (I’m not sure, however, if there’s a difference between Arduino Uno and Due regarding serial data transfer).
Could you let me know how you would tackle the problem?

Just to make sure: I assume that reading one byte from the buffer clears that byte so that the next serial.read reads the next byte. Is that right?

Thanks

Sorry for my last post. While all the testing I must have set Hairless to the wrong baud rate.

With "if (Serial.available() > 2)" everything works perfectly.

Thank you very much for your hint and best regards.

Philipp