Troubleshooting trying to get MIDI out to work..

Status
Not open for further replies.

rusty113

Member
I want to send Midi data through a 5-pin Din connector with Teensy LC. I attached a 47 ohm resistor 0.25w 5% on both pin 1(TX) and 3.3v. And i have ground connected to the middle DIN pin (have tested it, ground is there) I have double checked the schematic as to which pins on the DIN connector to connect them to and they seem to be accurate, like the pins aren't wrongly attached... I was thinking maybe 5% resistance might be the problem, like its too much? But I wanted to ask to see if someone knew. I tried using the example BASIC_IO from the arduino MIDI library and testing with a midi cable connected to an instrument capable of receiving midi (2 different instruments actually, which i have tested and they can receive midi sent from each other so I know the problem isnt on their side, but with the DIN jack or maybe something else on the Teensy side). I was unsuccessful using the example i also tried the one on the Midi Arduino page, unsuccessful as well.
Does any 5-pin DIN connector work as a Midi jack or is there a specific kind?

The code is for a midi bass pedal project (another one). I want the Teensy to be able to send both Midi from the DIN connector and usbMidi. (usbMidi works, have tested, just not the DIN midi):

HTML:
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

#define DEBOUNCE 1500

struct key
{
  int pin;
  int midiKey;
  int debounce;

};

struct key keys[] =
{
  { 2, 24, 0 },  // C  brown
  { 3, 25, 0 },  // Db red
  { 4, 26, 0 },  // D  red
  { 5, 27, 0 },  // Eb orange
  { 6, 28, 0 },  // E  orange
  { 7, 29, 0 },  // F  yellow
  { 8, 30, 0 },  // Gb green
  { 9, 31, 0 },  // G  green
  { 10, 32, 0 },  // Ab blue
  { 11, 33, 0 },  // A  blue
  { 12, 34, 0 },  // Bb violet
  { 13, 35, 0 },  // B  violet
  { A0, 36, 0 },  // high C  brown
  { 0, 0, 0 }     // end of list marker
};

const int octaveUp_button_pin = A2;
const int octaveDown_button_pin = A3;
const int velpin = A1;

int octave = 0;
int octave_prev = 0;
int octaveUp_Button_Voltage = 0;
int octaveDown_Button_Voltage = 0;
int last_octaveUp_Button_Voltage = 0;
int last_octaveDown_Button_Voltage = 0;


int time1;
int time2;
int interval;
bool velpin_state = {true};
bool velpin_prev_state = {true};

int channel = 3;


void setup() {
  // put your setup code here, to run once:
  for (int i = 0; keys[i].pin != 0; ++i)
  {
    pinMode(keys[i].pin, INPUT_PULLUP);
  }
  pinMode(octaveUp_button_pin, INPUT_PULLUP);
  pinMode(octaveDown_button_pin, INPUT_PULLUP);
  pinMode(velpin, INPUT_PULLUP);
  MIDI.begin(3);
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Setup complete");
  
  millis();
}

void noteOn(int midiKey)
{
  Serial.print("Note On: ");
  Serial.println(midiKey);
}

void noteOff(int midiKey)
{
  Serial.print("Note Off: ");
  Serial.println(midiKey);
  MIDI.sendNoteOff(midiKey + octave, 0, channel);
  usbMIDI.sendNoteOff(midiKey + octave, 0, channel);
}

