Trouble sending 5-pin Midi on Leonardo

Hello, I am new here, looking for help. I have built a basic 5-Pin Midi output setup with the Leonardo with some buttons, potis, distance measurement, display. The hardware, except for the Midi part, is working fine, meaning I can see all the values on the display. Midi out is connected to my audio interface, which monitors to the computer. If I connect the Interface's in to my Midi-keyboards out, it monitors fine. I want to trigger synths later, the monitoring on the computer is just a test. Here is the ruff schematic, leaving some stuff out but to give you an overview:

My problem is: I can not output any Midi. I have tried using different serials. The serial is working fine for monitoring serial prints at the monitor, but tx pin with Serial2.write does not send any Midi. I have tried using the Midi Library as well as the controller.h approach but it just wont output. Since i am getting a bit frustrated, I kindly ask for help. I use Visual Studio Code, but have tried the Arduino IDE as well. The code contains some outcommented serial prints, they work fine if I do Serial.begin(115200), that is not the problem. But the outcommented Serial1.write(...) did not work. This is the code:

#include <Arduino.h>
#include <MIDI.h>
#include <U8g2lib.h>
#include <Wire.h>

#define SDA 2
#define SCL 3
#define echoTrig A5
#define echoReturn A4
#define dice1 5
#define dice2 7
#define dice1Led 4
#define dice2Led 6

U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); 	
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

int i,j;
int pots[4] = {A3,A2,A1,A0};    // POT pins
int buts[6] ={8,9,10,11,12,13}; // BUT pin
const int numpots = sizeof(pots)/sizeof(pots[0]); // amount of potis
const int numbuts = sizeof(buts)/sizeof(buts[0]); // amount of buttons    
int potvals[numpots];     	    // POT value array
int butvals[numbuts];           // BUT value array
int distval;                    // Distance value
int yoff = 30;                  // screen Y-offset
int rowheight = 11;             // screen row height
int xcolpots = 55;              // x-pixels second column POT
int xcolbuts = 15;              // x-pixels columns BUT
int midival;                    // MIDI value

