Add MIDI to old Keyboard

I have an old 61 Key keyboard that I'm trying to add a MIDI Out to but I'm having problems.

I'm Following This guide. Now I have the exact same PSR-150 that he does but I don't think the tutorial he is showing and the code/circuit he is using the same. His setup shows a 6X11 Matrix but in reality, it's a 11X6 Pulldown(i assume not sure how to tell if it's pull-up or pulldown).

I have altered the code to the best of my knowledge but when I press a key it shows the whole section(6 notes) in the serial monitor.

Here is the code.

#define NUM_ROWS 11
#define NUM_COLS 6

#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define NOTE_VELOCITY 127

//MIDI baud rate
#define SERIAL_RATE 115200

// Pin Definitions

// Row input pins
const int row1Pin = 2;
const int row2Pin = 3;
const int row3Pin = 4;
const int row4Pin = 5;
const int row5Pin = 6;
const int row6Pin = 7;
const int row7Pin = 8;
const int row8Pin = 9;
const int row9Pin = 10;
const int row10Pin = 11;
const int row11Pin = 12;

// 74HC595 pins
const int dataPin = 14;
const int latchPin = 15;
const int clockPin = 16;

boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[NUM_ROWS][NUM_COLS];

// bitmasks for scanning columns
int bits[] =
{ 
  B00000001,
  B00000010,
  B00000100,
  B00001000,
  B00010000,
  B00100000,
  B01000000,
  B10000000
};

void setup()
{
  int note = 31;

  for(int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
  {
    for(int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
    {
      keyPressed[rowCtr][colCtr] = false;
      keyToMidiMap[rowCtr][colCtr] = note;
      note++;
    }
  }

  // setup pins output/input mode
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);

  pinMode(row1Pin, INPUT);
  pinMode(row2Pin, INPUT);
  pinMode(row3Pin, INPUT);
  pinMode(row4Pin, INPUT);
  pinMode(row5Pin, INPUT);
  pinMode(row6Pin, INPUT);
  pinMode(row7Pin, INPUT);
  pinMode(row8Pin, INPUT);
  pinMode(row9Pin, INPUT);
  pinMode(row10Pin, INPUT);
  pinMode(row11Pin, INPUT);

  Serial.begin(SERIAL_RATE);
}

void loop()
{
  for (int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
  {
    //scan next column
    scanColumn(colCtr);

    //get row values at this column
    int rowValue[NUM_ROWS];
    rowValue[0] = digitalRead(row1Pin);
    rowValue[1] = digitalRead(row2Pin);
    rowValue[2] = digitalRead(row3Pin);
    rowValue[3] = digitalRead(row4Pin);
    rowValue[4] = digitalRead(row5Pin);
    rowValue[5] = digitalRead(row6Pin);
    rowValue[6] = digitalRead(row7Pin);
    rowValue[7] = digitalRead(row8Pin);
    rowValue[8] = digitalRead(row9Pin);
    rowValue[9] = digitalRead(row10Pin);
    rowValue[10] = digitalRead(row11Pin);

    // process keys pressed
    for(int rowCtr=0; rowCtr<NUM_ROWS; ++rowCtr)
    {
      if(rowValue[rowCtr] != 0 && !keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = true;
        noteOn(rowCtr,colCtr);
      }
    }

    // process keys released
    for(int rowCtr=0; rowCtr<NUM_ROWS; ++rowCtr)
    {
      if(rowValue[rowCtr] == 0 && keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = false;
        noteOff(rowCtr,colCtr);
      }
    }
  }
}

void scanColumn(int colNum)
{
  digitalWrite(latchPin, LOW);

  if(0 <= colNum && colNum <= 7)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //right sr
    //shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr
  }
  else
  {
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum-8]); //right sr
    //shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //left sr
  }
  digitalWrite(latchPin, HIGH);
}

void noteOn(int row, int col)
{
  Serial.print("note on number ");
  Serial.println(keyToMidiMap[row][col]);
  //Serial.write(NOTE_ON_CMD);
  //Serial.write(keyToMidiMap[row][col]);
  //Serial.write(NOTE_VELOCITY);
  
}

