Teensy 4.1 and Serial Midi Output

Peetem

Member
I built a Midi controller using a Teensy 4.1 using USB output to control one of my midi devices. I would now like to add Midi control using the serial3 output. I have written the following script - buttons are dedicated to sending a Midi CC signal (via USB and work perfectly). I have also wired a 5-pin din Midi output as follows:

Arduino pin 14 ----> 47 ohm resistor ----------> pin 5 of the Midi din connector
Arduino pin +5v pin ----> 47 ohm resistor ----> pin 4 of the Midi din connector
Arduino And -------------------------------------> pin 2 of the Midi din connector

Four buttons send Control Change (CC) signals - works perfectly.

However, I cannot get the four (4) Program Change (PC) buttons to work (sending their signal to Serial3 output). The PC buttons also send a CC signal, but that doesn't matter - my device ignores those.

Here's the script (yes, I know its kinda sloppy - I'm new to this :D ). Any advice? Thanks!

Code:
#include <Bounce.h>
#include <MIDIUSB.h>

const int NUM_OF_BUTTONS = 8;
const int MIDI_CHAN = 1;
const int DEBOUNCE_TIME = 40;

Bounce buttons[NUM_OF_BUTTONS + 1] =
{
  Bounce(0, DEBOUNCE_TIME),
  Bounce(1, DEBOUNCE_TIME),
  Bounce(2, DEBOUNCE_TIME),
  Bounce(3, DEBOUNCE_TIME),
  Bounce(4, DEBOUNCE_TIME),
  Bounce(5, DEBOUNCE_TIME),
  Bounce(6, DEBOUNCE_TIME),
  Bounce(7, DEBOUNCE_TIME),
  Bounce(8, DEBOUNCE_TIME)
};

const int MIDI_MODE_NOTES = 0;
const int MIDI_MODE_CCS = 1;
int midiMode = MIDI_MODE_NOTES;

const int MIDI_NOTE_NUMS[NUM_OF_BUTTONS] = {7, 1, 3, 43, 5, 37, 38, 39};
const int MIDI_NOTE_VELS[NUM_OF_BUTTONS] = {110, 110, 110, 110, 110, 110, 110, 110};
const int MIDI_CC_NUMS[NUM_OF_BUTTONS] = {7, 1, 3, 27, 5, 21, 22, 23};
const int MIDI_CC_VALS[NUM_OF_BUTTONS] = {127, 127, 127, 127, 127, 127, 127, 127};

bool running = false;
bool toggle_state[NUM_OF_BUTTONS] = {false, false, false, false, false, false, false, false};
bool pc_sent[NUM_OF_BUTTONS] = {false, false, false, false, false, false, false, false};

void setup()
{
  Serial.begin(9600); // Initialize the Serial monitor
  Serial3.begin(9600); // Initialize Serial3

  for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
  {
    pinMode(i, INPUT_PULLUP);
  }
}

void loop()
{
  for (int i = 0; i < NUM_OF_BUTTONS; i++)
  {
    buttons[i].update();
  }

  for (int i = 0; i < NUM_OF_BUTTONS; i++)
  {
    if (buttons[i].fallingEdge())
    {
      Serial.print("Button ");
      Serial.print(i + 1);
      Serial.println(" pressed.");

      if (i == 0 || i == 1 || i == 2 || i == 4) // Buttons 1, 2, 3, 5
      {
        if (!pc_sent[i])
        {
          pc_sent[i] = true;
          sendProgramChange(MIDI_NOTE_NUMS[i]);
        }
      }
      else // Buttons 4, 6, 7, 8
      {
        toggle_state[i] = !toggle_state[i];
        if (toggle_state[i])
        {
          usbMIDI.sendNoteOn(MIDI_NOTE_NUMS[i], MIDI_NOTE_VELS[i], MIDI_CHAN);
          usbMIDI.sendControlChange(MIDI_CC_NUMS[i], MIDI_CC_VALS[i], MIDI_CHAN);
          Serial.print("Note On: ");
          Serial.print(MIDI_NOTE_NUMS[i]);
          Serial.print(" Velocity: ");
          Serial.println(MIDI_NOTE_VELS[i]);
          Serial.print("CC: ");
          Serial.print(MIDI_CC_NUMS[i]);
          Serial.print(" Value: ");
          Serial.println(MIDI_CC_VALS[i]);
        }
        else
        {
          usbMIDI.sendNoteOff(MIDI_NOTE_NUMS[i], MIDI_NOTE_VELS[i], MIDI_CHAN);
          usbMIDI.sendControlChange(MIDI_CC_NUMS[i], 0, MIDI_CHAN);
          Serial.print("Note Off: ");
          Serial.print(MIDI_NOTE_NUMS[i]);
          Serial.print(" Velocity: ");
          Serial.println(MIDI_NOTE_VELS[i]);
          Serial.print("CC: ");
          Serial.print(MIDI_CC_NUMS[i]);
          Serial.print(" Value: 0");
          Serial.println();
        }
      }
    }
    else if (buttons[i].risingEdge())
    {
      if (i == 0 || i == 1 || i == 2 || i == 4) // Buttons 1, 2, 3, 5
      {
        pc_sent[i] = false;
      }
    }
  }
}

void sendProgramChange(int pc)
{
  usbMIDI.sendProgramChange(pc, MIDI_CHAN);
  Serial.print("Program Change: ");
  Serial.println(pc);

  Serial3.write(0xC0 | (MIDI_CHAN - 1)); // Status byte for program change
  Serial3.write(pc); // Program change number
  Serial3.flush(); // Send the data immediately
}
 
First up, Teensy4x pins are not +5v tolerant so
Arduino pin +5v pin ----> 47 ohm resistor ----> pin 4 of the Midi din connector
is potentially hazardous to Pin 14.
Arduino pin +3.3v pin ----> 47 ohm resistor ----> pin 4 of the Midi din connector
is a better idea.
Next,
Code:
Serial3.begin(9600); // Initialize Serial3
needs to be:-
Code:
Serial3.begin(31250); // Initialize Serial3 to MIDI's baudrate

Hope this is helpful.
 
First up, Teensy4x pins are not +5v tolerant so

is potentially hazardous to Pin 14.

is a better idea.
Next,
Code:
Serial3.begin(9600); // Initialize Serial3
needs to be:-
Code:
Serial3.begin(31250); // Initialize Serial3 to MIDI's baudrate

Hope this is helpful.

Thanks.

I wrote out the connections incorrectly....

Pin 14 ------> 47 ohm -----> pin 5 of midi 5 pin din.

However, I am reading 5v on pin 5 of the din connection that comes from pin 14 on the Arduino.

So not sure what's going on there.....
 
Back
Top