help with Midi code

hey people!
I am currently working on a midi project I am not new to electronics but I am new to writing code. I opened up my broken keyboard and wanted to make a new midi keyboard. everything is working except my top five notes are all messed up. they are playing very low any guidance on how to fix this would be much appreciated. here is my code [see link below] the key matrix is 8x5 and the lowest note is C.

sketch_jul15b.ino (4.39 KB)

Please read the how to use this forum sticky post, it will tell you how to post code here. Many people are on mobile devices and they can’t cope with .ino files.

In order to make sense of the code we also need a schematic, note this does not mean a Fritzing physical layout picture.

uint8_t keyToMidiMap[32];

  keyToMidiMap[32] = 72;
  keyToMidiMap[1 + 32] = 68;
  keyToMidiMap[2 + 32] = 69;
  keyToMidiMap[3 + 32] = 70;
  keyToMidiMap[4 + 32] = 71;

You are putting 37 keys into a 32-key array!

Try initializing the array the normal way:

const uint8_t keyToMidiMap[37] =
{
  43, 36, 37, 38, 39, 40, 41, 42, 
  51, 44, 45, 46, 47, 48, 49, 50, 
  59, 52, 53, 54, 55, 56, 57, 58, 
  67, 60, 61, 62, 63, 64, 65, 66, 
  72, 68, 69, 70, 71
};

Thank you. Like I said I am fairly new to writing code. i got the code from this website. Code Tinker Hack: How to turn Piano toy into MIDI keyboard (using Arduino/Atmega)
and I adjusted the code as much as I could to make it work with my key matrix. So if im understanding you right I should delete this part of the code

void setup() {
	
	// Map scan matrix buttons/keys to actual Midi note number. Lowest num 41 corresponds to F MIDI note.
	keyToMidiMap[0] = 48;
	keyToMidiMap[1] = 41;
	keyToMidiMap[2] = 42;
	keyToMidiMap[3] = 43;
	keyToMidiMap[4] = 44;
	keyToMidiMap[5] = 45;
	keyToMidiMap[6] = 46;
	keyToMidiMap[7] = 47;

	keyToMidiMap[8] = 56;
	keyToMidiMap[1 + 8] = 49;
	keyToMidiMap[2 + 8] = 50;
	keyToMidiMap[3 + 8] = 51;
	keyToMidiMap[4 + 8] = 52;
	keyToMidiMap[5 + 8] = 53;
	keyToMidiMap[6 + 8] = 54;
	keyToMidiMap[7 + 8] = 55;

	keyToMidiMap[16] = 64;
	keyToMidiMap[1 + 16] = 57;
	keyToMidiMap[2 + 16] = 58;
	keyToMidiMap[3 + 16] = 59;
	keyToMidiMap[4 + 16] = 60;
	keyToMidiMap[5 + 16] = 61;
	keyToMidiMap[6 + 16] = 62;
	keyToMidiMap[7 + 16] = 63;

	keyToMidiMap[24] = 72;
	keyToMidiMap[1 + 24] = 65;
	keyToMidiMap[2 + 24] = 66;
	keyToMidiMap[3 + 24] = 67;
	keyToMidiMap[4 + 24] = 68;
	keyToMidiMap[5 + 24] = 69;
	keyToMidiMap[6 + 24] = 70;
	keyToMidiMap[7 + 24] = 71;

and put in the code you sent me?

orbitingpluto:
So if im understanding you right I should delete this part of the code
and put in the code you sent me?

Yes. The declaration I wrote includes the initialization values. Add the initialization to your declaration and then remove the part of setup() that initializes all of those array elements.

// Pin Definitions
// Rows are connected to
const int row1 = 4;
const int row2 = 5;
const int row3 = 6;
const int row4 = 7;
const int row5 = 8;


// The 74HC595 uses a serial communication
// link which has three pins
const int clock = 9;
const int latch = 10;
const int data = 11;


uint8_t keyToMidiMap[32];

boolean keyPressed[32];

int noteVelocity = 127;


// use prepared bit vectors instead of shifting bit left everytime
int bits[] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };


// 74HC595 shift to next column
void scanColumn(int value) {
  digitalWrite(latch, LOW); //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
}

void setup() {

  const uint8_t keyToMidiMap[37] =
{
  43, 36, 37, 38, 39, 40, 41, 42, 
  51, 44, 45, 46, 47, 48, 49, 50, 
  59, 52, 53, 54, 55, 56, 57, 58, 
  67, 60, 61, 62, 63, 64, 65, 66, 
  72, 68, 69, 70, 71
};


  // setup pins output/input mode
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);

  pinMode(row1, INPUT);
  pinMode(row2, INPUT);
  pinMode(row3, INPUT);
  pinMode(row4, INPUT);
  pinMode(row5, INPUT);

  Serial.begin(31250);

  delay(1000);

}

void loop() {

  for (int col = 0; col < 8; col++) {

    // shift scan matrix to following column
    scanColumn(bits[col]);

    // check if any keys were pressed - rows will have HIGH output in this case corresponding
    int groupValue1 = digitalRead(row1);
    int groupValue2 = digitalRead(row2);
    int groupValue3 = digitalRead(row3);
    int groupValue4 = digitalRead(row4);
    int groupValue5 = digitalRead(row5);

    // process if any combination of keys pressed
    if (groupValue1 != 0 || groupValue2 != 0 || groupValue3 != 0
        || groupValue4 != 0 || groupValue5 != 0) {

      if (groupValue1 != 0 && !keyPressed[col]) {
        keyPressed[col] = true;
        noteOn(0x91, keyToMidiMap[col], noteVelocity);
      }

      if (groupValue2 != 0 && !keyPressed[col + 8]) {
        keyPressed[col + 8] = true;
        noteOn(0x91, keyToMidiMap[col + 8], noteVelocity);
      }

      if (groupValue3 != 0 && !keyPressed[col + 16]) {
        keyPressed[col + 16] = true;
        noteOn(0x91, keyToMidiMap[col + 16], noteVelocity);
      }

      if (groupValue4 != 0 && !keyPressed[col + 24]) {
        keyPressed[col + 24] = true;
        noteOn(0x91, keyToMidiMap[col + 24], noteVelocity);
      }  
      if (groupValue5 != 0 && !keyPressed[col + 32]) {
        keyPressed[col + 32] = true;
        noteOn(0x91, keyToMidiMap[col + 32], noteVelocity);
      }

    }

    //  process if any combination of keys released
    if (groupValue1 == 0 && keyPressed[col]) {
      keyPressed[col] = false;
      noteOn(0x91, keyToMidiMap[col], 0);
    }

    if (groupValue2 == 0 && keyPressed[col + 8]) {
      keyPressed[col + 8] = false;
      noteOn(0x91, keyToMidiMap[col + 8], 0);
    }

    if (groupValue3 == 0 && keyPressed[col + 16]) {
      keyPressed[col + 16] = false;
      noteOn(0x91, keyToMidiMap[col + 16], 0);
    }

    if (groupValue4 == 0 && keyPressed[col + 24]) {
      keyPressed[col + 24] = false;
      noteOn(0x91, keyToMidiMap[col + 24], 0);
    }

    if (groupValue5 == 0 && keyPressed[col + 32]) {
      keyPressed[col + 32] = false;
      noteOn(0x91, keyToMidiMap[col + 32], 0);
    }

  }

}


void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

so is this code looking right?

so is this code looking right?

No.

Re read reply #4.

As you have defined the variable keyToMidiMap in the setup function it is only valid to use in the setup function.

If you could send me an example of the what you are talking about that would be very helpful. I am very new to code writing and sadly some of the things you are saying are going over my head. I have tried to look up what you are talking about as well as try different variations to the code but still no luck.

One important thing to learn: Variables declared inside a 'block' (code surrounded by '{' and '}') are local to that block and are not visible outside that block. By putting the declaration inside setup(){ } you are creating a temporary variable that the rest of the code can't use. The rest of the code will use THE ORIGINAL GLOBAL VARIABLE OF THE SAME NAME which has NOT been initialized. Move the declaration up, out of setup(), to replace the ORIGINAL declaration.

// Pin Definitions
// Rows are connected to
const int row1 = 4;
const int row2 = 5;
const int row3 = 6;
const int row4 = 7;
const int row5 = 8;


// The 74HC595 uses a serial communication
// link which has three pins
const int clock = 9;
const int latch = 10;
const int data = 11;


uint8_t keyToMidiMap[37];

boolean keyPressed[37];

int noteVelocity = 127;


// use prepared bit vectors instead of shifting bit left everytime
int bits[] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };


// 74HC595 shift to next column
void scanColumn(int value) {
  digitalWrite(latch, LOW); //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
}

void setup() {

  // Map scan matrix buttons/keys to actual Midi note number. Lowest num 41 corresponds to F MIDI note.
  keyToMidiMap[0] = 43;
  keyToMidiMap[1] = 36;
  keyToMidiMap[2] = 37;
  keyToMidiMap[3] = 38;
  keyToMidiMap[4] = 39;
  keyToMidiMap[5] = 40;
  keyToMidiMap[6] = 41;
  keyToMidiMap[7] = 42;

  keyToMidiMap[8] = 51;
  keyToMidiMap[1 + 8] = 44;
  keyToMidiMap[2 + 8] = 45;
  keyToMidiMap[3 + 8] = 46;
  keyToMidiMap[4 + 8] = 47;
  keyToMidiMap[5 + 8] = 48;
  keyToMidiMap[6 + 8] = 49;
  keyToMidiMap[7 + 8] = 50;

  keyToMidiMap[16] = 59;
  keyToMidiMap[1 + 16] = 52;
  keyToMidiMap[2 + 16] = 53;
  keyToMidiMap[3 + 16] = 54;
  keyToMidiMap[4 + 16] = 55;
  keyToMidiMap[5 + 16] = 56;
  keyToMidiMap[6 + 16] = 57;
  keyToMidiMap[7 + 16] = 58;

  keyToMidiMap[24] = 67;
  keyToMidiMap[1 + 24] = 60;
  keyToMidiMap[2 + 24] = 61;
  keyToMidiMap[3 + 24] = 62;
  keyToMidiMap[4 + 24] = 63;
  keyToMidiMap[5 + 24] = 64;
  keyToMidiMap[6 + 24] = 65;
  keyToMidiMap[7 + 24] = 66;

  keyToMidiMap[37] = 72;
  keyToMidiMap[1 + 37] = 68;
  keyToMidiMap[2 + 37] = 69;
  keyToMidiMap[3 + 37] = 70;
  keyToMidiMap[4 + 37] = 71;


  // setup pins output/input mode
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);

  pinMode(row1, INPUT);
  pinMode(row2, INPUT);
  pinMode(row3, INPUT);
  pinMode(row4, INPUT);
  pinMode(row5, INPUT);

  Serial.begin(31250);

  delay(1000);

}

void loop() {

  for (int col = 0; col < 8; col++) {

    // shift scan matrix to following column
    scanColumn(bits[col]);

    // check if any keys were pressed - rows will have HIGH output in this case corresponding
    int groupValue1 = digitalRead(row1);
    int groupValue2 = digitalRead(row2);
    int groupValue3 = digitalRead(row3);
    int groupValue4 = digitalRead(row4);
    int groupValue5 = digitalRead(row5);

    // process if any combination of keys pressed
    if (groupValue1 != 0 || groupValue2 != 0 || groupValue3 != 0
        || groupValue4 != 0 || groupValue5 != 0) {

      if (groupValue1 != 0 && !keyPressed[col]) {
        keyPressed[col] = true;
        noteOn(0x91, keyToMidiMap[col], noteVelocity);
      }

      if (groupValue2 != 0 && !keyPressed[col + 8]) {
        keyPressed[col + 8] = true;
        noteOn(0x91, keyToMidiMap[col + 8], noteVelocity);
      }

      if (groupValue3 != 0 && !keyPressed[col + 16]) {
        keyPressed[col + 16] = true;
        noteOn(0x91, keyToMidiMap[col + 16], noteVelocity);
      }

      if (groupValue4 != 0 && !keyPressed[col + 24]) {
        keyPressed[col + 24] = true;
        noteOn(0x91, keyToMidiMap[col + 24], noteVelocity);
      }  
      if (groupValue5 != 0 && !keyPressed[col + 37]) {
        keyPressed[col + 37] = true;
        noteOn(0x91, keyToMidiMap[col + 37], noteVelocity);
      }

    }

    //  process if any combination of keys released
    if (groupValue1 == 0 && keyPressed[col]) {
      keyPressed[col] = false;
      noteOn(0x91, keyToMidiMap[col], 0);
    }

    if (groupValue2 == 0 && keyPressed[col + 8]) {
      keyPressed[col + 8] = false;
      noteOn(0x91, keyToMidiMap[col + 8], 0);
    }

    if (groupValue3 == 0 && keyPressed[col + 16]) {
      keyPressed[col + 16] = false;
      noteOn(0x91, keyToMidiMap[col + 16], 0);
    }

    if (groupValue4 == 0 && keyPressed[col + 24]) {
      keyPressed[col + 24] = false;
      noteOn(0x91, keyToMidiMap[col + 24], 0);
    }

    if (groupValue5 == 0 && keyPressed[col + 37]) {
      keyPressed[col + 37] = false;
      noteOn(0x91, keyToMidiMap[col + 37], 0);
    }

  }

}


void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

so i put it into a 37 key array with the right values going to each key but I am still doing something wrong. I am sorry for all of the silly questions.

John said

Add the initialization to your declaration and then remove the part of setup() that initializes all of those array elements.

Once more with a little emphasis on what you did not do.

Add the initialization to your declaration and then remove the part of setup() that initializes all of those array elements.
So that last code you did not add what you were told nor did you remove what you were told.

when ever I do this I get an error code.

// Pin Definitions
// Rows are connected to
const int row1 = 4;
const int row2 = 5;
const int row3 = 6;
const int row4 = 7;
const int row5 = 8;


// The 74HC595 uses a serial communication
// link which has three pins
const int clock = 9;
const int latch = 10;
const int data = 11;


uint8_t keyToMidiMap[37];

boolean keyPressed[37];

int noteVelocity = 127;


// use prepared bit vectors instead of shifting bit left everytime
int bits[] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };


// 74HC595 shift to next column
void scanColumn(int value) {
  digitalWrite(latch, LOW); //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
}


const uint8_t keyToMidiMap[37] =
{
  43, 36, 37, 38, 39, 40, 41, 42, 
  51, 44, 45, 46, 47, 48, 49, 50, 
  59, 52, 53, 54, 55, 56, 57, 58, 
  67, 60, 61, 62, 63, 64, 65, 66, 
  72, 68, 69, 70, 71
};
void setup() 

  // setup pins output/input mode
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);

  pinMode(row1, INPUT);
  pinMode(row2, INPUT);
  pinMode(row3, INPUT);
  pinMode(row4, INPUT);
  pinMode(row5, INPUT);

  Serial.begin(31250);

  delay(1000);

}

void loop() {

  for (int col = 0; col < 8; col++) {

    // shift scan matrix to following column
    scanColumn(bits[col]);

    // check if any keys were pressed - rows will have HIGH output in this case corresponding
    int groupValue1 = digitalRead(row1);
    int groupValue2 = digitalRead(row2);
    int groupValue3 = digitalRead(row3);
    int groupValue4 = digitalRead(row4);
    int groupValue5 = digitalRead(row5);

    // process if any combination of keys pressed
    if (groupValue1 != 0 || groupValue2 != 0 || groupValue3 != 0
        || groupValue4 != 0 || groupValue5 != 0) {

      if (groupValue1 != 0 && !keyPressed[col]) {
        keyPressed[col] = true;
        noteOn(0x91, keyToMidiMap[col], noteVelocity);
      }

      if (groupValue2 != 0 && !keyPressed[col + 8]) {
        keyPressed[col + 8] = true;
        noteOn(0x91, keyToMidiMap[col + 8], noteVelocity);
      }

      if (groupValue3 != 0 && !keyPressed[col + 16]) {
        keyPressed[col + 16] = true;
        noteOn(0x91, keyToMidiMap[col + 16], noteVelocity);
      }

      if (groupValue4 != 0 && !keyPressed[col + 24]) {
        keyPressed[col + 24] = true;
        noteOn(0x91, keyToMidiMap[col + 24], noteVelocity);
      }  
      if (groupValue5 != 0 && !keyPressed[col + 37]) {
        keyPressed[col + 37] = true;
        noteOn(0x91, keyToMidiMap[col + 37], noteVelocity);
      }

    }

    //  process if any combination of keys released
    if (groupValue1 == 0 && keyPressed[col]) {
      keyPressed[col] = false;
      noteOn(0x91, keyToMidiMap[col], 0);
    }

    if (groupValue2 == 0 && keyPressed[col + 8]) {
      keyPressed[col + 8] = false;
      noteOn(0x91, keyToMidiMap[col + 8], 0);
    }

    if (groupValue3 == 0 && keyPressed[col + 16]) {
      keyPressed[col + 16] = false;
      noteOn(0x91, keyToMidiMap[col + 16], 0);
    }

    if (groupValue4 == 0 && keyPressed[col + 24]) {
      keyPressed[col + 24] = false;
      noteOn(0x91, keyToMidiMap[col + 24], 0);
    }

    if (groupValue5 == 0 && keyPressed[col + 37]) {
      keyPressed[col + 37] = false;
      noteOn(0x91, keyToMidiMap[col + 37], 0);
    }

  }

}


