MIDI Serial1 using MIDI.h not working for Midi Pedal Switcher

`Hey all, I'm having some issues using Serial1 (2 and 3) instead of Serial for Midi messages using MIDI.h. Everything works fine, but I must be missing something for Serial1 to finally work. I#m working in a Midi-Pedal Switcher and I have seen some comments of other DIYers, but they didn't fix my issue.

Posted all my code, just in case it can be helpful to someone like me. Thanks for any help.


```cpp
#include <hd44780.h>
#include <MIDI.h>
#include <Wire.h>                           // not surte I really needf it, but meh...
#include <hd44780.h>                        // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h>  // i2c expander i/o class header
#include <serialMIDI.h>                     // not sure I need it


MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);  // works flawlessly. Just a pain to disconnect TX0 when uploading every time

//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); // wont send MIDI out. I'v tried a lot, too, no success




hd44780_I2Cexp lcd;  // declare lcd object: auto locate & auto config expander chip




const int LCD_COLS = 20;
const int LCD_ROWS = 4;
// LEDs
const int led1(22);
const int led2(23);
const int led3(24);
const int led4(25);
const int led5(26);
const int led6(27);
const int led7(28);
const int led8(29);
const int led9(30);
const int led10(31);
// Switches
const int sw1(41);
const int sw2(42);
const int sw3(43);
const int sw4(44);
const int sw5(45);
const int sw6(46);
const int sw7(47);
const int sw8(48);
const int sw9(49);
const int sw10(50);
const int boostSw(32);

int stateLed1 = LOW;
int stateLed2 = LOW;
int stateLed3 = LOW;
int stateLed4 = LOW;
int stateLed5 = LOW;
int stateLed6 = LOW;
int stateLed7 = LOW;
int stateLed8 = LOW;
int stateLed9 = LOW;
int stateLed10 = LOW;

int stateSw1 = true;  // true because INPUT_PULLUP
int stateSw2 = true;
int stateSw3 = true;
int stateSw4 = true;
int stateSw5 = true;
int stateSw6 = true;
int stateSw7 = true;
int stateSw8 = true;
int stateSw9 = true;
int stateSw10 = true;
int lastStateSw1 = false;
int lastStateSw2 = false;
int lastStateSw3 = false;
int lastStateSw4 = false;
int lastStateSw5 = false;
int lastStateSw6 = false;
int lastStateSw7 = false;
int lastStateSw8 = false;
int lastStateSw9 = false;
int lastStateSw10 = false;

int statePg1 = false;
int statePg2 = false;
int statePg3 = false;
int statePg4 = false;
int statePg5 = false;
int statePg6 = false;
int statePg7 = false;
int statePg8 = false;
int statePg9 = false;
int statePg10 = false;

int name = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int sendValue1 = 0;  // the actual send value is off by 1 ("45" sent, "46" rcvd) so I deduct 1 in the switch code below)
int sendValue2 = 0;
int tap = 0;
int tap2 = 0;
bool midBoost = 0;  
int boostMsg = 0;
int boostLevel = 127;
int normLevel = 70;


//                                PRESET 1 VALUES
byte pg1ch1 = 0;  // Timeline - delay
byte pg1ch2 = 0;  // SPACE -  reverb
byte pg1ch3 = 1;  // Heavy Channel

//                                PRESET 2 VALUES

byte pg2ch1 = 29;
byte pg2ch2 = 22;
byte pg2ch3 = 2;

//                                PRESET 3 VALUES
byte pg3ch1 = 22;
byte pg3ch2 = 9;
byte pg3ch3 = 2;

//                                PRESET 4 VALUES
byte pg4ch1 = 0;
byte pg4ch2 = 5;
byte pg4ch3 = 2;

//                                PRESET 5 VALUES
byte pg5ch1 = 0;
byte pg5ch2 = 19;
byte pg5ch3 = 3;

//                                PRESET 6 VALUES
byte pg6ch1 = 1;
byte pg6ch2 = 11;
byte pg6ch3 = 3;

//                                PRESET 7 VALUES
byte pg7ch1 = 10;
byte pg7ch2 = 21;
byte pg7ch3 = 3;

//                                PRESET 8 VALUES
byte pg8ch1 = 0;
byte pg8ch2 = 5;
byte pg8ch3 = 3;

