Preset UP / Down via MIDI Foot Switch

Hi guys,

Im trying build a 2 Momentary Switchs Midi Foot Controller with Arduino UNO. Here go my code. Its doesnt work. Help me please

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

int buttonState = 127;
int buttonLastState = 127;
int buttonInitState = 127;

int bankup = 0;

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

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

// read current button state, a press is detected whenever
// state changes to anything else than the init state
buttonInitState = digitalRead(buttonPin);
// we only want to do something on state changes
buttonLastState = buttonInitState;
}

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

// has the state changed?
if (buttonState != buttonLastState) {
buttonLastState = buttonState;
if (buttonState != buttonInitState) {
// button is in pressed state, trigger action
if (bankup == 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(1, 82, 0);
bankup = 0;
} else {
// unmute and turn off LED
digitalWrite(ledPin, LOW);
midiCc(1, 82, 0);
bankup = 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);
}

You are not using the internal pullup resistor, so you have an external resistor, right? How IS the switch wired?

if (bankup == 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(1, 82, 0);
        bankup = 0;
      } else {
        // unmute and turn off LED
        digitalWrite(ledPin, LOW);
        midiCc(1, 82, 0);
        bankup = 0;
      }

If backup is 0, do something, and set it to 0. How will bankup ever have a value other than 0.

Here go my code. Its doesnt work.

Nonsense. The code works fine. It is your expectations that are wrong. Correct them.

Sorry Paul, i put a wrong code. My switch have a 10k Resistor conected on the pin 7 of my arduino and another on the GND. The right code is:


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

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

// read current button state, a press is detected whenever
// state changes to anything else than the init state
buttonInitState = digitalRead(buttonPin);
// we only want to do something on state changes
buttonLastState = buttonInitState;
}

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

// has the state changed?
if (buttonState != buttonLastState) {
buttonLastState = buttonState;
if (buttonState != buttonInitState) {
// button is in pressed state, trigger action
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(1, 82, 0);
muteStatus = 1;
} else {
// unmute and turn off LED
digitalWrite(ledPin, LOW);
midiCc(1, 82, 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 use code tags. If you don't use code tags, the forum software will treat array indexes as formatting. Read the sticky post.