void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

orbitingpluto:
when ever I do this I get an error code.

uint8_t keyToMidiMap[37];

const uint8_t keyToMidiMap[37] =
{
 43, 36, 37, 38, 39, 40, 41, 42,
 51, 44, 45, 46, 47, 48, 49, 50,
 59, 52, 53, 54, 55, 56, 57, 58,
 67, 60, 61, 62, 63, 64, 65, 66,
 72, 68, 69, 70, 71
};
void setup()

// setup pins output/input mode
 pinMode(data, OUTPUT);
 pinMode(clock, OUTPUT);

I’m guessing the error, which you should generally supply when you want help fixing an error, has something to do with ‘keyToMidiMap’ being defined twice and/or the fact that you deleted the ‘{’ that marks the beginning of setup().

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

sketch_jul15b:34: error: conflicting declaration 'const uint8_t keyToMidiMap [37]'

 const uint8_t keyToMidiMap[37] =

                              ^

C:\Users\Michael Johnson\Documents\Arduino\sketch_jul15b\sketch_jul15b.ino:17:9: note: previous declaration as 'uint8_t keyToMidiMap [37]'

 uint8_t keyToMidiMap[37];

         ^

exit status 1
conflicting declaration 'const uint8_t keyToMidiMap [37]'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

here is the error code

An array that is declared as having 37 elements only has index numbers 0 to 36. There is no such thing as index number 37, that is what your error message is telling you.

// Pin Definitions
// Rows are connected to
const int row1 = 4;
const int row2 = 5;
const int row3 = 6;
const int row4 = 7;
const int row5 = 8;


// The 74HC595 uses a serial communication
// link which has three pins
const int clock = 9;
const int latch = 10;
const int data = 11;




boolean keyPressed[37];

int noteVelocity = 127;


// use prepared bit vectors instead of shifting bit left everytime
int bits[] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };


// 74HC595 shift to next column
void scanColumn(int value) {
  digitalWrite(latch, LOW); //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
}
const uint8_t keyToMidiMap[37] =
{
  43, 36, 37, 38, 39, 40, 41, 42, 
  51, 44, 45, 46, 47, 48, 49, 50, 
  59, 52, 53, 54, 55, 56, 57, 58, 
  67, 60, 61, 62, 63, 64, 65, 66, 
  72, 68, 69, 70, 71
};
void setup() {

  


  // setup pins output/input mode
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);

  pinMode(row1, INPUT);
  pinMode(row2, INPUT);
  pinMode(row3, INPUT);
  pinMode(row4, INPUT);
  pinMode(row5, INPUT);

  Serial.begin(31250);

  delay(1000);

}