void setup() {

  int status;

  // initialize LCD with number of columns and rows:
  // hd44780 returns a status from begin() that can be used
  // to determine if initalization failed.
  // the actual status codes are defined in <hd44780.h>
  // See the values RV_XXXX
  //
  // looking at the return status from begin() is optional
  // it is being done here to provide feedback should there be an issue
  //
  // note:
  //	begin() will automatically turn on the backlight
  //
  status = lcd.begin(LCD_COLS, LCD_ROWS);
  if (status)  // non zero status means it was unsuccesful
  {
    // hd44780 has a fatalError() routine that blinks an led if possible
    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status);  // does not return
  }
pinMode (18, OUTPUT);
pinMode (19, INPUT);
MIDI.begin(31250);
  MIDI.begin(MIDI_CHANNEL_OMNI);  // I nbeed it to listen to all MIDI Chs

  // pinMode (17, INPUT_PULLUP); tried declaring RX2 as an input to make it work (it was the fix for DFRobot MP3 player at Serial2)
  // pinMode (16, OUTPUT); tried declaring TX2 as an output (it was the fix for DFRobot MP3 player at Serial2)
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);
  pinMode(sw1, INPUT_PULLUP);
  pinMode(sw2, INPUT_PULLUP);
  pinMode(sw3, INPUT_PULLUP);
  pinMode(sw4, INPUT_PULLUP);
  pinMode(sw5, INPUT_PULLUP);
  pinMode(sw6, INPUT_PULLUP);
  pinMode(sw7, INPUT_PULLUP);
  pinMode(sw8, INPUT_PULLUP);
  pinMode(sw9, INPUT_PULLUP);
  pinMode(sw10, INPUT_PULLUP);
  pinMode(boostSw, INPUT_PULLUP);
  // LED FUNCTION CHECK ON STARTUP - Check LEDs
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led9, HIGH);
  digitalWrite(led10, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("    MIDI-MONSTER    ");
  lcd.setCursor(0, 2);
  lcd.print("setting Amp to DEATH");
  MIDI.sendProgramChange (1,4);             // activating Death-Metal Preset
  delay(1000);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led9, LOW);
  digitalWrite(led10, LOW);
  lcd.clear();
}

