Midiusb.h and matrix buttons integration

I am not new to Arduino, however, I am new to button matrix when it comes to integration with midi, I have not found any examples of this, I have tried 10 different ways so I can not post all codes, but to give you an idea, i get the matrix working just fine:

const int numRows = 5;
const int numCols = 5;

int rowPins[numRows] = {2, 3, 4, 5, 6};
int colPins[numCols] = {7, 8, 9, 10, 16};


bool buttonState[numRows][numCols];

void setup() {
  for (int i = 0; i < numRows; i++) {
    pinMode(rowPins[i], INPUT_PULLUP);
  }

  for (int i = 0; i < numCols; i++) {
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], HIGH);
  }

  Serial.begin(9600);
}

void loop() {
  // Scan the matrix
  for (int col = 0; col < numCols; col++) {
    digitalWrite(colPins[col], LOW);
    for (int row = 0; row < numRows; row++) {
      bool currentState = digitalRead(rowPins[row]) == LOW;
      if (currentState != buttonState[row][col]) {
         buttonState[row][col] = currentState;

        if (currentState) {
          
          Serial.print("Button Pressed: Row = ");
          Serial.print(row + 1);
          Serial.print(", Column = ");
          Serial.println(col + 1);

          
        }
      }
    }

    digitalWrite(colPins[col], HIGH);
  }

}
                • And this is the midi code, i am using, and it works just fine:
#include <MIDIUSB.h>
#include "PitchToNote.h"
#define NUM_BUTTONS 14

const uint8_t button1 = 2;
const uint8_t button2 = 3;
const uint8_t button3 = 4;
const uint8_t button4 = 5;
const uint8_t button5 = 6;
const uint8_t button6 = 7;
const uint8_t button7 = 8;
const uint8_t button8 = 9;
const uint8_t button9 = 20;
const uint8_t button10 = 19;
const uint8_t button11 = 15;
const uint8_t button12 = 14;
const uint8_t button13 = 16;
const uint8_t button14 = 10;
const int intensityPot = 3;  //A3 - input not used right now

const uint8_t buttons[NUM_BUTTONS] = { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14 };
const byte notePitches[NUM_BUTTONS] = { pitchB7, pitchB7b, pitchA7, pitchA7b, pitchG7, pitchG7b, pitchF7, pitchE7, pitchE7b, pitchD7, pitchD7b, pitchC7, pitchB6, pitchB6b };

uint8_t notesTime[NUM_BUTTONS];
uint8_t pressedButtons = 0x00;
uint8_t previousButtons = 0x00;
uint8_t intensity;

void setup() {
  for (int i = 0; i < NUM_BUTTONS; i++)
    pinMode(buttons[i], INPUT_PULLUP);
}

void loop() {
  readButtons();
  readIntensity();
  playNotes();
}

void controlChange(byte channel, byte control, byte value) {
   midiEventPacket_t event = { 0x0B, 0xB0 | channel, control, value };
  MidiUSB.sendMIDI(event);
}

void readButtons() {
  for (int i = 0; i < NUM_BUTTONS; i++)
  {
    if (digitalRead(buttons[i]) == LOW)
    {
      bitWrite(pressedButtons, i, 1);
      delay(50);
    }
    else
      bitWrite(pressedButtons, i, 0);
  }
}

void readIntensity() {

  int val = analogRead(intensityPot);
  intensity = (uint8_t)(map(val, 0, 1023, 0, 127));
}

void playNotes() {
  for (int i = 0; i < NUM_BUTTONS; i++)
  {
    if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
    {
      if (bitRead(pressedButtons, i))
      {
        bitWrite(previousButtons, i, 1);
        noteOn(0, notePitches[i], intensity);
        MidiUSB.flush();
      }

      else
      {
        bitWrite(previousButtons, i, 0);
        noteOff(0, notePitches[i], 0);
        MidiUSB.flush();
      }
    }
  }
}

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = { 0x09, 0x90 | channel, pitch, velocity };
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = { 0x08, 0x80 | channel, pitch, velocity };
  MidiUSB.sendMIDI(noteOff);
}

how are people using button matrix to send midi messages? sometimes I can get the top row working, then I change the code mainly the part under 'void playNotes' changing the order of '<NUM_ROWS * NUM_COLS;...' and many other changes, but never could i get all rows sending data to the end device. can someone direct me to an example of this type of intergrations? or explain how it should work with code examples. Thanks

Have you tried using the Keypad library ?

Sounds like Control Surface: NoteButtonMatrix.ino does what you need.

1 Like

just found that today after typing this, not sure why there is little information out there. if you know of a good source, that would be great.

What kind of information are you looking for?

I appreciate your assistance and have started reviewing the link you provided regarding control_surface.h. As I am relatively new to this topic, I plan to familiarize myself with it before proceeding further.

For context, I am currently in the process of constructing a three-manual organ that incorporates "Stop pulls." These pulls are crucial for implementing matrix buttons, and ideally, I'm aiming for around 20 to 25 buttons. Each button will be associated with a specific stop in the organ program.

Additionally, the setup will include three sliders dedicated to controlling the "swell," and another mechanism for achieving a crescendo. This crescendo function involves a pedal that engages stops one by one, adding to the last until all stops are "pulled" The third pedal will be designated for general volume control.

Thank you again for your guidance, and I'll keep you updated as I progress through the material.

Wow, I assume you are the creator of this library. truly amazing work, check out the photos of the prototype "stop" controller, it works great. I will be doing a video of my progress and referencing your work if you are ok with that.

1 Like

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