void loop() {
  // put your main code here, to run repeatedly:
  //determine octave
  if (octave > octave_prev) {
    //    midiInst.sendControlChange(AllNotesOff, 0,  channel);
    for (int i = 0; keys[i].pin != 0; ++i) {
      noteOff(keys[i].midiKey + octave - 12);


    } octave_prev = octave;
  } else {

    if (octave < octave_prev) {
      //    midiInst.sendControlChange(AllNotesOff, 0,  channel);
      for (int i = 0; keys[i].pin != 0; ++i) {
        noteOff(keys[i].midiKey + octave + 12);

      } octave_prev = octave;
    } else {


      octaveUp_Button_Voltage = digitalRead(octaveUp_button_pin);

      // compare the buttonState to its previous state
      if (octaveUp_Button_Voltage != last_octaveUp_Button_Voltage) {
        // if the state has changed, increment the counter
        if (octaveUp_Button_Voltage == LOW) {
          // if the current state is HIGH then the button went from off to on:

          if (octave < 24) {


            octave += 12;
            Serial.println("Octave Up +1 ");
            Serial.print("(Transpose + ");
            Serial.print(octave);
            Serial.println(")");
          }
        } else {
          // if the current state is LOW then the button went from on to off:

        }
        // Delay a little bit to avoid bouncing
        delay(50);
      }
      // save the current state as the last state, for next time through the loop
      last_octaveUp_Button_Voltage = octaveUp_Button_Voltage;





      // read the pushbutton input pin 2:
      octaveDown_Button_Voltage = digitalRead(octaveDown_button_pin);

      // compare the buttonState to its previous state
      if (octaveDown_Button_Voltage != last_octaveDown_Button_Voltage) {
        // if the state has changed, increment the counter
        if (octaveDown_Button_Voltage == LOW) {
          // if the current state is HIGH then the button went from off to on:

          if (octave > -24) {

            octave -= 12;
            Serial.println("Octave Down -1 ");
            Serial.print("Transpose + ");
            Serial.print(octave);
            Serial.println(")");

          }
        } else {
          // if the current state is LOW then the button went from on to off:

        }
        // Delay a little bit to avoid bouncing
        delay(50);
      }
      // save the current state as the last state, for next time through the loop
      last_octaveDown_Button_Voltage = octaveDown_Button_Voltage;



      int value;
      for (int i = 0; keys[i].pin != 0; ++i)
      {
        value = digitalRead(keys[i].pin);
        if (keys[i].debounce == 0) // Key has been off
        {
          if (value == HIGH)       // Key is now on
          {
            time1 = millis();
            Serial.println(time1);

            keys[i].debounce = DEBOUNCE;  // Set the note off debounce counter


          }
        }
        else                      // Key has been on
        {
          if (value == LOW)      // Key has gone off
          {
            if (--keys[i].debounce == 0) // If Key has remained off for DEBOUNCE scans,
              noteOff(keys[i].midiKey + octave);
                           
          }
          else                    // Key has not gone off
            keys[i].debounce = DEBOUNCE;  // Reset debounce counter in case we got
          // a small number of key off scans





          velpin_state = digitalRead(velpin);

          if (velpin_state != velpin_prev_state)
          {

            if (velpin_state == LOW)

            {
              time2 = millis();
              Serial.println(time2);
              interval = time2 - time1;
              Serial.print("interval: ");
              Serial.println(interval);
              velocity = map(interval, 0, 700, 127, 0);
              if (velocity < 0) {
                velocity = 0; }
              Serial.print("Velocity = ");
              Serial.println(velocity);
              noteOn(keys[i].midiKey + octave);      // Send the MIDI note on message
              MIDI.sendNoteOn(keys[i].midiKey + octave, velocity, channel);
              usbMIDI.sendNoteOn(keys[i].midiKey + octave, velocity, channel);
            }
          }
          velpin_prev_state = velpin_state;
        }
      }
    }
  }
}
 
I don't know what's wrong, but I can try to quickly answer these 2 questions.

I was thinking maybe 5% resistance might be the problem, like its too much? But I wanted to ask to see if someone knew.

5% is perfectly fine.


Does any 5-pin DIN connector work as a Midi jack or is there a specific kind?

Any connector which mates properly and reliably connects to the 2 pins needed by MIDI should work.

Even though you've double checked everything, sharing photos here might help. If we can see how you really connected everything, maybe someone will notice any issue?

About 1 week ago there was a similar MIDI question. Looking at the photo, it turned out the resistor color wasn't right and it was actually a pair of 47K resistors instead of 47 ohm. Misunderstanding about which 2 pins to use on the DIN connector are a common problem. It's really easy to get them swapped if you look at a diagram for the front side while soldering wires to the back side.
 
Which pin are you using as the MIDI output? The MIDI library defaults to using the Serial1 Tx pin (pin 1). But also, that basic example will only send a midi note if it receives one.
Try this code which continuously plays a midi note for one second and then off for one second - I know this works.
Code:
#include <MIDI.h>

// Simple tutorial on how to receive and send MIDI messages.
// Here, when receiving any message on channel 4, the Arduino
// will blink a led and play back a note for 1 second.

MIDI_CREATE_DEFAULT_INSTANCE();

static const unsigned ledPin = 13;      // LED pin on Arduino Uno

void setup()
{
    pinMode(ledPin, OUTPUT);
    MIDI.begin(4);                      // Launch MIDI and listen to channel 4
}

void loop()
{
//    if (MIDI.read())                    // If we have received a message
//    {
        digitalWrite(ledPin, HIGH);
        MIDI.sendNoteOn(42, 127, 1);    // Send a Note (pitch 42, velo 127 on channel 1)
        delay(1000);		            // Wait for a second
        MIDI.sendNoteOff(42, 0, 1);     // Stop the note
        digitalWrite(ledPin, LOW);
        delay(1000);
//    }
}

Pete
 
I now have 47 ohm resistors on both non-ground pins on the midi/din connector. Still not receiving any MIDI data...I tried the code you posted too. I have received usbMIDI from the Teensy just not regular midi from the din connector...another thing I thought is: the 3.3v pin i am using is the one next to pin 23(A9). Could that be the reason?