void loop() {

  lcd.setCursor(0, 1);
  lcd.print("Tml1-");
  lcd.setCursor(5, 1);
  lcd.print(val1);
  lcd.setCursor(0, 2);
  lcd.print("Spc2-");
  lcd.setCursor(5, 2);
  lcd.print(val2);
  lcd.setCursor(0, 3);
  lcd.print("Hvy3-");
  lcd.setCursor(5, 3);
  lcd.print(val3);
  lcd.setCursor(8,1);
  lcd.print("Bst-");
  lcd.setCursor(12,1);
  lcd.print(midBoost);
  lcd.setCursor(8,2);
  lcd.print("Mid-");
  lcd.setCursor(12,2);
  lcd.print(boostMsg);

  stateSw1 = digitalRead(sw1);
  stateSw2 = digitalRead(sw2);
  stateSw3 = digitalRead(sw3);
  stateSw4 = digitalRead(sw4);
  stateSw5 = digitalRead(sw5);
  stateSw6 = digitalRead(sw6);
  stateSw7 = digitalRead(sw7);
  stateSw8 = digitalRead(sw8);
  stateSw9 = digitalRead(sw9);
  stateSw10 = digitalRead(sw10);

  digitalWrite(led1, stateLed1);
  digitalWrite(led2, stateLed2);
  digitalWrite(led3, stateLed3);
  digitalWrite(led4, stateLed4);
  digitalWrite(led5, stateLed5);
  digitalWrite(led6, stateLed6);
  digitalWrite(led7, stateLed7);
  digitalWrite(led8, stateLed8);
  digitalWrite(led9, stateLed9);
  digitalWrite(led10, stateLed10);




  //                                      SWITCH 1
  if (!stateSw1) {
    if (stateSw1 == LOW && stateLed1 == false) {
      stateLed1 = 1;
      stateLed2 = 0;
      stateLed3 = 0;
      stateLed4 = 0;
      stateLed5 = 0;
      stateLed6 = 0;
      stateLed7 = 0;
      stateLed8 = 0;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("01: obZen");
      val1 = pg1ch1;  // The Value from above gets inserted here and also gets displayed correctly
      val2 = pg1ch2;
      val3 = pg1ch3;
      sendValue2 = pg1ch2 - 1;  // this is where I have to reduce the value by 1 so value 40 calls preset 40 ( otherwise preset would be 41 ) The FCB1010 does that too
      sendValue1 = pg1ch1 - 1;
      MIDI.sendProgramChange(val3, 3);
      delay(10);

      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (normLevel), 4);
        boostMsg = 0;
      }
    }
  }
  


  //                                      SWITCH 2
  if (!stateSw2) {
    if (stateSw2 == LOW && stateLed2 == false) {
      stateLed1 = 0;
      stateLed2 = 1;
      stateLed3 = 0;
      stateLed4 = 0;
      stateLed5 = 0;
      stateLed6 = 0;
      stateLed7 = 0;
      stateLed8 = 0;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("02: Psycho Shred");
      val1 = pg2ch1;
      val2 = pg2ch2;
      val3 = pg2ch3;
      sendValue2 = pg2ch2 - 1;
      sendValue1 = pg2ch1 - 1;
      MIDI.sendProgramChange (val3, 3);
      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (boostLevel), 4);
        boostMsg = 1;
      }
    }
  }
    
  
  //                                      SWITCH 3
  if (!stateSw3) {
    if (stateSw3 == LOW && stateLed3 == false) {
      stateLed1 = 0;
      stateLed2 = 0;
      stateLed3 = 1;
      stateLed4 = 0;
      stateLed5 = 0;
      stateLed6 = 0;
      stateLed7 = 0;
      stateLed8 = 0;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("03: Hellfire Solo");
      val1 = pg3ch1;
      val2 = pg3ch2;
      val3 = pg3ch3;
      sendValue2 = pg3ch2 - 1;
      sendValue1 = pg3ch1 - 1;
      MIDI.sendProgramChange (val3, 3);
      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (boostLevel), 4);
        boostMsg = 1;
      }
    }
  }
  //                                      SWITCH 4
  if (!stateSw4 && stateLed4 == false) {
    if (stateSw4 == LOW) {
      stateLed1 = 0;
      stateLed2 = 0;
      stateLed3 = 0;
      stateLed4 = 1;
      stateLed5 = 0;
      stateLed6 = 0;
      stateLed7 = 0;
      stateLed8 = 0;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("04: BLACKHOLE");
      sendValue2 = pg4ch2 - 1;
      sendValue1 = pg4ch1 - 1;
      val1 = pg4ch1;
      val2 = pg4ch2;
      val3 = pg4ch3;
      MIDI.sendProgramChange (val3, 3);
      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (boostLevel), 4);
        boostMsg = 1;
      }
    }
  }
  //                                      SWITCH 5
  if (!stateSw5 && stateLed5 == false) {
    if (stateSw5 == LOW) {
      stateLed1 = 0;
      stateLed2 = 0;
      stateLed3 = 0;
      stateLed4 = 0;
      stateLed5 = 1;
      stateLed6 = 0;
      stateLed7 = 0;
      stateLed8 = 0;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("05: Master Reverb");
      val1 = pg5ch1;
      val2 = pg5ch2;
      val3 = pg5ch3;
      sendValue2 = pg5ch2 - 1;
      sendValue1 = pg5ch1 - 1;
      MIDI.sendProgramChange (val3, 3);
      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (normLevel), 4);
        boostMsg = 0;
     }
    }
  }
  //                                      SWITCH 6
  if (!stateSw6 && stateLed6 == false) {
    if (stateSw6 == LOW) {
      stateLed1 = 0;
      stateLed2 = 0;
      stateLed3 = 0;
      stateLed4 = 0;
      stateLed5 = 0;
      stateLed6 = 1;
      stateLed7 = 0;
      stateLed8 = 0;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("06: STARGAZING");
      val1 = pg6ch1;
      val2 = pg6ch2;
      val3 = pg6ch3;
      sendValue2 = pg6ch2 - 1;
      sendValue1 = pg6ch1 - 1;
      MIDI.sendProgramChange (val3, 3);
      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (boostLevel), 4);
        boostMsg = 1;
      }
    }
  }
  //                                      SWITCH 7
  if (!stateSw7 && stateLed7 == false) {
    if (stateSw7 == LOW) {
      stateLed1 = 0;
      stateLed2 = 0;
      stateLed3 = 0;
      stateLed4 = 0;
      stateLed5 = 0;
      stateLed6 = 0;
      stateLed7 = 1;
      stateLed8 = 0;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("07: Tape Machine");
      val1 = pg7ch1;
      val2 = pg7ch2;
      val3 = pg7ch3;
      sendValue2 = pg7ch2 - 1;
      sendValue1 = pg7ch1 - 1;
      MIDI.sendProgramChange (val3, 3);
      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (boostLevel), 4);
        boostMsg = 1;
      }
    }
  }
  //                                      SWITCH 8
  if (!stateSw8 && stateLed8 == false) {
    if (stateSw8 == LOW) {
      stateLed1 = 0;
      stateLed2 = 0;
      stateLed3 = 0;
      stateLed4 = 0;
      stateLed5 = 0;
      stateLed6 = 0;
      stateLed7 = 0;
      stateLed8 = 1;
      stateLed10 = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("08: BLACKHOLE");
      val1 = pg8ch1;
      val2 = pg8ch2;
      val3 = pg8ch3;
      sendValue2 = pg8ch2 - 1;
      sendValue1 = pg8ch1 - 1;
      MIDI.sendProgramChange (val3, 3);
      if (val1 == 0){
        MIDI.sendControlChange(102,0,1);  //bypass Timeline if value is == 0
      }
      if (val2 == 0){
        MIDI.sendControlChange(02, 127, 02); // bypass Space if value is == 0
      }
      if (val1 >= 1){
      MIDI.sendProgramChange(sendValue1, 1);  // load Preset sendValue1 Timeline
      MIDI.sendControlChange(102, 127, 1); // activate Preset on Timeline
      }
      delay(10);
      if (val2 >= 1){
      MIDI.sendProgramChange(sendValue2, 2);  // loadPreset sendValue2 on Timeline (loading different preset also activates it)
      }
      delay(10);
      if (midBoost == true){
        MIDI.sendControlChange(15, (boostLevel), 4);
        boostMsg = 1;
      }
    }
  }
  //                                              SWITCH 9 // TAP Footswitch
  if (stateSw9 == LOW) {
      stateLed9 = 1;
      tap++;  // increases value while button is pressed
    }
   else{
    stateLed9 = 0;
    tap = 0;  // resets value if button is releases
    }
  
  if (tap == 1){
    MIDI.sendControlChange(93, 127, 1);  // TAP TEMPO TIMELINE
    MIDI.sendControlChange(100, 127,2);  // TAP TEMPO SPACE 
  }

                    //                          SWITCH 10 // HOT SW FOOTSWITCH


