Arduino Mega MIDI controllers going into USB hub not working reliably...


I am having a weird problem. It’s weird because I have done this before many times and this is the first time I’ve had this problem.

I have two arduino megas. I used midi moco for lufa to make them midi controllers, by flashing the m16u2 chip next to the USB port.

I used some code, which I placed below, to read sensor data and output it as midi control messages. One device is on midi channel 1 and one is on midi channel 2. The code for both is very similar other wise.

I have some sensors hooked up to both of them now for testing. Both of them work fine when plugged in by themselves.

When they are both plugged in, either into a hub, or into two separate USB ports they get completely out of wack, and send sensor data really late and unreliably.

This is strange because for years I have been using two arduino megas running (almost) the exact same code and everything has been fine. I even added two other Arduino megas running the same code (on different midi channels) and all four work great together.

The only thing I changed in the new version of this code is I added these lines at the top of the code:

#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>


I think without the " MIDI_CREATE_DEFAULT_INSTANCE(); " the code wasn’t verifying.

I am using a mac powerbook running OSX 10.11.3

I have tried commenting out

#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>

And I tried reflashing the m16u2 chip with an older version of midi moco for lufa that worked the last time I did this.

My Arduino code is below. Any suggestions or guidance is greatly appreciated.

thank you. Nick

#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>

MIDI_CREATE_DEFAULT_INSTANCE();  //nick added for compatibility with older sketch, 8/13/17

  - controller 1
  March 2012

  modified 8/13/17 by N.Demopoulos

const int MIDIChannel = 1;

const int numAnalogIn = 13;
const int firstAnalogIn = 0;  // pin number of first analog in
const int firstAnalogController = 1;  // midi controller number for this analog in
int currentAnalogIndex = 0;
int analogOld[numAnalogIn];

const int numDigitalIn = 17;
const int firstDigitalIn = 22;   //pin number of first digital in
const int firstDigitalController = 14;  // midi controller number for first digital out
int currentDigitalIndex = 0;
int digitalOld[numDigitalIn];

void setup() {
  // wire 3.3V to AREF for 3.3V reference
  int i;
  for (i=0;i<numAnalogIn; i++) {
    analogOld[i] = -1;
  for (i=0;i<numDigitalIn; i++) {
    digitalOld[i] = -1;
    // set digital pins to input with pull-up
    pinMode(i+firstDigitalIn, INPUT);
    digitalWrite(i+firstDigitalIn, HIGH);

void loop() {
  int analog = analogRead(currentAnalogIndex + firstAnalogIn);
  if (analog != analogOld[currentAnalogIndex]) {
    //MIDI.sendControlChange(currentAnalogIndex+firstAnalogController, analog >> 3, 1);
    analogOld[currentAnalogIndex] = analog;
    sendControlChange(currentAnalogIndex+firstAnalogController, analog >> 3, MIDIChannel);
  currentAnalogIndex %= numAnalogIn;
  int digital = digitalRead(currentDigitalIndex + firstDigitalIn);
  if (digital != digitalOld[currentDigitalIndex]) {
    digitalOld[currentDigitalIndex] = digital;
    sendControlChange(currentDigitalIndex+firstDigitalController, (digital==1 ? 0 :127), MIDIChannel);
  currentDigitalIndex %= numDigitalIn;

void sendControlChange(int controllerNumber, int controllerValue, int midiChannel) {
  //Serial.write(0xB0 | (midiChannel & 0x0F));
  //Serial.write(controllerNumber & 0x7F);
  //Serial.write(controllerValue & 0x7F);
  MIDI.sendControlChange(controllerNumber, controllerValue, midiChannel);

Something is seriously FUBAR on the MIDI aspects of the Arduino. I had 3 working projects and now none of them work. No hardware changes purely IDE update.