void noteOff(int row, int col)
{
  Serial.print("Note off number ");
  Serial.println(keyToMidiMap[row][col]);
  //Serial.write(NOTE_OFF_CMD);
  //Serial.write(keyToMidiMap[row][col]);
  //Serial.write(NOTE_VELOCITY);
}

Here Is what I found for the key matrix by using my multimeter in diode mode.

Here is what the sections look like and the input pin that each section is mapped to.

Here is my circuit.
I might have wired the output pins in the wrong order as they go (from right to left on the keyboard 1,3,5,7,9,2) but in the shift register, I think I will need to flip that around. But all that is going to change is the sequence of the keys.


Any help will be much appreciated.

Note in the original code he was using 2 shift registers for the outputs but i should be able to just use this right ?

void scanColumn(int colNum)
{
  digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //right sr
    //shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr
  digitalWrite(latchPin, HIGH);
}

Note 2: Inthe comments from Instructables some people were saying that you cant play 2 notes at the same time from the same group. EX: I can play G2 & G3 at the same time but G3 & A3 since they are part of the same group (input pin 14)

Again any thanks will be appreciated.

Check my topic about my conversion, I made a similar project not too long ago.

I only used one shift register and I don't remember how the diodes were in my matrix but some element of my project might be useful to you.

Good luck!

fuegovic:
Check my topic about my conversion, I made a similar project not too long ago.

I only used one shift register and I don't remember how the diodes were in my matrix but some element of my project might be useful to you.

Good luck!

I took a look at your code and tried to see what the problem with mine was and it all went to hell haha. To be honest most if not all of this is beyond me.

I'm going to keep poking at it to try and learn something.

Thank you

fuegovic:
Check my topic about my conversion, I made a similar project not too long ago.

I only used one shift register and I don't remember how the diodes were in my matrix but some element of my project might be useful to you.

Good luck!

I tried to alter your code quickly to see but i get no output.

This is what i changed.

I have 11 input pins from 2 to 12 and my sr is on pin 14 15 16