if (stateSw10 == LOW) {
      tap2++;  // increases value while button is pressed
    }
   else{
    tap2 = 0;  // resets value if button is releases
  
    }
  
  if (tap2 == 1){
    MIDI.sendControlChange(101, 127,2);  // send CC101 value 127 channel 3
    delay(20);
    stateLed10 = !stateLed10;
    MIDI.sendControlChange(101,0,2);    // send CC101 value 0 channel3
  }

                  

                                                  // MID BOOST SWITCH ACTIVE CODE

if (digitalRead (boostSw) == LOW){
  midBoost = 1;
}
else {
  midBoost = 0;
}


}






Hi @peteccprogrammer,

welcome to the arduino-forum.
Very well done posting code as a code-section in your very first posting.

You should provide some more information.
What microcontroller are you using?
mentioning serial1 serial2 serial3 could be Arduino Mega 2560

mentioning hardwareserial might be ESP32
so what microcontroller are you using?
Solving the problem depends on the type of microcontroller

best regards Stefan

Hi, I‘m using an arduino Mega 2560rev3 and I‘ve built my own midi circuit as per some youtube tutorial. Like I said, other users just hat to replace Serial with Serial1 in the code. But that doesn’t work for me.

Thanks in advance

This is too less information for a detailed analysis what might be the problem.

You should post to what IO-pin number do you have connected your Midi-interface?
Your code says

Does this mean the code does all the code shall do as long as you connected your MIDI-input to TX0?

And as soon as you change the code to

//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); // wont send MIDI out. I'v tried a lot, too, no success

Do you have a logic analyser or an oscilloscope?

Do you know how to make a voltage-follower-circuit with an OP-Amp?
The voltage-follower-circuit could be used as a very basic "sniffer" with an LED on the OpAmps output to check if the serial port does send something at all. = LED flashes

Do you have an extra TTL-to-USB-converter that you could use to read in what your Serial1-port is sending?

best regards Stefan

Hi Stefan, I see you have had some experience with finding errors, much more than I do. Also, you will have seen from the code that I'm still rather fresh with all this and don't know yet what information is needed.