void loop() {

  for (int col = 0; col < 8; col++) {

    // shift scan matrix to following column
    scanColumn(bits[col]);

    // check if any keys were pressed - rows will have HIGH output in this case corresponding
    int groupValue1 = digitalRead(row1);
    int groupValue2 = digitalRead(row2);
    int groupValue3 = digitalRead(row3);
    int groupValue4 = digitalRead(row4);
    int groupValue5 = digitalRead(row5);

    // process if any combination of keys pressed
    if (groupValue1 != 0 || groupValue2 != 0 || groupValue3 != 0
        || groupValue4 != 0 || groupValue5 != 0) {

      if (groupValue1 != 0 && !keyPressed[col]) {
        keyPressed[col] = true;
        noteOn(0x91, keyToMidiMap[col], noteVelocity);
      }

      if (groupValue2 != 0 && !keyPressed[col + 8]) {
        keyPressed[col + 8] = true;
        noteOn(0x91, keyToMidiMap[col + 8], noteVelocity);
      }

      if (groupValue3 != 0 && !keyPressed[col + 16]) {
        keyPressed[col + 16] = true;
        noteOn(0x91, keyToMidiMap[col + 16], noteVelocity);
      }

      if (groupValue4 != 0 && !keyPressed[col + 24]) {
        keyPressed[col + 24] = true;
        noteOn(0x91, keyToMidiMap[col + 24], noteVelocity);
      }  
      if (groupValue5 != 0 && !keyPressed[col + 32]) {
        keyPressed[col + 32] = true;
        noteOn(0x91, keyToMidiMap[col + 32], noteVelocity);
      }

    }

    //  process if any combination of keys released
    if (groupValue1 == 0 && keyPressed[col]) {
      keyPressed[col] = false;
      noteOn(0x91, keyToMidiMap[col], 0);
    }

    if (groupValue2 == 0 && keyPressed[col + 8]) {
      keyPressed[col + 8] = false;
      noteOn(0x91, keyToMidiMap[col + 8], 0);
    }

    if (groupValue3 == 0 && keyPressed[col + 16]) {
      keyPressed[col + 16] = false;
      noteOn(0x91, keyToMidiMap[col + 16], 0);
    }

    if (groupValue4 == 0 && keyPressed[col + 24]) {
      keyPressed[col + 24] = false;
      noteOn(0x91, keyToMidiMap[col + 24], 0);
    }

    if (groupValue5 == 0 && keyPressed[col + 32]) {
      keyPressed[col + 32] = false;
      noteOn(0x91, keyToMidiMap[col + 32], 0);
    }

  }

}


void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

so this is the closest I have gotten I have gotten every note to play except for high C. so that must mean I still am doing something wrong in my code. I feel it has to do with this section but I could be wrong

    // process if any combination of keys pressed
    if (groupValue1 != 0 || groupValue2 != 0 || groupValue3 != 0
        || groupValue4 != 0 || groupValue5 != 0) {

      if (groupValue1 != 0 && !keyPressed[col]) {
        keyPressed[col] = true;
        noteOn(0x91, keyToMidiMap[col], noteVelocity);
      }

      if (groupValue2 != 0 && !keyPressed[col + 8]) {
        keyPressed[col + 8] = true;
        noteOn(0x91, keyToMidiMap[col + 8], noteVelocity);
      }

      if (groupValue3 != 0 && !keyPressed[col + 16]) {
        keyPressed[col + 16] = true;
        noteOn(0x91, keyToMidiMap[col + 16], noteVelocity);
      }

      if (groupValue4 != 0 && !keyPressed[col + 24]) {
        keyPressed[col + 24] = true;
        noteOn(0x91, keyToMidiMap[col + 24], noteVelocity);
      }  
      if (groupValue5 != 0 && !keyPressed[col + 32]) {
        keyPressed[col + 32] = true;
        noteOn(0x91, keyToMidiMap[col + 32], noteVelocity);
      }

    }

    //  process if any combination of keys released
    if (groupValue1 == 0 && keyPressed[col]) {
      keyPressed[col] = false;
      noteOn(0x91, keyToMidiMap[col], 0);
    }

    if (groupValue2 == 0 && keyPressed[col + 8]) {
      keyPressed[col + 8] = false;
      noteOn(0x91, keyToMidiMap[col + 8], 0);
    }

    if (groupValue3 == 0 && keyPressed[col + 16]) {
      keyPressed[col + 16] = false;
      noteOn(0x91, keyToMidiMap[col + 16], 0);
    }

    if (groupValue4 == 0 && keyPressed[col + 24]) {
      keyPressed[col + 24] = false;
      noteOn(0x91, keyToMidiMap[col + 24], 0);
    }

    if (groupValue5 == 0 && keyPressed[col + 32]) {
      keyPressed[col + 32] = false;
      noteOn(0x91, keyToMidiMap[col + 32], 0);

Your keyToMidiMap does not contain enough entries. The maximum value of col + 32 is more than 36 isn’t it?

when I go higher than 32 the notes get even more messed up. I put in 37 at first because there are 37 keys total and that did not work.

I put in 37 at first because there are 37 keys total and that did not work.

Because you tried to address element 37 which is one more than the number in the array.

You are now trying to address higher elements than even 37.

I think you are perhaps not up to this project yet, you seem to be having difficulty understanding the simple mistakes you are making. Try reading up how arrays works in a C tutorial.

SOOOOOO! I got the key bed working!

// Pin Definitions
// Rows are connected to
const int row1 = 4;
const int row2 = 5;
const int row3 = 6;
const int row4 = 7;
const int row5 = 8;

const int rows[5] = {4, 5, 6, 7, 8};

// The 74HC595 uses a serial communication 
// link which has three pins
const int clock = 9;
const int latch = 10;
const int data = 11;


uint8_t keyToMidiMap[40];

boolean keyPressed[37];

int noteVelocity = 127;


// use prepared bit vectors instead of shifting bit left everytime
int bits[] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };

int values[5][8] = {
    {43, 36, 37, 38, 39, 40, 41, 42},
    {51, 44, 45, 46, 47, 48, 49, 50},
    {59, 52, 53, 54, 55, 56, 57, 58},
    {67, 60, 61, 62, 63, 64, 65, 66},
    {0, 68, 69, 70, 71, 72, 0, 0}
  };  


// 74HC595 shift to next column
void scanColumn(int value) {
  digitalWrite(latch, LOW); //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH); //Pulls the latch high displaying the data
}

void setup() {
  Serial.begin(31250);
//  Serial.begin(115200);
  
  // Map scan matrix buttons/keys to actual Midi note number. Lowest num 41 corresponds to F MIDI note.  
  for(int row = 0; row < 5; row++) {
    for(int col = 0; col < 8; col++) {
      int index = (row * 8) + col;
      keyToMidiMap[index] = values[row][col];
//      // Limit to 37 keys
//      if(index < 37) {
//        
//      }      
    }
  }

  // setup pins output/input mode
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);

  pinMode(row1, INPUT);
  pinMode(row2, INPUT);
  pinMode(row3, INPUT);
  pinMode(row4, INPUT);
  pinMode(row5, INPUT);

  delay(1000);

}

void loop() {
  for (int col = 0; col < 8; col++) {    
    // shift scan matrix to following column
    scanColumn(bits[col]);

    // check if any keys were pressed - rows will have HIGH output in this case corresponding

    for(int row = 0; row < 5; row++) {
      // If any row is pulled high, scan columns for that row
      int index = (row * 8) + col;
      int rowState = digitalRead(rows[row]);
      
      if(rowState != 0 && !keyPressed[index]) {
        keyPressed[index] = true;
        noteOn(0x91, keyToMidiMap[index], noteVelocity);
        break;
      }
      
      if(rowState == 0 && keyPressed[index]) {
        keyPressed[index] = false;
        noteOn(0x91, keyToMidiMap[index], 0);
        break;
      }
    }
  }
}


void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

I had some help with my friend. the problem was that note 72 shared the same column with 6 not column 8. now that that is out of the way I am working on add octave switches to the board.
So I found this code on another forum

// MultiButtonMIDIOctave.ino
// Driving MIDI using a Multiple Buttons
// Added variables for Octave, Transpose and Velocity
// Rob Ives 2012
// This code is released into the Public Domain.
 
#include 
#include 
 
int keyispressed[16]; //Is the key currently pressed?
int noteisplaying[16]; //Is the Note currently playing?
unsigned char data1; //data from chip 1
unsigned char data2; //data from chip 2
int octave = 2; //set currently played octave
int transpose = 0; //transpose notes on the board
int velocity = 127; //set note velocity
//--- Octave LED output pins
int octave1 = 8;
int octave2 = 9;
int octave3 = 10;
int octave4 = 11;
//--- Octave Switches 
int octaveup = 7;
int octavedown = 6;
//--- Pot
int potpin = 2;
 