//=======================Keyboard Matrix=======================//
const int rowPin[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; // Rows are connected to Digital
const int clock = 16;  //SHCP p11 // 74HC595 (columns)
const int latch = 15; //STCP p12 // 74HC595 (columns)
const int data = 14;  //DS p14   // 74HC595 (columns)
//=============================Pin Definitions END=============================//

I have 61 keys

//==================How Many Keys In The Matrix==================//
uint8_t keyToMidiMap[61];
boolean keyPressed[61];

//=========================Channel Number=========================//

I didnt change the notes because at this point im just trying to get a proper output over the correct values

//==============Map Keys To Actual Midi Note Number==============//
//==========(num 41 corresponds to F1 MIDI note)==========//
//=========Group 1=========//
  keyToMidiMap[0] = 41;
  keyToMidiMap[1] = 48;
  keyToMidiMap[2] = 55;
  keyToMidiMap[3] = 62;
  keyToMidiMap[4] = 69;
  keyToMidiMap[5] = 76;
//=========Group 2=========//
  keyToMidiMap[0 + 6] = 42;
  keyToMidiMap[1 + 6] = 49;
  keyToMidiMap[2 + 6] = 56;
  keyToMidiMap[3 + 6] = 63;
  keyToMidiMap[4 + 6] = 70;
  keyToMidiMap[5 + 6] = 77;
//=========Group 3=========//
  keyToMidiMap[0 + 12] = 43;
  keyToMidiMap[1 + 12] = 50;
  keyToMidiMap[2 + 12] = 57;
  keyToMidiMap[3 + 12] = 64;
  keyToMidiMap[4 + 12] = 71;
  keyToMidiMap[5 + 12] = 78;
//=========Group 4=========//
  keyToMidiMap[0 + 18] = 44;
  keyToMidiMap[1 + 18] = 51;
  keyToMidiMap[2 + 18] = 58;
  keyToMidiMap[3 + 18] = 65;
  keyToMidiMap[4 + 18] = 72;
  keyToMidiMap[5 + 18] = 79;
//=========Group 5=========//
  keyToMidiMap[0 + 24] = 45;
  keyToMidiMap[1 + 24] = 52;
  keyToMidiMap[2 + 24] = 59;
  keyToMidiMap[3 + 24] = 66;
  keyToMidiMap[4 + 24] = 73;
  keyToMidiMap[5 + 24] = 80;
//=========Group 6=========//
  keyToMidiMap[0 + 30] = 46;
  keyToMidiMap[1 + 30] = 53;
  keyToMidiMap[2 + 30] = 60;
  keyToMidiMap[3 + 30] = 67;
  keyToMidiMap[4 + 30] = 74;
  keyToMidiMap[5 + 30] = 81;
//=========Group 7=========//
  keyToMidiMap[0 + 36] = 47;
  keyToMidiMap[1 + 36] = 54;
  keyToMidiMap[2 + 36] = 61;
  keyToMidiMap[3 + 36] = 68;
  keyToMidiMap[4 + 36] = 75;
  keyToMidiMap[5 + 36] = 82; 
  //=========Group 8=========//
  keyToMidiMap[0 + 42] = 47;
  keyToMidiMap[1 + 42] = 54;
  keyToMidiMap[2 + 42] = 61;
  keyToMidiMap[3 + 42] = 68;
  keyToMidiMap[4 + 42] = 75;
  keyToMidiMap[5 + 42] = 82; 
  //=========Group 9=========//
  keyToMidiMap[0 + 48] = 47;
  keyToMidiMap[1 + 48] = 54;
  keyToMidiMap[2 + 48] = 61;
  keyToMidiMap[3 + 48] = 68;
  keyToMidiMap[4 + 48] = 75;
  keyToMidiMap[5 + 48] = 82; 
  //=========Group 10=========//
  keyToMidiMap[0 + 54] = 47;
  keyToMidiMap[1 + 54] = 54;
  keyToMidiMap[2 + 54] = 61;
  keyToMidiMap[3 + 54] = 68;
  keyToMidiMap[4 + 54] = 75;
  keyToMidiMap[5 + 54] = 82; 
  //=========Group 11========//
  keyToMidiMap[0 + 60] = 47;
  keyToMidiMap[1 + 60] = 54;
  keyToMidiMap[2 + 60] = 61;
  keyToMidiMap[3 + 60] = 68;
  keyToMidiMap[4 + 60] = 75;
  keyToMidiMap[5 + 60] = 82; 
//================END of Keyboard Matrix Map================//

I have 11 inputs

//================Setup Pins Output/Input Mode================//
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
  pinMode(rowPin[0], INPUT);
  pinMode(rowPin[1], INPUT);
  pinMode(rowPin[2], INPUT);
  pinMode(rowPin[3], INPUT);
  pinMode(rowPin[4], INPUT);
  pinMode(rowPin[5], INPUT);
  pinMode(rowPin[6], INPUT);
  pinMode(rowPin[7], INPUT);
  pinMode(rowPin[8], INPUT);
  pinMode(rowPin[9], INPUT);
  pinMode(rowPin[10], INPUT);
  pinMode(raiseBtn, INPUT); // Transpose up
  pinMode(lowerBtn, INPUT); // Transpose down
  pinMode(modeBtn, INPUT);  // Transpose mode

but no go

I used this project for insiration, maybe it could help you more to start than looking at my code. He has schematic and a lot more explications than me :wink:

The only way for me to succeed was to go one small step at a time. The first step was the hardest, having the matrix working in my code, after that it gets easier!

I'm so confused right now. From what I'm seeing if I have 11 groups of 6 pins it should be a 6x11 (6 pins going to the Arduino for input and 11 from the shift register.) But when I test it with my multimeter the current only flows the other way from 6 pins TO the 11 other pins and NOT from 11 pins to 6 pins.

So I have no idea anymore. If I hook it up like 6x11 I get no results either in pulldown or pullup. If I hook it up as an 11x6 configuration I only get a result as pulldown but 8 keys show up at once using the first code I had listed. When I try using the guide you posted I get no output at all.

I know that I would have to use the code like the one you posted to get the multiple keys working in the same group at once.

Thank you.

I will see if I can dig up my schematic with the matrix polarity and send it to you.

Draknoid:
But when I test it with my multimeter the current only flows the other way from 6 pins TO the 11 other pins and NOT from 11 pins to 6 pins.

Which implies its a diode switch matrix. Perhaps this is useful?: arduino duemilanove - Need Help in circuit diagram of 8*8 reed switch matrix - Arduino Stack Exchange

Well, I think I found the problem.

I had this

void scanColumn(int colNum)
{
  digitalWrite(latchPin, LOW);

  if(0 <= colNum && colNum <= 7)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //right sr
    //shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr
  }
  else
  {
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum-8]); //right sr
    //shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //left sr
  }
  digitalWrite(latchPin, HIGH);
}