Using the standard "Serial" command, my Midi is connected to TX (pin1) and RX (pin0) on my Mega board. The MIDI output circuit is pretty simple and only requires on 220Ohm resistor ( I believe to drop the voltage to midi spec) and a socket. I'm using various guitar effects pedals as midi receivers and, just to be safe, I use a Midi Monitor app on the computer to actually see what's being sent out to the effects pedals. The Midi Monitor app does not filter anything and puts everything up on my screen even though a pedal might not have to react to a specific midi command that is being sent. I see how an oscilloscope or a voltage follower OP amp LED would be handy. Also, that TTL to USB converter will be a nice addition in the future. Thanks for your hints. For this issue, my gut feeling tells me that the commands will either be sent perfectly fine if it works , or not at all if it doesn't.

The basic command is this (part of an example)

MIDI_CREATE_DEFAULT_INSTANCE();

The above code line sets everything up and My code does all I intended it to do. With my midi socket connected to TX (1) and RX (0) my code still runs wiothout issues.

Then, I changed the first bit of code to this:

MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);

I'm no longer using the default template, but specify what to use. This also works perfectly fine with my socket connected to TX and RX. Only downside still is I can't upload new code to arduino unless I unplug TX pin first. But it works like a charm.

So I connected my midi socket to TX1 (18) and RX1(19) on my Mega 2560 and used this code instead

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

You see, the only difference is that now, "Serial1" is used instead of "Serial". I got this idea from the "DualMerger" Example of the MIDI.h library. Here's the first couple of lines of the example code using "Serial1"

#ifdef ARDUINO_SAM_DUE
    MIDI_CREATE_INSTANCE(HardwareSerial, Serial,     midiA);
    MIDI_CREATE_INSTANCE(HardwareSerial, Serial1,    midiB);
#elif defined(ARDUINO_SAMD_ZERO)
    MIDI_CREATE_INSTANCE(HardwareSerial, SerialUSB,  midiA);
    MIDI_CREATE_INSTANCE(HardwareSerial, Serial1,    midiB);
#else
    #include <SoftwareSerial.h>
    SoftwareSerial softSerial(2,3);
    MIDI_CREATE_INSTANCE(HardwareSerial, Serial,     midiA);
    MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB);
#endif

Now, pasting the code I noticed that my Mega 2560rev3 board might not be compatible with using "Serial1" of this library. The example would have used "software serial" for this. Interesting, but also confusing.

Do you think this could be the issue?

BTW, thanks for making me think about it!! This sometimes helps just as much.

Have you checked that the pins used for the extra serial ports are not being used for other purposes in your code?

Hi Grumpy_Mike,

just now I have double checked. My code does not use the pins required for TX1 or RX1. I'm unsure if the MIDI.h library somehow uses them. I did run into an issue with DFmidi MP3 players not accepting Serial1 until I declared the TX1 pin as an POUTPUT and RX1 as INPUT_PULLUP. Bus I'Ve tried that also, to no avail.

An arduino Mega has three hardware serial ports
If you look at this pinout of the arduino Mega 2560


what is named Tx and what is named Rx is a matter of perspektive
image
So give changing Rx/Tx a test

a simple 220-ohm resistor is not always enough please post a picture of the circuit

The picture you posted is of an arduino clone. They are known to have the pinouts labelled incorrectly. Notice how on an original arduino TX and RX always follow in the same order. Your pic has Tx2 Rx2 Rx1 Tx1, so the labelling is wrong. Nonetheless, I've tried swapping wires around. No change unfortunately.

Here's my circuit:


analysing what is the problem is much easier of you have the following equipment
so you should answer these questions:

  • do you have an oscilloscope?

  • do you have a logic analyser?

  • do you have a extra TTL-to-USB converter (= a virtual COM-Port-adapter)

  • do you have a OPAmp like LM358 LM324 or something similar?

good pictures of your real setup. Taken from almost vertcial above high resolution and sharp
nonetheless a schematic would tell more and is much easier to read
I googled for MIDI input circuit

Most of them use this optocoupler type 6N138 with photo-darlington transistor

Does your input circuit look like that?

best regards Stefan

Hi Stefan,

I do not have an ocilloscope,
I do not have a logic analyser
I do not have a TTL to USB thingy
I do not have an OPAmp

That midi input circuit is exactly how I have connected everything.!!

