"Array of MIDI-Interfaces" - how?

Hi Arduinianers,

I have a bigger problem with a project (http://sourceforge.net/projects/ardumidimerge/). I am working with an Arduino-Mega and three of the serial ports as MIDI. I need to make an array of my MIDI instances - but how?

Here is my test code - works not. I think it is a problem of understanding C++:

#include <MIDI.h>

midi::MidiInterface<HardwareSerial> m[3];

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, m[0]);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, m[1]);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, m[2]); 

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");
  
  m[0].begin(MIDI_CHANNEL_OMNI);
  m[1].begin(MIDI_CHANNEL_OMNI);
  m[2].begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
  byte in_port;
  
  for(in_port=0;in_port<3;in_port++)
  {    
    if(m[in_port].read())
    {
      Serial.print("incoming port: ");
      Serial.print(in_port,DEC);
      Serial.print(" data1: ");
      Serial.println(m[in_port].getData1());
    }
  }
}

Does anyone know how I can get my instandes in an array?

Thanks a lot, Holger

I just tried the following. It compiles with no errors but it does not work - the read()-method does never show any events…

#include <MIDI.h>

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiA);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midiB);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, midiC); 
midi::MidiInterface<HardwareSerial> m[3]={
  midiA,midiB,midiC};

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");
  
  m[0].begin(MIDI_CHANNEL_OMNI);
  m[1].begin(MIDI_CHANNEL_OMNI);
  m[2].begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
  byte in_port;
  
  for(in_port=0;in_port<3;in_port++)
  {    
    if(m[in_port].read())
    {
      Serial.print("incoming port: ");
      Serial.print(in_port,DEC);
      Serial.print(" data1: ");
      Serial.println(m[in_port].getData1());
    }
  }
}

Okokok… I am such an idiot. The problem was not the code. The problem was in front of the screen.

In my main code I just forgot to initialise the MIDI interfaces and in my example code I made a mistake while initialising the array for the MIDIs.

This works (on a Mega!):

#include <MIDI.h>

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiA);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midiB);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, midiC); 

midi::MidiInterface<HardwareSerial> m[]={midiA,midiB,midiC};

void setup()
{
  byte i;
  
  Serial.begin(115200);
  Serial.println("Start");

  // dont't forget to initialize!!! 
  for(i=0;i<3;i++)  
    m[i].begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
  byte in_port;
  
  for(in_port=0;in_port<3;in_port++)
  {    
    if(m[in_port].read())
    {
      Serial.print("incoming port: ");
      Serial.print(in_port,DEC);
      Serial.print(" channel: ");
      Serial.print(m[in_port].getChannel());
      Serial.print(" data1: ");
      Serial.print(m[in_port].getData1());
      Serial.print(" data2: ");
      Serial.println(m[in_port].getData2());
    }
  }
}

Hey! I'm glad it worked for you :D

What about latency? Have you worked further on this? reading inputs and driving outputs, as you use the midi library?