Help with midi controller transpose button

I found this code on the internet and decided to study programming to use it on my casio keyboard, I have reached the point of perfectly understanding this code and it works! my matrix is ​​7 x 8,
now I want to make while pressing a button the keys become functions to transpose, and to change channel.
someone can helpme to how i can do that please?

Jorge_Midi_Casio.ino (6.7 KB)

const int row1 = 2;
const int row2 = 3;
const int row3 = 4;
const int row4 = 5;
const int row5 = 6;
const int row6 = 7;
const int row7 = 8;
const int row8 = 9;





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



uint8_t keyToMidiMap[64];

boolean keyPressed[64];

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 36 corresponds to C MIDI note.
  keyToMidiMap[0] = 0;
  keyToMidiMap[1] = 36;
  keyToMidiMap[2] = 44;
  keyToMidiMap[3] = 52;
  keyToMidiMap[4] = 60;
  keyToMidiMap[5] = 68;
  keyToMidiMap[6] = 76;
  keyToMidiMap[7] = 84;

  keyToMidiMap[8] = 0;
  keyToMidiMap[1 + 8] = 37;
  keyToMidiMap[2 + 8] = 45;
  keyToMidiMap[3 + 8] = 53;
  keyToMidiMap[4 + 8] = 61;
  keyToMidiMap[5 + 8] = 69;
  keyToMidiMap[6 + 8] = 77;
  keyToMidiMap[7 + 8] = 85;

  keyToMidiMap[16] = 0;
  keyToMidiMap[1 + 16] = 38;
  keyToMidiMap[2 + 16] = 46;
  keyToMidiMap[3 + 16] = 54;
  keyToMidiMap[4 + 16] = 62;
  keyToMidiMap[5 + 16] = 70;
  keyToMidiMap[6 + 16] = 78;
  keyToMidiMap[7 + 16] = 86;

  keyToMidiMap[24] = 0;
  keyToMidiMap[1 + 24] = 39;
  keyToMidiMap[2 + 24] = 47;
  keyToMidiMap[3 + 24] = 55;
  keyToMidiMap[4 + 24] = 63;
  keyToMidiMap[5 + 24] = 71;
  keyToMidiMap[6 + 24] = 79;
  keyToMidiMap[7 + 24] = 87;

  keyToMidiMap[32] = 0;
  keyToMidiMap[1 + 32] = 40;
  keyToMidiMap[2 + 32] = 48;
  keyToMidiMap[3 + 32] = 56;
  keyToMidiMap[4 + 32] = 64;
  keyToMidiMap[5 + 32] = 72;
  keyToMidiMap[6 + 32] = 80;
  keyToMidiMap[7 + 32] = 88;

  keyToMidiMap[40] = 0;
  keyToMidiMap[1 + 40] = 41;
  keyToMidiMap[2 + 40] = 49;
  keyToMidiMap[3 + 40] = 57;
  keyToMidiMap[4 + 40] = 65;
  keyToMidiMap[5 + 40] = 73;
  keyToMidiMap[6 + 40] = 81;
  keyToMidiMap[7 + 40] = 89;

  keyToMidiMap[48] = 0;
  keyToMidiMap[1 + 48] = 42;
  keyToMidiMap[2 + 48] = 50;
  keyToMidiMap[3 + 48] = 58;
  keyToMidiMap[4 + 48] = 66;
  keyToMidiMap[5 + 48] = 74;
  keyToMidiMap[6 + 48] = 82;
  keyToMidiMap[7 + 48] = 90;

  keyToMidiMap[56] = 0;
  keyToMidiMap[1 + 56] = 43;
  keyToMidiMap[2 + 56] = 51;
  keyToMidiMap[3 + 56] = 59;
  keyToMidiMap[4 + 56] = 67;
  keyToMidiMap[5 + 56] = 75;
  keyToMidiMap[6 + 56] = 83;
  keyToMidiMap[7 + 56] = 91;



  // 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);
  pinMode(row6, INPUT);
  pinMode(row7, INPUT);
  pinMode(row8, INPUT);
  pinMode(9, INPUT_PULLUP);


   Serial.begin(31250);
  //Serial.begin(9600);

  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);
    int groupValue6 = digitalRead(row6);
    int groupValue7 = digitalRead(row7);
    int groupValue8 = digitalRead(row8);

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

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

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

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

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

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

      if (groupValue6 != 0 && !keyPressed[col + 40]) {
        keyPressed[col + 40] = true;
        noteOn(0x90, keyToMidiMap[col + 40], noteVelocity);
      }

      if (groupValue7 != 0 && !keyPressed[col + 48]) {
        keyPressed[col + 48] = true;
        noteOn(0x90, keyToMidiMap[col + 48], noteVelocity);
      }

      if (groupValue8 != 0 && !keyPressed[col + 56]) {
        keyPressed[col + 56] = true;
        noteOn(0x90, keyToMidiMap[col + 56], noteVelocity);
      }

    }

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

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

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

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

    if (groupValue6 == 0 && keyPressed[col + 40]) {
      keyPressed[col + 40] = false;
      noteOn(0x90, keyToMidiMap[col + 40], 0);
    }

    if (groupValue7 == 0 && keyPressed[col + 48]) {
      keyPressed[col + 48] = false;
      noteOn(0x90, keyToMidiMap[col + 48], 0);
    }

    if (groupValue8 == 0 && keyPressed[col + 56]) {
      keyPressed[col + 56] = false;
      noteOn(0x90, keyToMidiMap[col + 56], 0);
    }

  }

}



void noteOn(int cmd, int pitch, int velocity) {

  int btn1 = digitalRead (13);
  
if (btn1 == 1) {
    Serial.write(cmd);
    Serial.write(pitch);
    Serial.write(velocity);

 }
   }
1 Like

thank you! i dont know how to put the code visible :sweat:

i tried to do this at final part to transpose 1 semitones but when release the combination of buttons dont change the pitch :



void noteOn(int cmd, int pitch, int velocity) {

  int btn1 = digitalRead (13);
  
if (btn1 == 0 && pitch == 36){
pitch = pitch + 1;
}
if (btn1 == 1) {
    Serial.write(cmd);
    Serial.write(pitch);
    Serial.write(velocity);

 }
   }

Those if statements are mutually exclusive. If the pitch is modified, it can never be sent.

I think I should create a variable x that when I press the key + button, 1 is added each time, and at the end in the serial.write(pitch + x) but I don't know how to do it

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.