Latching Footswitch to MIDI Converter

Hi, I am struggling with getting this sketch to work, can anyone help?

// pin to use for indicator LED
int ledPin = 12;
// pin that is connected to the footswitch
int buttonPin = 2;

int buttonState = 0;
int buttonLastState = 0;
int buttonInitState = 0;

int muteStatus = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  // Setup serial port for MIDI communication
  Serial.begin(31250);

}

void loop() {
  buttonState = digitalRead(buttonPin);

  // status?
  if (buttonState == 1) {
    // switch closed
    if (muteStatus == 0) {
      // 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);
      muteStatus = 1;
    }

    buttonState = digitalRead(buttonPin);

    if (buttonState == 0) {
      // switch open
      if (muteStatus == 1) {
        // 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);
        muteStatus = 0;
      }
    }
    // workaround to prevent fluttering of button state
    delay(10);
  }
}

void midiCc(int channel, int command, int value) {
  Serial.write(175 + channel);
  Serial.write(command);
  Serial.write(value);
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You don't say what the problem actually is

What should the code do ?
What does it actually do ?

Thanks for your response - sorry to say that I don't fully understand the "Code Tags" mentioned.

The circuit is simple - a locking Footswitch is connected between pin2 & GND an LED between 12 & GND operation of the footswitch cycles the LED and sends a MIDI Control Change to a digital audio mixer where it mutes the effects (reverb) return to turn it off between songs. The sketch has to sense the state of the footswitch and send one Control Change message for each state.

If I could just get the LED to turn on when the switch is closed and then off when it is opened then my problem would be solved as it would be easy to insert the code to send the Control Change.

Its important that the Arduino doen't end up sending a stream of Control Change messages - just one to turn it on ands one to turn it off.

The problem at the moment is that the LED just stays on all the time.

I am an absolute beginner at this so apologies again if I formatted the question incorrectly.

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);
}

Perfect! Thank you so much for sorting this out.

If you auto-formatted your code you can see that in loop() you had all of the processing within the if (buttonState == 1) conditional. buttonState is 1 (HIGH) when the switch is open and so you immediately turned on the LED. When buttonState was 0 (LOW) you never turned the LED off because everything was within the if (buttonState == 1) conditional.

void loop() {
  buttonState = digitalRead(buttonPin);

  // status?
  if (buttonState == 1) {
    // switch closed
    if (muteStatus == 0) {
      // 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);
      muteStatus = 1;
    }

    buttonState = digitalRead(buttonPin);

    if (buttonState == 0) {
      // switch open
      if (muteStatus == 1) {
        // 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);
        muteStatus = 0;
      }
    }
    // workaround to prevent fluttering of button state
    delay(10);
  }
}

Then you did not follow the advice in the link I posted or did not read it

The easier you make it to read and copy your code the more likely it is that you will get help

I did follow and read the link but couldn't get it (probably due to having spent most of the day struggling with my sketch and being at the end of my tether). I have now revisited it and even then couldn't get it until I realised that the </> was on the post (yes I know it says that) and not on the sketch box on my computer. I now know how it is done and I have edited my original post.

Thank you for adding the tags

The easiest way to post code is to use "Copy for forum" in the IDE which adds the code tags to the code on the clipboard so that it can simply be posted here complete with the tags