Hello
I got tired of lecturing about debouncing buttons and finite state machines tonight, so I added the changes to your sketch. The sketch is untested and may need to be adapted to your MIDI environment.
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
void setup()
{
Serial.begin(31250);
MIDI.begin (MIDI_CHANNEL_OMNI); // Launch MIDI and listen to channel omni
pinMode(2, INPUT_PULLUP);
}
void loop() {
static bool state = 0;
static int stage = 0;
static unsigned long buttonMillis;
const unsigned long buttondelay = 50;
if (millis() - buttonMillis >= buttondelay) {
buttonMillis = millis();
bool pinState = !digitalRead(2);
if (state != pinState) {
state = pinState;
if (state) {
switch (stage) {
case 0:
MIDI.sendControlChange(12, 0, 1);
break;
case 1:
MIDI.sendControlChange(12, 127, 1);
break;
}
stage++;
stage = stage % 2;
}
}
}
}