Midi in on Mega serial vs serial1

Hi!

I’ve been using this midi-in setup with success http://forum.arduino.cc/index.php?topic=22447.0

But when i try changing the RX port code and wiring on my mega to another than RX0 i run into trouble.

Want am I doing wrong?

Here is a working test code with the normal serial setup to blink the LED on pin 13 when midi is coming in:

byte incomingByte;
byte note;
byte velocity;


int statusLed = 13;   // select the pin for the LED

int action = 2; //0 =note off ; 1=note on ; 2= nada


//setup: declaring iputs and outputs and begin serial
void setup() {
  pinMode(statusLed, OUTPUT);  // declare the LED's pin as output


  //start serial with midi baudrate 31250 or 38400 for debugging
  Serial.begin(31250);
  digitalWrite(statusLed, HIGH);
}

//loop: wait for serial data, and interpret the message
void loop () {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();


    // wait for as status-byte, channel 1, note on or off
    if (incomingByte == 144) { // note on message starting starting
      action = 1;
      blink();
    } else if (incomingByte == 128) { // note off message starting
      action = 0;
    } else if (incomingByte == 208) { // aftertouch message starting
      //not implemented yet
    } else if (incomingByte == 160) { // polypressure message starting
      //not implemented yet
    } else if ( (action == 0) && (note == 0) ) { // if we received a "note off", we wait for which note (databyte)
      note = incomingByte;
      playNote(note, 0);
      note = 0;
      velocity = 0;
      action = 2;
      digitalWrite(statusLed, HIGH);
    } else if ( (action == 1) && (note == 0) ) { // if we received a "note on", we wait for the note (databyte)
      note = incomingByte;
      blink();
    } else if ( (action == 1) && (note != 0) ) { // ...and then the velocity
      blink();
      velocity = incomingByte;
      playNote(note, velocity);
      note = 0;
      velocity = 0;
      action = 0;
    } else {
      //nada
    }
  }
}

void blink() {
  digitalWrite(statusLed, HIGH);
  delay(100);
  digitalWrite(statusLed, LOW);
  delay(100);
}


void playNote(byte note, byte velocity) {
  int value = LOW;
  if (velocity > 10) {
    value = HIGH;
  } else {
    value = LOW;
  }

  //since we don't want to "play" all notes we wait for a note between 36 & 44
  if (note >= 1 && note < 100) {
    blink();
    //byte myPin=note-34; // to get a pinnumber between 2 and 9
    //digitalWrite(myPin, value);
  }

}

Here a not test working code (all is the same but with serial1 instead) and RX wire is now on pin 19.

byte incomingByte;
byte note;
byte velocity;


int statusLed = 13;   // select the pin for the LED

int action = 2; //0 =note off ; 1=note on ; 2= nada


//setup: declaring iputs and outputs and begin serial
void setup() {
  pinMode(statusLed, OUTPUT);  // declare the LED's pin as output


  //start serial with midi baudrate 31250 or 38400 for debugging
  Serial1.begin(31250);
  digitalWrite(statusLed, HIGH);
}

//loop: wait for serial data, and interpret the message
void loop () {
  if (Serial1.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial1.read();


    // wait for as status-byte, channel 1, note on or off
    if (incomingByte == 144) { // note on message starting starting
      action = 1;
      blink();
    } else if (incomingByte == 128) { // note off message starting
      action = 0;
    } else if (incomingByte == 208) { // aftertouch message starting
      //not implemented yet
    } else if (incomingByte == 160) { // polypressure message starting
      //not implemented yet
    } else if ( (action == 0) && (note == 0) ) { // if we received a "note off", we wait for which note (databyte)
      note = incomingByte;
      playNote(note, 0);
      note = 0;
      velocity = 0;
      action = 2;
      digitalWrite(statusLed, HIGH);
    } else if ( (action == 1) && (note == 0) ) { // if we received a "note on", we wait for the note (databyte)
      note = incomingByte;
      blink();
    } else if ( (action == 1) && (note != 0) ) { // ...and then the velocity
      blink();
      velocity = incomingByte;
      playNote(note, velocity);
      note = 0;
      velocity = 0;
      action = 0;
    } else {
      //nada
    }
  }
}

void blink() {
  digitalWrite(statusLed, HIGH);
  delay(100);
  digitalWrite(statusLed, LOW);
  delay(100);
}


void playNote(byte note, byte velocity) {
  int value = LOW;
  if (velocity > 10) {
    value = HIGH;
  } else {
    value = LOW;
  }

  //since we don't want to "play" all notes we wait for a note between 36 & 44
  if (note >= 1 && note < 100) {
    blink();
    //byte myPin=note-34; // to get a pinnumber between 2 and 9
    //digitalWrite(myPin, value);
  }

}

What is going on?

I see two hypothesis : the serial port is not working because of a configuration problem or the input is broken (chip dead)

Maybe you can try to send something from Serial1, to see if the TX is working or not. If you are unable to send MIDI too, then it's probably a configuration issue. I do not have my Mega here with me, but I can take a look to that later. Now, I have already used the other serial ports of the Mega for some MIDI applications, so it should work for you too

Eventually, try to connect the RX0 to something else (maybe simply a pushbutton). You will receive garbage when you press the button, but it will trigger the reception (a falling edge will make the Mega believe that this is a start bit). If you can't get anything, then you may have the RX1 out of order. But the only way to be sure would be to swap with another Mega board.

Benoit