Keypad - Piano key matrix - 1st & 2nd octave issue

I have written this code for my 6 octave 72 key piano, however the octaves above 2 are fine, if I press a key on the 1st of 2nd octave, it will trigger the same key from the first and 2nd row when only one is pressed.. and I cant for the love of me see where its going wrong in the code.
I had this working perfectly and lost my code so had to re-write it

A wiring explanation of the physical keyboard circuit is as follows.
Octave 1: Has 9 Keys and starts on key 16
Octave 2: has 12 Keys and starts on key 25
Octave 3: has 12 Keys and starts on key 37
Octave 4: has 12 Keys and starts on key 49
Octave 5: has 12 Keys and starts on key 61
Octave 6: has 12 Keys and starts on key 84

If I press Key 18 on Octave 1, it will MIDI output the same key, on octave 1 & 2. Anything above octave 2 doesn't bug out like this

I have attached the snippet

#include <MIDI.h>
#include <Keypad.h>
int quiet = 0;


const byte ROWS = 6; //Seven rows
const byte COLS = 12; //Ten columns
char keys[ROWS][COLS] = {
 {13,14,15,16,17,18,19,20,21,22,23,24},
 {25,26,27,28,29,30,31,32,33,34,35,36},
 {37,38,39,40,41,42,43,44,45,46,47,48},
 {49,50,51,52,53,54,55,56,57,58,59,60},
 {61,62,63,64,65,66,67,68,69,70,71,72},
 {73,74,75,76,77,78,79,80,81,82,83,84}
};
byte rowPins[ROWS] = {A0,A1,A2,A3,A4,A5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {8,2,9,3,10,4,11,5,12,6,13,7}; //connect to the column pinouts of the kpd

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );




void setup() {
    Serial.begin(115200);
}


void loop() {

    if (kpd.getKeys())
    {
     
        for (int i=0; i<72; i++)   // Scan the whole key list.
        {
            if ((kpd.key[i].stateChanged ) && (kpd.key[i].kstate == PRESSED))  // Only find keys that have changed state.
            {
              int myKey = (kpd.key[i].kchar);
                {                 
               Serial.write(144);
                Serial.write(myKey);
                Serial.write(100);
                }

            }
            if ((kpd.key[i].stateChanged ) && (kpd.key[i].kstate == RELEASED))  // Only find keys that have changed state.
            {
              int myKey = (kpd.key[i].kchar);
                {                 
                Serial.write(128);
                Serial.write(myKey);
                Serial.write(quiet);
                }

            }
        } 
    }
}
        for (int i=0; i<72; i++)   // Scan the whole key list.
        {

That should be:

        for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
        {

You can only have LIST_MAX (10) keys active at a time. You are indexing WAY off the end of the list.

Even with that makes 0 difference

Serial.write() sends a single byte and truncates int myKey. Better send both bytes of the int or fix whatever causes key values higher than 255.

Surely that's not causing the actual issue?
It's only on the first two octaves and is not a wiring issue as I've tested it by just touching the wires

Could it be that A0 and A1 are shorted together?

I've tried and tested on different pins on my Arduino Due and still get the same effects.
I just need a simple keyboard matrix to scan 7 columns and 12 rows and send a midi note on press and release.
I've tried touching the 7 col pins to the 12 rows, and all the 12 rows are hitting the right numbers, but the first two columns are pressing two keys at the same time.
I've literally spent hours today researching this. I wish you could read code from an arduino... I got this Arduino Due to handle the process faster with also a fast LED library for leds on the keys, and I had written the piano code, and over wrote it, and its on a arduino nano... :sob: Pulling my hair out

Maybe it's a DUE thing. I switched it from MIDI output to displaying the presses and releases and it works fine on my Arduino UNO.

#include <Keypad.h>
int quiet = 0;


const byte ROWS = 6; //Seven rows
const byte COLS = 12; //Ten columns
char keys[ROWS][COLS] =
{
  {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},
  {25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36},
  {37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48},
  {49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
  {61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72},
  {73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84}
};
byte rowPins[ROWS] = {A0, A1, A2, A3, A4, A5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //connect to the column pinouts of the kpd


Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup()
{
  Serial.begin(115200);
}


void loop()
{
  if (kpd.getKeys())
  {
    for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
    {
      int myKey = kpd.key[i].kchar;
      
      if ((kpd.key[i].stateChanged) && (kpd.key[i].kstate == PRESSED))  // Only find keys that have changed state.
      {
        Serial.print("Pressed: ");
        Serial.println(myKey);
      }


      if ((kpd.key[i].stateChanged) && (kpd.key[i].kstate == RELEASED))  // Only find keys that have changed state.
      {
        Serial.print("Released: ");
        Serial.println(myKey);
      }
    }
  }
}

Just tried it on my arduino uno too same result. Its like I cant have more than 4 Rows... I need 7.
Anything on rows 4 is fine

Im wondering if theres a limit to the max rows?
I have the keyboard matrix setup as such, each key has 3 pins.
Key down connects the columns with the rows, key up grounds the columns to prevent ghosting which has seemed to help.
But anything from rows 6 to 7 trigger multiple values?
So this is why im wondering if keypad.h has a limit to the max number of rows being 5? But I cant find anything in the file relating such

casperround64:
Im wondering if theres a limit to the max rows?

The Keypad library, as shipped, is limited to 10 rows and 16 columns.
If you need more than 10 rows you should edit Keypad.h to change this line:

#define MAPSIZE 10		// MAPSIZE is the number of rows (times 16 columns)

casperround64:
I have the keyboard matrix setup as such, each key has 3 pins.
Key down connects the columns with the rows, key up grounds the columns to prevent ghosting which has seemed to help.

That sounds like a mistake. I don't think the Keypad library expects all of the column pins to be grounded.
I think the correct way to avoid ghosting is to add a diode in series with each key.