Photo on 11-23-19 at 4.42 AM #2n.jpgPhoto on 11-23-19 at 4.43 AM.jpg
 
Are these pictures reversed from your webcam, based on the resistor photo it looks like it. If so then you have it wired backwards, here’s a photo from sparkfun that may help your orientation:
F7441974-0041-420F-9196-764F66C81AFE.png
Pin 5 is TX
Pin 4 is 3.3v
Pin 2 is shield/ground
 
sorry that photo made things more confusing. I have tried them both ways...currently have them attached to the pins indicated by the sparkfun photo.
 
@rusty113
I could be wrong but looking at your first picture it seems your didn't solder the wires to the resistors. If so, how do you expect it to work?
 
that looks useful. Did you make it?
I thought that the wires just needed to be making contact for it to work...i didn't solder it yet because i am planning on putting it in a enclosure
 
That would be especially useful if it had a bi-color LED so it would light up a different color when it’s wired backwards, the circuitry is the exact same that you already have built just using a bi-color LED like this:
9EADE752-34C1-4EE5-A35A-616985B62659.png
 
As long as your wires have a solid connection you can get away without soldering for prototyping, I typically use alligator clips so I know the connection has some strength to it. If your Teensy is on a breadboard and you have an LED it’s easy enough to connect it straight to the TX pin to verify activity, you could also do a simple loop back test by connecting TX to RX on the Teensy without any other circuitry.
 
I thought that the wires just needed to be making contact for it to work...i didn't solder it yet because i am planning on putting it in a enclosure
Physical contact does not mean electrical contact. There might be some glue residue from the paper tape at the extremities of the resistor legs.
 
Got it to work!
After soldering everything together I found a thread on this forum about someone also having trouble getting hardware midi to work that they eventually found a solution to.
It turned out I had to specify that i wanted to use serial1 on the Teensy by including this line of code:

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); // 5 Pin DIN Out

Before i had: MIDI_CREATE_DEFAULT_INSTANCE();
And after changing it, i was receiving midi input from a midi cable plugged into the 5 Pin Din wired to the Teensy.

So now the full working code for a (clunky) monophonic, velocity sensitive, 13-note midi pedal board with octave switch buttons and supporting hardware and usb midi using the Teensy is this:


Code:
#include <MIDI.h>


#define HWSerial Serial1
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); // 5 Pin DIN Out


#define DEBOUNCE 20

struct key
{
  int pin;
  int midiKey;
  int debounce;

};

struct key keys[] =
{
  { 2, 36, 0 },  // C  brown
  { 3, 37, 0 },  // Db red
  { 4, 38, 0 },  // D  red
  { 5, 39, 0 },  // Eb orange
  { 6, 40, 0 },  // E  orange
  { 7, 41, 0 },  // F  yellow
  { 8, 42, 0 },  // Gb green
  { 9, 43, 0 },  // G  green
  { 10, 44, 0 },  // Ab blue
  { 11, 45, 0 },  // A  blue
  { 12, 46, 0 },  // Bb violet
  { 13, 47, 0 },  // B  violet
  { A0, 48, 0 },  // high C  brown
  { 0, 0, 0 }     // end of list marker
};

int keyOffset = 0;

const int octaveUp_button_pin = A2;
const int octaveDown_button_pin = A3;
const int velpin = A1;

int octave = 0;
int octave_prev = 0;
int octaveUp_Button_Voltage = 0;
int octaveDown_Button_Voltage = 0;
int last_octaveUp_Button_Voltage = 0;
int last_octaveDown_Button_Voltage = 0;




// time variables
unsigned long scantime;
unsigned long time_now;
unsigned long time_prev;
unsigned long vel_start[13] = {0};
unsigned long vel_time;



// varialble for velocity calculation
int velocity;

int time1;
int time2;
int interval;
bool velpin_state = {true};
bool velpin_prev_state = {true};

int channel = 3;


void setup() {
  // put your setup code here, to run once:
  for (int i = 0; keys[i].pin != 0; ++i)
  {
    pinMode(keys[i].pin, INPUT_PULLUP);
  }
  pinMode(octaveUp_button_pin, INPUT_PULLUP);
  pinMode(octaveDown_button_pin, INPUT_PULLUP);
  pinMode(velpin, INPUT_PULLUP);
  MIDI.begin(3);
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Setup complete");

  millis();
}

void noteOn(int midiKey)
{
  Serial.print("Note On: ");
  Serial.println(midiKey);
}

void noteOff(int midiKey)
{
  Serial.print("Note Off: ");
  Serial.println(midiKey);
}