void  setup() //The Setup Loop
{
  Wire.begin(); // setup the I2C bus
  for (unsigned int i = 0; i &lt; 16; i++) { //Init variables
    keyispressed[i] = 1; //clear the keys array (High is off)
    noteisplaying[i] = 0; //no notes are playing
  }
  //--- Set up pins
  pinMode(octaveup, INPUT);
  pinMode(octavedown, INPUT);
  pinMode(octave1, OUTPUT);
  pinMode(octave2, OUTPUT);
  pinMode(octave3, OUTPUT);
  pinMode(octave4, OUTPUT);
 
  MIDI.begin(); //initialise midi library
  displayoctave(); //display the current octave
 
}
//---------------------------------------------
void loop() //the main loop
{
  readkeys();
  sendMIDI();
  checkoctave();
}  
//-------------------------------------
void readkeys() { //Read the state of the I2C chips. 1 is open, 0 is closed.
  Wire.requestFrom(0x38, 1); // read the data from chip 1 in data1
  if (Wire.available()){
     data1 = Wire.read(); 
 
  }
  Wire.requestFrom(0x39, 1); // read the data freom chip 2 into data2
  if (Wire.available()){
     data2 = Wire.read();    
  }
 
  for (unsigned char i = 0; i &lt; 8; i++) {// puts data bits from chip 1 into keys array        keyispressed[i] = ((data1 &gt;&gt; i) &amp; 1); // set the key variable to the current state. chip 1
       keyispressed[i + 8] = ((data2 &gt;&gt; i) &amp; 1); //chip 2
  }
}  
//-------------------------------------
void sendMIDI() { // Send MIDI instructions via the MIDI out
  int note; //holder for the value of note being played
 
  velocity = analogRead(potpin)/8;// Read velocity from the pot
  for (unsigned char i = 0; i &lt; 16; i++) { //for each note in the array
    if (keyispressed[i] == LOW){ //the key on the board is pressed 
      if(!noteisplaying[i]){ //if the note is not already playing send MIDI instruction to start the note
         note = i + (octave*12) + transpose;
         MIDI.sendNoteOn(note,velocity,1);  // Send a Note ( vel.127  ch. 1)
         noteisplaying[i] = note; // set the note playing flag to TRUE and store the note value
      }
    }
    else{
      if(noteisplaying[i]){ //if the note is currently playing, turn it off
        note = noteisplaying[i]; //retrieve the saved note value incase the octave has changed
        MIDI.sendNoteOff(note,0,1);   // Stop the note
        noteisplaying[i] = 0; // clear the note is playing flag
      }
    }
  }
}
//----------------------------------------
void checkoctave(){
  int up = digitalRead(octaveup);
  int down = digitalRead(octavedown);
 
  if(up == HIGH){ //up button pressed
    if(octave &lt; 4){       octave++;     }     while(up == HIGH){// wait until button is released       up = digitalRead(octaveup);       delay(20);     }     displayoctave();   }   if(down == HIGH){ //down button pressed     if(octave &gt; 1){
      octave--;
    }
    while(down == HIGH){// wait until button is released
      down = digitalRead(octavedown);
      delay(20);
    }
    displayoctave();
  }
}
//-------------------------------------
void displayoctave(){
  // clear all the LEDs
  digitalWrite(octave1, LOW);
  digitalWrite(octave2, LOW);
  digitalWrite(octave3, LOW);
  digitalWrite(octave4, LOW);
  // ...and then display the current octave
  switch(octave){ // display the current actave
  case 1: digitalWrite(octave1, HIGH); 
          break;
  case 2: digitalWrite(octave2, HIGH); 
          break;
  case 3: digitalWrite(octave3, HIGH); 
          break;
  case 4: digitalWrite(octave4, HIGH); 
          break;
  }
}

If I am reading this sketch right the parts that pertain to my code are these parts?

//--- Octave Switches 
int octaveup = 7;
int octavedown = 6;

 //--- Set up pins
  pinMode(octaveup, INPUT);
  pinMode(octavedown, INPUT);
  pinMode(octave1, OUTPUT);
  pinMode(octave2, OUTPUT);
  pinMode(octave3, OUTPUT);
  pinMode(octave4, OUTPUT);

  //----------------------------------------
void checkoctave(){
  int up = digitalRead(octaveup);
  int down = digitalRead(octavedown);
 
  if(up == HIGH){ //up button pressed
    if(octave &lt; 4){       octave++;     }     while(up == HIGH){// wait until button is released       up = digitalRead(octaveup);       delay(20);     }     displayoctave();   }   if(down == HIGH){ //down button pressed     if(octave &gt; 1){
      octave--;
    }
    while(down == HIGH){// wait until button is released
      down = digitalRead(octavedown);
      delay(20);
    }
    displayoctave();
  }
}

I just want to make sure I am on the right track.