BLE Midi Controller, slow switch case loop

Hello guys,

So I am super new to this whole programming thing and you could definitely say that I am self taught. I am trying to make a BLE MIDI controller using the Adafruit Feather 32u4. I want to setup 15 buttons to send midi data and I got everything working with one button. I then just basically copy pasted what I had for one button for 15 and everything still works, however there is a considerable amount of latency.

My guess is that my void loop is too long with all the switch cases for each button (if I delete just those multiple sections and leave only one button then there is no discernible latency).

Consolidating the switch case down is above my pay grade so I am at a loss…

midi_Controller_working_but_slight_delay_4.ino (14 KB)

First, use code tags when posting code. See #7 on How to use this forum

Second, instead of button1 through button15, debouncer1 through debouncer15… use Arrays.

const int button[15] = {2, 3, 5, 6, 9, 10, 11, 12, 13, 18, 19, 20, 21, 22, 23};

then you can simplify the code that does the same thing over and over again:

for(int i = 0; i < 15; i++)
  pinMode(button[i],INPUT_PULLUP); // setup button as input

However, none of that is your problem. I think the reason it speeds up when you delete all those switch statements is because you are always doing something in the switch statement regardless of if the button has not been recently (or ever) pressed or unpressed. Instead of doing something every time, only do something when the state changes. See the “change” example included with the library.

I’m fairly sure that the main problem is sending 13 MIDI messages every time through loop().

Whenever you see more than two or three variables that are treated nearly identically and are named the same except for a sequence number you should use an array instead.

To keep from sending the same messages over and over you will need to store a previous button state for EACH button. Another good use for an array.

You can easily do the debounce yourself by keeping track of the time EACH button last changed state. Another good use for an array.

And some constant arrays for the few values that are different for each button: pin number and note number.

void loop() {
  unsigned long currentMillis = millis();
  for (int i=0; i<13; i++) {
    boolean state = !digitalRead(ButtonPins[i]);  // 'true'==pressed, 'false'==released
    // Check for state change and do debounce
    if (state != LastButtonState[i] && currentMillis-LastButtonChangeTime[i] > 50) {
      LastButtonState[i] = state;
      LastButtonChangeTime[i] = currentMillis;
      if (state)
        midi.send(0x90, octave + NoteNumber[i], 0x64);  // Pressed
        midi.send(0x80, octave + NoteNumber[i], 0x64);  // Released

Thanks guys for the help. I figured that I needed to use an array but the way to get the information back from it is confusing to me.

I see people use the same int i=0; i<13; i++ code to recall information from an array. For that to work do I need to define what i is?

I see people use the same int i=0; i<13; i++ code to recall information from an array. For that to work do I need to define what i is?

No. The “int i = 0;” includes the declaration of the local variable ‘i’. The ‘for’ loop is fairly easy to understand:

for (a; b; c)

is roughly equivalent to:

  while (b) {

It is a very useful construct for stepping through sequential values.

“i++;” is shorthand for “i += 1;” which is shorthand for “i = i + 1;”.

You did declare i when you wrote 'int i=0'


It looks like everything is all working. I guess if it works then there is nothing to fix right? haha

Thank you for your help guys.

Separate question:

What is everybody's thoughts on me posting the working code? I don't want to be a jerk that gets help from an open source community and then doesn't contribute at all, but from my internet searches it seems like a lot of people especially wanting to make MIDI devices just want somebody to give them the whole code so they can just install the Arduino IDE and copy paste and have a working MIDI device? That mentality rubs me the wrong way but I was wondering what you guys think?

There is a forum section called "Exhibition / Gallery" which is the place to share (and show off) completed projects.