MIDI Library

Fred Ringwald

New member
Hello,

I had a working project on Teensy 4.0 linking the MIDI Library by Francois Best, lathoub (https://www.arduino.cc/reference/en/libraries/midi-library/). When I moved this project to Teensy 4.1 using the Arduino IDE, the project wouldn't compile, complaining that I had two different MIDI libraries, and it was using the internal Teensy 4.1 MIDI library. I have tried to rewrite my sketch to use the internal Teensy 4.1 MIDI library, but I can't find and reference to it anywhere. Therefore, I don't know what code syntax to use to accomplish the same MIDI functions that my current project used with the Francois Best MIDI library in the Teensy 4.0 application.

Can someone please point me to the syntax for using the Teensy 4.1 MIDI library?

Thank you,


Fred Ringwald
 
Here is the code for my Teensy 4.0 project that worked using Teensy 4.0. The project is to implement a MIDI interface for an organ pedalboard using Hall effect sensors, generating MIDI NoteOn and NoteOff signals whenever the Teensy detected either a pedal depressed (when the magnet is close to the Hall effect sensor, or released (when the magnet is away from the Hall effect sensor). The button32/16/8/4State switches emulate organ 32, 16, 8, and 4 foot stops to dictate whether a depressed pedal generates a low note (if the 32 foot button is depressed), or notes one, two, or three octaves higher if the 16, 8, or 4 foot buttons are depressed.

The MIDI functions used are quite simple. I just need the syntax to convert the current MIDI code lines to the correct syntax for the Teensy 4.1 internal MIDI library syntax.

Thank you for any and all help!


Fred Ringwald

================= code ======================
#include <c:\Users\fred\Documents\Arduino\libraries\MIDI_Library\src\MIDI.h>
// Create and bind the MIDI interface to the default hardware Serial port
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);
// Define variables to assign Hall effect sensor input pins and status
int hallPin6 = 6; // First Hall effect sensor status connected to digital pin 6
int hallPin16 = 12; // Second Hall effect sensor status connected to digital pin 16
int hallStatus6 = 0; // variable to hold first Hall effect sensor status
int hallStatus16 = 0; // variable to hold second Hall effect sensor status
// variable to hold previous pin status for comparison with current status
int previousStatus6 = 0;
int previousStatus16 = 0;
// initialization for stop buttons
const int button32Pin = 5; // the number of the 32 foot stop button pin
const int button16Pin = 4; // the number of the 16 foot stop button pin
const int button8Pin = 3; // the number of the 8 foot stop button pin
const int button4Pin = 2; // the number of the 4 foot stop button pin
const int ledPin = 13; // the number of the LED pin
int button32State = 0; // variable for reading the 32 foot stop button status
int button16State = 0; // variable for reading the 16 foot stop button status
int button8State = 0; // variable for reading the 8 foot stop button status
int button4State = 0; // variable for reading the 4 foot stop button status
void setup() {
Serial.begin(31250);
MIDI.begin(MIDI_CHANNEL_OMNI); // Listen to all incoming messages
pinMode(hallPin6, INPUT); // sets the digital pin 6 as input
pinMode(hallPin16, INPUT); // sets the digital pin 16 as input
previousStatus6 = 0;
previousStatus16 = 0;
// Setup for stop buttons
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button32Pin, INPUT);
pinMode(button16Pin, INPUT);
pinMode(button8Pin, INPUT);
pinMode(button4Pin, INPUT);
}
void loop() {
// Section to monitor stop buttons
// read the state of the stop button values
button32State = digitalRead(button32Pin);
button16State = digitalRead(button16Pin);
button8State = digitalRead(button8Pin);
button4State = digitalRead(button4Pin);

//read the inputs on digital pins 6
hallStatus6 = !digitalRead(hallPin6); // read input pin 6
if (previousStatus6 != hallStatus6) {
previousStatus6 = hallStatus6; // set previousStatus6 to new sensor status
if (hallStatus6 == 1) {
if (button32State == HIGH) {
MIDI.sendNoteOn(29, 127, 1);
} else {
MIDI.sendNoteOff(29, 127, 1);
}
if (button16State == HIGH) {
MIDI.sendNoteOn(41, 127, 1);
} else {
MIDI.sendNoteOff(41, 127, 1);
}
if (button8State == HIGH) {
MIDI.sendNoteOn(53, 127, 1);
} else {
MIDI.sendNoteOff(53, 127, 1);
}
if (button4State == HIGH) {
MIDI.sendNoteOn(65, 127, 1);
} else {
MIDI.sendNoteOff(65, 127, 1);
}
}
}
//read the inputs on digital pins 16
hallStatus16 = !digitalRead(hallPin16); // read input pin 6
if (previousStatus16 != hallStatus16) {
previousStatus16 = hallStatus16; // set previousStatus6 to new sensor status
if (hallStatus16 == 1) {
if (button32State == HIGH) {
MIDI.sendNoteOn(39, 127, 1);
} else {
MIDI.sendNoteOff(39, 127, 1);
}
if (button16State == HIGH) {
MIDI.sendNoteOn(51, 127, 1);
} else {
MIDI.sendNoteOff(51, 127, 1);
}
if (button8State == HIGH) {
MIDI.sendNoteOn(63, 127, 1);
} else {
MIDI.sendNoteOff(63, 127, 1);
}
if (button4State == HIGH) {
MIDI.sendNoteOn(75, 127, 1);
} else {
MIDI.sendNoteOff(75, 127, 1);
}
}
}



delay(500);
}
 