There is a midi output circuit also that is not part of your picture, but since it works on Serial, I do not assume it is the error.
IMG_0982

But it does use the pins for TX1 and RX1

pinMode (18, OUTPUT);
pinMode (19, INPUT);

I totally overlooked that. Must have been a moment when I thought declaring them as I/O solves the issue. It did not. And the two lines got removed. Good eye though!!

A 24 MHz 8 Ch logic analyser is very useful for analysing digital signals.
A logic analyser can only "measure" LOW or HIGH nothing inbetween.

But they are pretty cheap:

on ebay you can find them for $15 or $10
They all work with a software called sigrok pulseview

You can record digital signals going LOW/HIGH over time and even do protocol interpretation of the bitbanging.

OpAmps like an LM358 can be used to "sniff" signal levels without influencing the signal.
because wired as a voltage-follower the current "pulled" from the sniffed IO-pin is extremely low = no influence on the signal.

best regards Stefan

Thanks, but I can't take any credit, for my eyes at least.

I loaded the code into the Arduino IDE and then did a search for 16: then 17 then 18 and finally 19. First two came up with nothing, second two found something but only one instance of the use of that number.

Ok here is some code for sending random notes on a Mega on serial port one.

char midiChannel = 0; // channel to send note on
void setup() {
  Serial1.begin(31250);
  delay(1000); // to allow things to settle down
}

void loop() {
  char mynote = random(20,127);
  noteSend(0x90, mynote, 90); // send note on with velocity 90
  delay(300);
  noteSend(0x80, mynote, 0);  // send note off
  delay(1000);
}

//  plays a MIDI note
 void noteSend(char cmd, char data1, char data2) {
  cmd = cmd | char(midiChannel);  // merge channel number
  Serial1.write(cmd);
  Serial1.write(data1);
  Serial1.write(data2);
}

Is this how you are using the serial ports in your code?

Hi Grumpy_Mike,

It's not exactly how I use the Serials in my code, but I thought I'd gio back to the very very basic MIDI out code.

#include <MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);  // works
//MIDI_CREATE_INSTANCE(Hardware.Serial, Serial1, MIDI);      // doesn't work

void setup() {
  // put your setup code here, to run once:
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MIDI.sendProgramChange(1, 1);
}

void loop() {
  // put your main code here, to run repeatedly:
}

This sends a program change and works, as long as "Serial" is used the other row where I use "Serial1" wont output and midi. it must be something really really stupid, I'm sure :slight_smile:

Maybe something worth noting is, that it only transmitts MIDI, when both, TX and RX are connected properly
MIDI_MONSTER.sxe - schematic.pdf (51,9 KB)
. Now, the RX wire is connected to an opto-isolator with darlington and also connected to 5v using a 220Ohm resistor. If I remove the opto-isolator from the breadboard, it still works. I think it needs the 5v with the 220Ohm resistor connected to be operational. Normally the RX wire shouldn't have an impact, because I'm not reading any midi, only sending (as per now)

I disconnected RX wire. It wont send midi out through TX.
I connected RX to 5V. It won't send midi out through TX.
I connected RX to GND, it won't send midi out through TX
I connect RX to 5V with a 220Ohm resistor. Works perfectly! Isn't this weird?!?

Ok, this goes quick now.

I connected my midi in wire to pin 19(rx1) and my midi out wire to pin18(tx1) , changed "Serial" to "Serial1" in the code above and nothing worked, as expected.

Went back to "Serial" (or CREATE_DEFAULT_INSTANCE(); doesn't matter which) and connected both wires to pin0 (rx) and pin1(tx) and everything worked, just as expected.

Here's whats interesting.

I went back to using "Serial1" in the code line but only moved the TX wire from pin1(tx) to pin18(tx1)....the rx wire is still connected to pin0(rx). AND IT WORKS!!!
If I connect my RX wire to pin19(rx1) it stops working. That is, it stops sendiong midi.
Not where it wants to receive midi now, but that's a thing for later.

How crazy is that?!?

Not crazy. Just some hardware that is wired wrong or some circuitry that does not comply with the midi-specification.

You should have at least a digital multimeter for analysing such problems.
Do you have a digital multimeter?
Do you know how to measure voltage ?
Do you know how to measure current?

measuring current is done completely different than measuring voltages
And if you do it wrong you create a shortcut that might destroy the microcontroller and / or
blow the internal fuse of the digital multimeter.

This means you have to learn it how to measure current before really doing it.

best regards Stefan