Go Down

Topic: *SOLVED* MIDI IN to serial results in an endless stream of FF values (Read 131 times) previous topic - next topic

glocka

I'm trying to get incoming MIDI CC messages via serial, and transmit them out of another.

I have some devilishly simple code that isn't performing at all.

Code: [Select]


byte data;


void setup() {
  Serial1.begin(31250);
  Serial2.begin(31250);
}

void checkMIDI() {
  if (Serial1.available() > 0) {
   data = Serial1.read();
    if (data == 176) {
Serial2.write(Serial1.read());
    }
    }
  }



void loop() {
  checkMIDI();
}


When I check MIDI monitor connected to Serial2, the only readings I get are FF over and over again when I change a MIDI CC value.

I would expect to get 45 - or the hex value - as I am tweaking cutoff, followed by the value.

The significance of 176 is the first byte of a message containing MIDI CC on channel 1.

Thanks for your help in this deep confusion...

EDIT:

Synth attached is outputting MIDI clock pulses... I can see this if I pass through all MIDI info... It hits me with F8, which is a MIDI clock pulse.

It seems like something is interfering with MIDI, as even the simple code below causes an endless stream of FF:

void setup() {
 Serial3.begin(31250);
 Serial2.begin(31250);
}
void loop() {
Serial2.write(Serial3.read());
   }

glocka


el_supremo

I don't think you've solved the problem.
When Serial1.available() returns a value greater than zero it only guarantees you that there is at least one byte in the buffer. In your checkMIDI you read that first byte, but if it is 176 then you read another byte without making sure that there is another one in the buffer.
The same problem afflicts your second piece of code where you continuously read from Serial3 without checking whether there's anything there to be read.
Try this modified checkMIDI:
Code: [Select]
void checkMIDI() {
  if (Serial1.available() > 0) {
    data = Serial1.read();
    if (data == 176) {
      while(Serial1.available() < 1);
      Serial2.write(Serial1.read());
    }
  }
}


and in the second piece of code:
Code: [Select]
void loop() {
  if(Serial3.available() > 0) {
    Serial2.write(Serial3.read());
  }
}

Pete
P.S. forgot to mention the central point. If there's nothing in the buffer, Serial.read returns -1 which prints as hex FFFF (integer) or FF (char).

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy