Sending midi from one sketch to another?

I would like some help stitching together two sketches. I have built a midi controller with a lot of buttons that sends out midi. So far so good. Next I would like to have the midi output of this sketch “picked up” by another script in order for it to spread the midi notes out to several different midi channels. I can’t seem to make it work.

The first script is this:

#include “MIDI_Controller.h” // Include the library

const uint8_t velocity = 0b1111111; // Maximum velocity (0b1111111 = 0x7F = 100)
const uint8_t addresses[16][16] = { // the note numbers corresponding to the buttons in the matrix
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 79, 74, 70, 65, 48, 43},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 78, 73, 69, 64, 47, 42},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 77, 72, 68, 63, 46, 41},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 76, 71, 67, 62, 45, 40},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 75, 70, 66, 61, 44, 39},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 74, 69, 65, 60, 43, 38},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 73, 68, 64, 59, 42, 37},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 72, 67, 63, 58, 41, 36},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 71, 66, 62, 57, 40, 35},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 70, 65, 61, 56, 39, 34},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 69, 64, 60, 55, 38, 33},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 68, 63, 59, 54, 37, 32},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 67, 62, 58, 53, 36, 31},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 66, 61, 57, 52, 35, 30},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 65, 60, 56, 51, 34, 29},
{ 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 64, 59, 55, 50, 33, 28}
};

// Create a new instance of the class ‘ButtonMatrix’, called ‘buttonmatrix’, with dimensions 4 rows and 3 columns, with the rows connected to pins 2, 3, 4 and 5
// and the columns connected to pins 6, 7 and 8, that sends MIDI messages with the notes specified in ‘addresses’ on MIDI channel 1, with velocity 127
ButtonMatrix<16, 16> buttonmatrix({11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41},{22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52}, addresses, 16, velocity);

void setup() {
}

void loop() {
// Refresh the buttons (check whether the states have changed since last time, if so, send it over MIDI)
MIDI_Controller.refresh();

}

The second script is this:

#include <MIDI.h>

//If you’r using a Mac and are getting a compilation error delete the next 3 lines of code
#ifdef MIDI_CREATE_DEFAULT_INSTANCE
MIDI_CREATE_DEFAULT_INSTANCE();
#endif

// -----------------------------------------------------------------------------

//Note: Midi CC value 64 = Speed value 0
const int vSpeedValue = { //Pitches (speed) for each note
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 32, 34, 37, 40, 43, 45, 48, 51, 53, 56, 59, 61, 64, 67, 69, 72, 75, 78, 80, 83, 85, 88, 91, 93, 96, 97, 99, 100, 102, 103, 104, 106, 107, 108, 109, 111, 112
};

int vVolcaMode = 1;

//Default Channel to send MIDI to Volca (Midi msg will be redirected to the corresponding channel on the Volca Sample)
int vPolyChan=16;

int vFirstVoice = 7;
int vVoiceChan = vFirstVoice;

int vNote;

// Range of notes that the Volca Sample can generate
int vPitchFirstNote = 36;
int vPitchLastNote = 84;

// Synth code, will assign the speed(pitch) depending on the note that was played
void fVoiceSend(int vVoiceChan, int vSpeed)
{
//MIDI.send(midi::NoteOff,60,0,vVoiceChan); //Thw Midi “Note Off” msg is not recognized by the volca Sample
MIDI.sendControlChange(43,vSpeed,vVoiceChan);
MIDI.send(MIDI.getType(),60,MIDI.getData2(),vVoiceChan);
}
// -----------------------------------------------------------------------------

//This code will make it possibble to play each sample using a single midi channel (channel 16 by default) on mini notes 0 to 9 (the lowest notes)
void fChannelRedir(int vChan)
{
MIDI.send(MIDI.getType(),
MIDI.getData1(),
MIDI.getData2(),
vChan);
}

// -----------------------------------------------------------------------------

// This function will be automatically called when a NoteOn is received.
void handleNoteOn(byte channel, byte pitch, byte velocity)
{

digitalWrite(13,HIGH); //Turns on led on pin 13 when note on is recived on channnel 16

if (MIDI.getChannel() != vPolyChan)
{
vVoiceChan = MIDI.getChannel();
}

vNote = MIDI.getData1();

if( vNote < 10 & MIDI.getChannel() == vPolyChan)
{
fChannelRedir(vNote+1);
};

//Polyphonic 4 voices Synth
if (vNote>= vPitchFirstNote & vNote <=vPitchLastNote) //Just notes on pitched range
{
fVoiceSend(vVoiceChan,vSpeedValue[vNote]);
if (vVoiceChan < vFirstVoice + 3 & MIDI.getChannel() == vPolyChan)
{
vVoiceChan++; //Go to the next voice
}
else
{
vVoiceChan = vFirstVoice; //Return to first voice channel
}
};

}

void setup()
{
pinMode(13, OUTPUT);
MIDI.turnThruOff();

// Connect the handleNoteOn function to the library,
// so it is called upon reception of a NoteOn.
MIDI.setHandleNoteOn(handleNoteOn); // Put only the name of the function

// Initiate MIDI communications, listen to all channels
MIDI.begin(MIDI_CHANNEL_OMNI);

//Set the pitch envelopes to 0 (Midi cc value of 64)
int counter = 1;
while (counter <= 10)
{
MIDI.sendControlChange(43,64,vVoiceChan);
MIDI.sendControlChange(44,64,vVoiceChan);
counter++;
}

vVoiceChan = vFirstVoice;
}

void loop()
{
// Call MIDI.read the fastest you can for real-time performance.
MIDI.read();
}

Any help would be greatly appreciated!

Best

Lameller