Posting here since it is quite long, but it's not a complete answer, just a few thoughts and a possible improvement.
Ok, so there are a couple of things in your code I don't understand:
1: in the setup()
{
int i;
for (i = 0; i < 4; i++);
}
{
int j;
for (j = 0; j < 4; j++);
}
What are these for?
2: debouncing
You are not implementing it correctly, at least not for me. You are reading the button every 50ms, without debouncing. Ok, you won't see "bounces", but there are better solutions (see later).
3: modes 1 and 3
They are quite useless. What are they for?
4: pins 6,7,8,9
What are they for?
5: variable laststate
What is it for? You never use it
6: updating one button per loop
Is it needed? I mean, you are just updating one button per loop instead of all of them for each loop.
In the end
Ok, now my suggestions, apart from learning to indent the code (otherwise it's impossible to read it): I always use the bounce2 class to debounce a button, since it contains everything inside and.. Well, it works well. It can also detect the rising and falling edges of the button, thing that you need if I interpreted correctly your code.
One more thing: debounce delay can be lowered (even to just 5ms):
#include <MIDI.h>
#include <Bounce2.h>
int buttonPin[] = {2, 3, 4, 5};
int buttonPinM[] = {6, 7, 8, 9};
Bounce debouncers[4];
Bounce debouncersM[4];
int debounceDelay = 50; // Usually is lower - even 5ms is fine
uint8_t i = 0;
void setup ()
{
MIDI.begin(MIDI_CHANNEL_OMNI);
for (i = 0; i < 4; i++)
{
pinMode(buttonPin[i], INPUT);
debouncers[i].attach(buttonPin[i]);
debouncers[i].interval(debounceDelay);
pinMode(buttonPinM[i], INPUT);
debouncersM[i].attach(buttonPinM[i]);
debouncersM[i].interval(debounceDelay);
}
}
void loop ()
{
for (i = 0; i < 4; i++)
{
debouncers[i].update();
debouncersM[i].update();
}
for (i = 0; i < 4; i++)
{
if (debouncers[i].rose()) // button i switched from 0 to 1
MIDI.sendNoteOff(i+55, 0, 1);
if (debouncers[i].fell()) // button i switched from 1 to 0
MIDI.sendNoteOn (i+55, 127, 1);
if (debouncersM[i].rose()) // button i switched from 0 to 1
MIDI.sendNoteOn (j+61, 127, 1);
if (debouncersM[i].fell()) // button i switched from 1 to 0
MIDI.sendNoteOff(j+61, 0, 1);
}
}