was for 2 shift registers but i needed this

void scanColumn(int colNum)
{
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]);
    digitalWrite(latchPin, HIGH);
}

So now i can press 1 note and only that note shows up.

Now they are out of sequence and i got to figure that out

So some keys were not grouped right so I changed the code from

for(int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
  {
    for(int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
    {
      keyPressed[rowCtr][colCtr] = false;
      keyToMidiMap[rowCtr][colCtr] = note;
      note++;
    }
  }

To

for(int rowCtr = 0; rowCtr < NUM_ROWS; rowCtr++)
  {
    for(int colCtr = 0; colCtr < NUM_COLS; colCtr++)
    {
      keyPressed[rowCtr][colCtr] = false;
      keyToMidiMap[rowCtr][colCtr] = note;
      note++;
    }
  }

Now all the notes were in their proper groups but out of sequence. I fixed this by swapping around my 6 input pins. Now all the keys show in order and I get them working in midi-ox using loopMIDI and HairlessMIDI.

So this is the final result

#define NUM_ROWS 11
#define NUM_COLS 6

#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define NOTE_VELOCITY 127

//MIDI baud rate
#define SERIAL_RATE 115200

// Pin Definitions

// Row input pins
const int row1Pin = 2;
const int row2Pin = 3;
const int row3Pin = 4;
const int row4Pin = 5;
const int row5Pin = 6;
const int row6Pin = 7;
const int row7Pin = 8;
const int row8Pin = 9;
const int row9Pin = 10;
const int row10Pin = 11;
const int row11Pin = 12;

// 74HC595 pins
const int dataPin = 16;
const int latchPin = 15;
const int clockPin = 14;

boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[NUM_ROWS][NUM_COLS];

// bitmasks for scanning columns
int bits[] =
{
  B00000001,
  B00000010,
  B00000100,
  B00001000,
  B00010000,
  B00100000,
  B01000000,
  B10000000
};

void setup()
{
  int note = 31;
  for(int rowCtr = 0; rowCtr < NUM_ROWS; rowCtr++)
  {
    for(int colCtr = 0; colCtr < NUM_COLS; colCtr++)
    {
      keyPressed[rowCtr][colCtr] = false;
      keyToMidiMap[rowCtr][colCtr] = note;
      note++;
    }
  }

  // setup pins output/input mode
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);

  pinMode(row1Pin, INPUT);
  pinMode(row2Pin, INPUT);
  pinMode(row3Pin, INPUT);
  pinMode(row4Pin, INPUT);
  pinMode(row5Pin, INPUT);
  pinMode(row6Pin, INPUT);
  pinMode(row7Pin, INPUT);
  pinMode(row8Pin, INPUT);
  pinMode(row9Pin, INPUT);
  pinMode(row10Pin, INPUT);
  pinMode(row11Pin, INPUT);

  Serial.begin(SERIAL_RATE);
}

void loop()
{
  for (int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
  {
    //scan next column
    scanColumn(colCtr);

    //get row values at this column
    int rowValue[NUM_ROWS];
    rowValue[0] = digitalRead(row1Pin);
    rowValue[1] = digitalRead(row2Pin);
    rowValue[2] = digitalRead(row3Pin);
    rowValue[3] = digitalRead(row4Pin);
    rowValue[4] = digitalRead(row5Pin);
    rowValue[5] = digitalRead(row6Pin);
    rowValue[6] = digitalRead(row7Pin);
    rowValue[7] = digitalRead(row8Pin);
    rowValue[8] = digitalRead(row9Pin);
    rowValue[9] = digitalRead(row10Pin);
    rowValue[10] = digitalRead(row11Pin);

    // process keys pressed
    for (int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
    {
      if (rowValue[rowCtr] != 0 && !keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = true;
        noteOn(rowCtr, colCtr);
      }
    }

    // process keys released
    for (int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
    {
      if (rowValue[rowCtr] == 0 && keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = false;
        noteOff(rowCtr, colCtr);
      }
    }
  }
}

void scanColumn(int colNum)
{
  digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr

  digitalWrite(latchPin, HIGH);
}

void noteOn(int row, int col)
{
  Serial.write(NOTE_ON_CMD);
  Serial.write(keyToMidiMap[row][col]);
  Serial.write(NOTE_VELOCITY);

}

void noteOff(int row, int col)
{
  Serial.write(NOTE_OFF_CMD);
  Serial.write(keyToMidiMap[row][col]);
  Serial.write(NOTE_VELOCITY);
}

Draknoid

Karma for the follow up.

    rowValue[0] = digitalRead(row1Pin);
    rowValue[1] = digitalRead(row2Pin);
    rowValue[2] = digitalRead(row3Pin);
    rowValue[3] = digitalRead(row4Pin);
    rowValue[4] = digitalRead(row5Pin);
    rowValue[5] = digitalRead(row6Pin);
    rowValue[6] = digitalRead(row7Pin);
    rowValue[7] = digitalRead(row8Pin);
    rowValue[8] = digitalRead(row9Pin);
    rowValue[9] = digitalRead(row10Pin);
    rowValue[10] = digitalRead(row11Pin);

I think an array of pin numbers could be useful here!

const byte pins[] = {row1Pin, row2Pin, ...., row11Pin};
...
  for (byte i = 0; i < 11 ; i++)
    rowValue[i] = digitalRead (pins[i]) ;

MarkT:

    rowValue[0] = digitalRead(row1Pin);

rowValue[1] = digitalRead(row2Pin);
    rowValue[2] = digitalRead(row3Pin);
    rowValue[3] = digitalRead(row4Pin);
    rowValue[4] = digitalRead(row5Pin);
    rowValue[5] = digitalRead(row6Pin);
    rowValue[6] = digitalRead(row7Pin);
    rowValue[7] = digitalRead(row8Pin);
    rowValue[8] = digitalRead(row9Pin);
    rowValue[9] = digitalRead(row10Pin);
    rowValue[10] = digitalRead(row11Pin);




I think an array of pin numbers could be useful here!



const byte pins[] = {row1Pin, row2Pin, ...., row11Pin};
...
  for (byte i = 0; i < 11 ; i++)
    rowValue[i] = digitalRead (pins[i]) ;

Thank you for that.

I made changes to the code. If I press the 61st key after powering on it goes into test mode. If I press the 60th key it goes into "pc" mode so I can use loopmidi and hairlessmidi. Else it starts in midi mode. Still waiting for my midi connector and midi to USB cable.

#define NUM_ROWS 11
#define NUM_COLS 6

#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define NOTE_VELOCITY 127

//MIDI baud rate
#define SERIAL_RATE 31250

bool testing = false;
bool firstKey = true;

// Pin Definitions

// Row input pins
const int row1Pin = 2;
const int row2Pin = 3;
const int row3Pin = 4;
const int row4Pin = 5;
const int row5Pin = 6;
const int row6Pin = 7;
const int row7Pin = 8;
const int row8Pin = 9;
const int row9Pin = 10;
const int row10Pin = 11;
const int row11Pin = 12;
const byte pins[] = {row1Pin, row2Pin, row3Pin, row4Pin, row5Pin, row6Pin, row7Pin, row8Pin, row9Pin, row10Pin, row11Pin};

// 74HC595 pins
const int dataPin = 16;
const int latchPin = 15;
const int clockPin = 14;

boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[NUM_ROWS][NUM_COLS];

// bitmasks for scanning columns
int bits[] =
{
  B00000001,
  B00000010,
  B00000100,
  B00001000,
  B00010000,
  B00100000,
  B01000000,
  B10000000
};

void setup()
{
  int note = 31;
  for(int rowCtr = 0; rowCtr < NUM_ROWS; rowCtr++)
  {
    for(int colCtr = 0; colCtr < NUM_COLS; colCtr++)
    {
      keyPressed[rowCtr][colCtr] = false;
      keyToMidiMap[rowCtr][colCtr] = note;
      note++;
    }
  }

  // setup pins output/input mode
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);

  pinMode(row1Pin, INPUT);
  pinMode(row2Pin, INPUT);
  pinMode(row3Pin, INPUT);
  pinMode(row4Pin, INPUT);
  pinMode(row5Pin, INPUT);
  pinMode(row6Pin, INPUT);
  pinMode(row7Pin, INPUT);
  pinMode(row8Pin, INPUT);
  pinMode(row9Pin, INPUT);
  pinMode(row10Pin, INPUT);
  pinMode(row11Pin, INPUT);
  
  Serial.begin(SERIAL_RATE);
}

void loop()
{
  for (int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
  {
    //scan next column
    scanColumn(colCtr);

    //get row values at this column
    int rowValue[NUM_ROWS];
    for (byte i = 0; i < 11 ; i++)
    rowValue[i] = digitalRead (pins[i]) ;

    // process keys pressed
    for (int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
    { 
      if (rowValue[rowCtr] != 0 && !keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = true;
        noteOn(rowCtr, colCtr);
      }
    }

    // process keys released
    for (int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
    {
      if (rowValue[rowCtr] == 0 && keyPressed[rowCtr][colCtr])
      {
        keyPressed[rowCtr][colCtr] = false;
        noteOff(rowCtr, colCtr);
      }
    }
  }
}

void scanColumn(int colNum)
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr
  digitalWrite(latchPin, HIGH);
}

void noteOn(int row, int col)
{
  if (firstKey)
  {
    // If key 61 (C7) is held down enter USB mode
    if (row == 10 && col == 5)
      {
        testing = true;
        Serial.flush();
        Serial.begin(115200);
        // empty  out possible garbage from input buffer
        while(Serial.available()) Serial.read();
        Serial.println("Testing mode on");
      }
      // If key 60 (B6) is held down enter testing mode
      else if (row == 10 && col == 4)
      {
        Serial.flush();
        Serial.begin(115200);
      }
   firstKey = false;
   }
  
  // For Testing
  if (testing)
  {
    Serial.print("note on number ");
    Serial.println(keyToMidiMap[row][col]);
    Serial.print(row);
    Serial.print(",");
    Serial.println(col);
  }
  else
  {
    Serial.write(NOTE_ON_CMD);
    Serial.write(keyToMidiMap[row][col]);
    Serial.write(NOTE_VELOCITY);
  }
}

void noteOff(int row, int col)
{
  // For Testing
  if (testing)
  {
    Serial.print("Note off number ");
    Serial.println(keyToMidiMap[row][col]);
    Serial.print(row);
    Serial.print(",");
    Serial.println(col); 
  }
  else
  {
    Serial.write(NOTE_OFF_CMD);
    Serial.write(keyToMidiMap[row][col]);
    Serial.write(NOTE_VELOCITY);
  }
  
}

My question is is there another way of doing it then in noteon ?

void noteOn(int row, int col)
{
  if (firstKey)
  {
    // If key 61 (C7) is held down enter testing mode
    if (row == 10 && col == 5)
      {
        testing = true;
        Serial.flush();
        Serial.begin(115200);
        // empty  out possible garbage from input buffer
        while(Serial.available()) Serial.read();
        Serial.println("Testing mode on");
      }
      // If key 60 (B6) is held down enter testing mode
      else if (row == 10 && col == 4)
      {
        Serial.flush();
        Serial.begin(115200);
      }
   firstKey = false;
   }
  
  // For Testing
  if (testing)
  {
    Serial.print("note on number ");
    Serial.println(keyToMidiMap[row][col]);
    Serial.print(row);
    Serial.print(",");
    Serial.println(col);
  }
  else
  {
    Serial.write(NOTE_ON_CMD);
    Serial.write(keyToMidiMap[row][col]);
    Serial.write(NOTE_VELOCITY);
  }
}