I would recommend using state change detection. It eliminates having to use extra flags:
// pin to use for indicator LED
const int ledPin = 12;
// pin that is connected to the footswitch
const int buttonPin = 2;
int buttonState = 0;
int buttonLastState = digitalRead(buttonPin);;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// Setup serial port for MIDI communication
Serial.begin(31250);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != buttonLastState)
{
delay(50); // debounce
buttonLastState = buttonState;
if (buttonState == LOW) // went from HIGH to LOW (closed)
{
// mute and light up LED
digitalWrite(ledPin, HIGH);
// send CC 85 on channel 2 with value 0 to mute
// mute group 6 on a Behringer X32
midiCc(2, 85, 0);
}
else // went from LOW to HIGH (opened)
{
// unmute and dim LED
digitalWrite(ledPin, LOW);
// send CC 85 on channel 2 with value 127 to mute
// unmute group 6 on a Behringer X32
midiCc(2, 85, 127);
}
}
}
void midiCc(int channel, int command, int value) {
Serial.write(175 + channel);
Serial.write(command);
Serial.write(value);
}