Ayuda con controlador MIDI

Estoy tratando de construir un controlador MIDI para controlar software de música, y he encontrado unos ejemplos que tratan de optimizar el codigo para consegir la menor latencia posible.

El codigo es el siguiente:

// S2MIDI Multi key Arduino code
// Handles matrixed keypads and controllers at the same time
// This code is not supported, updated, etc. For example only.

int midiChannel = 9;
int outPin = 13;       // the number of the output pin
int analogPin = A4;
byte portMaskD = B10001000; //B00110001;
byte portMaskB = B00010000;  // Pins we care about are high
int keypadKeys[] = {4232,  // No keys pressed
                     4224, 
                     4104,
                     136};

// Key de-bouncing stuff
int lastKey = 0;
long time = 0;
int debounce = 50;

// Analog smoothing stuff
int lastAnalog = 0;
int analogSmooth = 2;  // Amount of change needed to send control change

void setup()
{
  // Setup relivant pins, slow way but only done once
  
  pinMode(3, INPUT); digitalWrite(3, HIGH); // pull high
  pinMode(7, INPUT); digitalWrite(7, HIGH); // pull high
  pinMode(12, INPUT); digitalWrite(12, HIGH); // pull high
  pinMode(outPin, OUTPUT);  // Output LED
  

  Serial.begin(31250);
}

void loop()
{
  int c = 0;
  int reading = 0;
// Read important pins  
  reading = PIND & portMaskD;
  reading += (PINB & portMaskB) << 8;
//  Serial.println(reading); // show raw input for finding keycodes // show raw input for finding keycodes

// Lookup key pressed
  c = ((sizeof(keypadKeys)+1)/2)-1;
  while (keypadKeys[c] != reading && c>0)
    c--;
// Play note
  if (c != lastKey && millis()-time > debounce) {
      lastKey = c;
      time = millis();
       switch (c) { 
          case 1:
          noteOn(midiChannel, 41, 120);
          break;
          case 2:
          noteOn(midiChannel, 40, 120); // debug
          break;
          case 3:
          noteOn(midiChannel, 56, 120); // debug
          break;
      }
  }
// Do analog line
  reading = analogRead(analogPin)/8; // change 0-1024 into 0-127
  if (abs(reading-lastAnalog) > analogSmooth) {
    lastAnalog = reading;
    controlChange(midiChannel,10,reading);  // Send CC10
  }
}


// Send a MIDI note-on message.  Like pressing a piano key
void noteOn(byte channel, byte note, byte velocity) {
  midiMsg(channel+0x90, note, velocity);
}

// Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
   midiMsg(channel+0x80, note, velocity);
}

// Send a MIDI control change
void controlChange(byte channel, byte controller, byte value) {
   midiMsg(channel+0xB0, controller, value);
}

// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
  digitalWrite(outPin,HIGH);  // indicate we're sending MIDI data
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
  digitalWrite(outPin,LOW);
}

tras corregir algunos fallos que tenia en la asignación de puertos, lo he hecho funcionar pero me surge un problema.

Solo escribe un noteon, cuando apretamos un pulsador, pero el programa se piensa que esa nota sigue pulsada porque espera un noteoff.

No entiendo muy bien la logica del codigo y es por esto que no se implementar el noteoff (todavia soy muy novato para este codigo) Alguien puede tratar de explicame para que es la cadena llamada "keypadKeys"...

Tampoco entiendo
reading = PIND & portMaskD;
reading += (PINB & portMaskB) << 8;
puesto que siempre me da cero, ya que desplaza 8 bits a la izquierda...

A ver si alguien me hecha una mano porque me estoy volviendo loco

Bueno pues un montón de horas despues ya he terminado de comprender el codigo y de implementar el noteoff.

Al final todo el fallo lo cometia al sumar y desplazar bits en binario, un simple parentesis te puede volver loco.

Copio el codigo de como queda, puesto que funciona muy bien, aunque esta un poquillo feo porque no lo he limpiado.

// S2MIDI Multi key Arduino code
// Handles matrixed keypads and controllers at the same time
// This code is not supported, updated, etc. For example only.

int midiChannel = 9;
int outPin = 13;       // the number of the output pin
int analogPin = A4;
byte portMaskD = B10001000; //B00110001;
byte portMaskB = B00010000;  // Pins we care about are high
int keypadKeys[] = {4232,  // No keys pressed
                     4224, //apretado botón A
                     4104, //apretado botón B
                     136}; //apretado botón C

// Key de-bouncing stuff
int lastKey = 0;
int pulso;
long time = 0;
int debounce = 150;

// Analog smoothing stuff
int lastAnalog = 0;
int analogSmooth = 2;  // Amount of change needed to send control change

void setup()
{
  // Setup relivant pins, slow way but only done once
  
  pinMode(3, INPUT); digitalWrite(3, HIGH); // pull high
  pinMode(7, INPUT); digitalWrite(7, HIGH); // pull high
  pinMode(12, INPUT); digitalWrite(12, HIGH); // pull high
  pinMode(outPin, OUTPUT);  // Output LED
  
 //Serial.begin(9600); //Opcional para pruebas
 Serial.begin(31250); //para MIDI
}

void loop()
{
  int c = 0;
  int reading = 0;
// Read important pins  
  reading = PIND & portMaskD;
  reading += (PINB & portMaskB) << 8;
//Serial.println(reading,BIN);  delay (1000);// show raw input for finding keycodes
//Serial.println(reading,DEC);  delay (1000);// show raw input for finding keycodes

// Lookup key pressed
  c = ((sizeof(keypadKeys)+1)/2)-1;
  while (keypadKeys[c] != reading && c>0)
    c--;
// Play note
  if (c != lastKey && pulso ==1) {
        switch (lastKey) {
          case 1:
          noteOn(midiChannel, 41, 0);
          break;
          case 2:
          noteOn(midiChannel, 40, 0); // debug
          break;
          case 3:
          noteOn(midiChannel, 56, 0); // debug
          break;
      }
  }   
  if (c != lastKey && millis()-time > debounce) {
   time = millis();     
       switch (c) { 
          case 0:
          pulso=0;
          break;
          case 1:
          noteOn(midiChannel, 41, 120);
          pulso=1;
          break;
          case 2:
          noteOn(midiChannel, 40, 120); // debug
          pulso=1;
          break;
          case 3:
          noteOn(midiChannel, 56, 120); // debug
          pulso=1;
          break;
      }
  }

 
  
  
 lastKey = c;

  
// Do analog line
  reading = analogRead(analogPin)/8; // change 0-1024 into 0-127
  if (abs(reading-lastAnalog) > analogSmooth) {
    lastAnalog = reading;
    controlChange(midiChannel,10,reading);  // Send CC10
  }
}


// Send a MIDI note-on message.  Like pressing a piano key
void noteOn(byte channel, byte note, byte velocity) {
  midiMsg(channel+0x90, note, velocity);
}

// Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
   midiMsg(channel+0x80, note, velocity);
}

// Send a MIDI control change
void controlChange(byte channel, byte controller, byte value) {
   midiMsg(channel+0xB0, controller, value);
}

// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
  digitalWrite(outPin,HIGH);  // indicate we're sending MIDI data
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
  digitalWrite(outPin,LOW);
}

PD: hubiera agradecido algún consejillo, eh!!