Just change line #1 and #3 like this:
C++:
#include <MIDI.h>  
// Create and bind the MIDI interface to the default hardware Serial port
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);           
// Define variables to assign Hall effect sensor input pins and status
int hallPin6 = 6;      // First Hall effect sensor status connected to digital pin 6
int hallPin16 = 12;    // Second Hall effect sensor status connected to digital pin 16
int hallStatus6 = 0;   // variable to hold first Hall effect sensor status
int hallStatus16 = 0;  // variable to hold second Hall effect sensor status
// variable to hold previous pin status for comparison with current status
int previousStatus6 = 0;
int previousStatus16 = 0;
// initialization for stop buttons
const int button32Pin = 5;  // the number of the 32 foot stop button pin
const int button16Pin = 4;  // the number of the 16 foot stop button pin
const int button8Pin = 3;   // the number of the 8 foot stop button pin
const int button4Pin = 2;   // the number of the 4 foot stop button pin
const int ledPin = 13;      // the number of the LED pin
int button32State = 0;      // variable for reading the 32 foot stop button status
int button16State = 0;      // variable for reading the 16 foot stop button status
int button8State = 0;       // variable for reading the 8 foot stop button status
int button4State = 0;       // variable for reading the 4 foot stop button status
void setup() {
  Serial.begin(31250);
  MIDI.begin(MIDI_CHANNEL_OMNI);  // Listen to all incoming messages
  pinMode(hallPin6, INPUT);       // sets the digital pin 6 as input
  pinMode(hallPin16, INPUT);      // sets the digital pin 16 as input
  previousStatus6 = 0;
  previousStatus16 = 0;
  // Setup for stop buttons
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(button32Pin, INPUT);
  pinMode(button16Pin, INPUT);
  pinMode(button8Pin, INPUT);
  pinMode(button4Pin, INPUT);
}
void loop() {
  // Section to monitor stop buttons
  // read the state of the stop button values
  button32State = digitalRead(button32Pin);
  button16State = digitalRead(button16Pin);
  button8State = digitalRead(button8Pin);
  button4State = digitalRead(button4Pin);

  //read the inputs on digital pins 6
  hallStatus6 = !digitalRead(hallPin6);  // read input pin 6
  if (previousStatus6 != hallStatus6) {
    previousStatus6 = hallStatus6;  // set previousStatus6 to new sensor status
    if (hallStatus6 == 1) {
      if (button32State == HIGH) {
        MIDI.sendNoteOn(29, 127, 1);
      } else {
        MIDI.sendNoteOff(29, 127, 1);
      }
      if (button16State == HIGH) {
        MIDI.sendNoteOn(41, 127, 1);
      } else {
        MIDI.sendNoteOff(41, 127, 1);
      }
      if (button8State == HIGH) {
        MIDI.sendNoteOn(53, 127, 1);
      } else {
        MIDI.sendNoteOff(53, 127, 1);
      }
      if (button4State == HIGH) {
        MIDI.sendNoteOn(65, 127, 1);
      } else {
        MIDI.sendNoteOff(65, 127, 1);
      }
    }
  }
  //read the inputs on digital pins 16
  hallStatus16 = !digitalRead(hallPin16);  // read input pin 6
  if (previousStatus16 != hallStatus16) {
    previousStatus16 = hallStatus16;  // set previousStatus6 to new sensor status
    if (hallStatus16 == 1) {
      if (button32State == HIGH) {
        MIDI.sendNoteOn(39, 127, 1);
      } else {
        MIDI.sendNoteOff(39, 127, 1);
      }
      if (button16State == HIGH) {
        MIDI.sendNoteOn(51, 127, 1);
      } else {
        MIDI.sendNoteOff(51, 127, 1);
      }
      if (button8State == HIGH) {
        MIDI.sendNoteOn(63, 127, 1);
      } else {
        MIDI.sendNoteOff(63, 127, 1);
      }
      if (button4State == HIGH) {
        MIDI.sendNoteOn(75, 127, 1);
      } else {
        MIDI.sendNoteOff(75, 127, 1);
      }
    }
  }



  delay(500);
}

More info on this page.

Paul
 
Back
Top