void loop() {
  // put your main code here, to run repeatedly:
  //determine octave
  if (octave > octave_prev) {
    //    midiInst.sendControlChange(AllNotesOff, 0,  channel);
    for (int i = 0; keys[i].pin != 0; ++i) {
      noteOff(keys[i].midiKey + octave - 12);
      usbMIDI.sendNoteOff(keys[i].midiKey + octave - 12, 0, channel);
      MIDI.sendNoteOff(keys[i].midiKey + octave - 12, 0, channel);

    } octave_prev = octave;
  } else {

    if (octave < octave_prev) {
      //    midiInst.sendControlChange(AllNotesOff, 0,  channel);
      for (int i = 0; keys[i].pin != 0; ++i) {
        noteOff(keys[i].midiKey + octave + 12);
        usbMIDI.sendNoteOff(keys[i].midiKey + octave + 12, 0, channel);
        MIDI.sendNoteOff(keys[i].midiKey + octave + 12, 0, channel);

      } octave_prev = octave;
    } else {


      octaveUp_Button_Voltage = digitalRead(octaveUp_button_pin);

      // compare the buttonState to its previous state
      if (octaveUp_Button_Voltage != last_octaveUp_Button_Voltage) {
        // if the state has changed, increment the counter
        if (octaveUp_Button_Voltage == LOW) {
          // if the current state is HIGH then the button went from off to on:

          if (octave < 24) {


            octave += 12;
            Serial.println("Octave Up +1 ");
            Serial.print("(Transpose + ");
            Serial.print(octave);
            Serial.println(")");
          }
        } else {
          // if the current state is LOW then the button went from on to off:

        }
        // Delay a little bit to avoid bouncing
        delay(50);
      }
      // save the current state as the last state, for next time through the loop
      last_octaveUp_Button_Voltage = octaveUp_Button_Voltage;





      // read the pushbutton input pin 2:
      octaveDown_Button_Voltage = digitalRead(octaveDown_button_pin);

      // compare the buttonState to its previous state
      if (octaveDown_Button_Voltage != last_octaveDown_Button_Voltage) {
        // if the state has changed, increment the counter
        if (octaveDown_Button_Voltage == LOW) {
          // if the current state is HIGH then the button went from off to on:

          if (octave > -24) {

            octave -= 12;
            Serial.println("Octave Down -1 ");
            Serial.print("Transpose + ");
            Serial.print(octave);
            Serial.println(")");

          }
        } else {
          // if the current state is LOW then the button went from on to off:

        }
        // Delay a little bit to avoid bouncing
        delay(50);
      }
      // save the current state as the last state, for next time through the loop
      last_octaveDown_Button_Voltage = octaveDown_Button_Voltage;


    }
  }
  int value;
  for (int i = 0; keys[i].pin != 0; ++i)
  {
    value = digitalRead(keys[i].pin);
    if (keys[i].debounce == 0) // Key has been off
    {
      if (value == HIGH)       // Key is now on
      {
        time1 = millis();
        Serial.println(time1);

        keys[i].debounce = DEBOUNCE;  // Set the note off debounce counter


      }
    }
    else                      // Key has been on
    {
      if (value == LOW)      // Key has gone off
      {
        if (--keys[i].debounce == 0) // If Key has remained off for DEBOUNCE scans,
          noteOff(keys[i].midiKey + octave);
        usbMIDI.sendNoteOff(keys[i].midiKey + octave, 0, channel);
        MIDI.sendNoteOff(keys[i].midiKey + octave, 0, channel);
      }
      else                    // Key has not gone off
        keys[i].debounce = DEBOUNCE;  // Reset debounce counter in case we got
      // a small number of key off scans





      velpin_state = digitalRead(velpin);

      if (velpin_state != velpin_prev_state)
      {

        if (velpin_state == LOW)

        {
          time2 = millis();
          Serial.println(time2);
          interval = time2 - time1;
          Serial.print("interval: ");
          Serial.println(interval);
          velocity = map(interval, 0, 100, 127, 0);
          if (velocity < 0) {
            velocity = 1;
          }
          Serial.print("Velocity = ");
          Serial.println(velocity);
          noteOn(keys[i].midiKey + octave);      // Send the MIDI note on message
          MIDI.sendNoteOn(keys[i].midiKey + octave, velocity, channel);
          usbMIDI.sendNoteOn(keys[i].midiKey + octave, velocity, channel);
        }
      }
      velpin_prev_state = velpin_state;
    }
  }
}

(the reason why i say it is clunky is because if i press another pedal while already holding one, it will not send data (which is okay for me) but the clunky part is that i have to fully release the pedal playing before pressing another one or the next one will not play)
 
I don't think you're going to get help on this issue with this code at the tail end of a thread on DIN MIDI out problems.

This code seems a bit unusual in how the octave feature works and how it seems integrated with the note on/off logic.

If it's from some source you've altered it might help to note/link to the source of the code as well.


...but maybe in a new thread if you're looking for help on what is an unrelated issue.:D
 
Status
Not open for further replies.
Back
Top