void show(int potvals[], int butvals[], int distval){    
  u8g2.setFont(u8g2_font_t0_11b_tf);
  u8g2.firstPage();
  do {
    for (i=0; i<numbuts; i++){
      if(butvals[i] == 0){
        u8g2.setCursor(2 + xcolbuts*i, yoff + numpots/2 * rowheight);
        u8g2.print(i+1);
        // Serial.print("But "); Serial.print (i+1); 
        // Serial.print(" / Value: "); Serial.print(butvals[i]);
        // Serial.print(" | ");
        // Serial.println();      
      }
    }
    j = 0;
    for (i=0; i<numpots/2; i++){
      u8g2.setCursor(2, yoff + i*rowheight);
      u8g2.print(F("P")); 
      u8g2.setCursor(8, yoff + i*rowheight);
      u8g2.print(j+1); 
      u8g2.setCursor(23, yoff + i*rowheight);  	
      u8g2.print(potvals[j]); j++;
      // Serial.print("Pot "); Serial.print (j+1); 
      // Serial.print(" / Value: "); Serial.print(potvals[j]);
      // Serial.print(" | ");
      u8g2.setCursor(2+xcolpots, yoff + i*rowheight);
      u8g2.print(F("P"));
      u8g2.setCursor(8+xcolpots, yoff + i*rowheight);
      u8g2.print(j+1); 
      u8g2.setCursor(23+xcolpots, yoff + i*rowheight);
      u8g2.print(potvals[j]); j++;
      // Serial.print("Pot "); Serial.print (j+1); 
      // Serial.print(" / Value: "); Serial.print(potvals[j]);
      // Serial.print(" | ");
      // Serial.println();
    }
    if(distval < 50){
      u8g2.setCursor(2, yoff - rowheight);
      u8g2.print(F("DIS")); 
      u8g2.setCursor(23, yoff - rowheight);
      u8g2.print(distval); 
    }
  } while( u8g2.nextPage() );
}
void potbuts(int potvals[], int butvals[]){
  for (i = 0; i<numpots; i++){
    int tmp = analogRead(pots[i]);
    midival = map(tmp,13,1020,0,127);
    // Serial.print("Pot "); Serial.print (i+1); 
    // Serial.print(" / Value: "); Serial.print(midival);
    // Serial.print(" | ");
    potvals[i] = midival;
  }
  for (i = 0; i<numbuts; i++){
    int butvalue = digitalRead(buts[i]);
    butvals[i] = butvalue;
    if(butvalue == 0){
      int note = 60;
      int velo = 100;
      int chan = 1;
      MIDI.sendNoteOn(note, velo, chan);  
      // Serial1.write(144); //noteOn channel 1
      // Serial1.write(42);  //pitch
      // Serial1.write(127); //velocity
      TXLED1;
      //show(potvals, butvals, distval);
      digitalWrite(dice2Led, HIGH);
      delay(100);		                
      TXLED0;
      MIDI.sendNoteOff(note, 0, chan); 
      //note OFF
      // Serial1.write(144);
      // Serial1.write(42);
      // Serial1.write(0);
      digitalWrite(dice2Led, LOW);
    }
  }
}
// DISTANCE MEASUREMENT
void distance(int *distval){   
  long duration;
  digitalWrite(echoTrig, LOW);
  delayMicroseconds(2);
  digitalWrite(echoTrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(echoTrig, LOW);
  duration = pulseIn(echoReturn, HIGH);
  *distval = (duration/2) / 29.1;
}
//SETUP
void setup(){
  u8g2.begin();
  u8g2.setFlipMode(0);
  MIDI.begin(MIDI_CHANNEL_OFF); 
  // Serial.begin(115200);
  // Serial1.begin(31250);
  // while (!Serial1) {
  //   ; // wait for serial port to connect. Needed for Leonardo only
  // }
  pinMode(SDA, OUTPUT);
  pinMode(SCL, OUTPUT);
  for (int i = 0; i<4; i++)
  {
    pinMode(pots[i], INPUT_PULLUP);
  }
  for (int i = 0; i<6; i++)
  {
    pinMode(buts[i], INPUT_PULLUP);
  }  
  pinMode(echoTrig, OUTPUT);
  pinMode(echoReturn, INPUT);
  pinMode(dice1, INPUT);
  pinMode(dice2, INPUT);
  pinMode(dice1Led, OUTPUT);
  pinMode(dice2Led, OUTPUT);
}
//LOOOP
void loop(){
  potbuts(potvals, butvals);
  distance(&distval);
  show(potvals, butvals, distval);
}

It just outputs the values of potis, knobs and distance on the display. It should just send a MIDI.noteOn if a button is pressed for testing.
Any help is appreciated. Thank you very much.

You have some important stuff "commented out", yes?.
cf.

void setup(){
  u8g2.begin();
  u8g2.setFlipMode(0);
  MIDI.begin(MIDI_CHANNEL_OFF); 
  Serial.begin(115200);
  Serial1.begin(31250);
  while (!Serial1) 
{
  //   ; // wait for serial port to connect. Needed for Leonardo only
}

Thank you for inspecting! I was trying out different ways to use the serial connection. Actually using MIDI.begin() should start the Serial1 on Leonardo as I understood from learning the library. I defined the HardwareSerial to Serial1. Unfortunately this does not seem to be the trick and doesnt have any effect on the Midi output. Do I have to do the Serial1.begin() while using the Midi Library? I ran the code again with the lines above but unfortunately the output has not changed.

Have you MIDI'd with the Leonardo, something simple to send a note (repeatedly), nothing complicated (no pots, etc.)?

I have not tried USBMidi, but regarding the 5pin only on TX, I was not able to get any output, no matter how much I strip the script and ignore the inputs. I apologize for posting the whole thing, actually you are right, I want to output anything, could have been a smaller code sample. I was not worrying about the buttons and pots because they work fine. If I vary the delay of sent messages, I can read different voltages measuring the TX pin (between 2V with 1 ms delay and 4.9V with 1-4s loops. So it actually seem to output "something", might be midi messages, but the monitor can not read them, so they might not be midi messages :thinking:. Thank you for your help.

Ok, wow. After 3 days of not getting a note out of this thing, your advice to simple down the setup made me even just try to directly connect TX and VCC to the Midi out, without the resistors. And suddenly it works fine! I have seen these resistors in most tutorials about building Midi outputs. Is there any damage, that can be done by leaving them out?
For anoyone else who is having similar troubles, I can now certainly say, that
MIDI_CREATE_DEFAULT_INSTANCE();
in combination with
MIDI.begin(MIDI_CHANNEL_OFF);
is enough to start the serial connection on Leonardo's TX pin, using Midi.h. So there is no need for Serial1.begin(31250). Till now, I was not sure about this. My question remains: do I need those two 220Ohm resistors in lines of VCC and TX for safety reasons? It works perfectly without now. Thank you @runaway_pancake for your smart keep-it simple-advice!

Now I found out, they were 220k. Of course it didn't send anything. Works fine with 220. That was a stupid mistake and I didn't check twice. All good now, I am within the Midi specs and everything is working as it should. Thanks anyways!

Nothing works without the fundamentals.

word.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.