Hi. I'm building a double footswitch with led to extend to a midi controller where switch timing is vital. I've taken the basics of this code from a working project, but for some reason it doesn't "behave". The led works (as in the "intro" code and I've tested both buttons using the arduino example code, but the switches don't operate the led on/off as I'd hoped. For the simplest test, I wanted one switch to light the led, other other to switch it off.
Probably something obvious, but it's giving me headaches!
// orange 2 way box midi controller sending CC and note midi data
#define SWITCH1 2
#define SWITCH2 3
#define SWITCHES 2 // how many switches?
int switches[SWITCHES] = { SWITCH1, SWITCH2 };
int switchState[SWITCHES] = { LOW, LOW }; // Initial state of switch
int currentSwitch = 0;
const int led1 = 7; // led 1
// int scene = 1;
unsigned long SwitchMillis = 0; // when button was released
unsigned long DebounceMillis = 20; //
unsigned long CurrentMillis = 0;
void setup() {
pinMode(led1, OUTPUT); // setup LED 1
for (currentSwitch = 0; currentSwitch < SWITCHES; currentSwitch++) {
pinMode(switches[currentSwitch], INPUT); // Set pin for switch
digitalWrite(switches[currentSwitch], HIGH); // Turn on internal pullup
}
for (int i = 0; i < 5; i++) // show "welcome" message
{
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);
}
} // end setup
void loop() {
CurrentMillis = millis(); // get the time at the start of this loop()
if (CurrentMillis - SwitchMillis >= DebounceMillis) //is it time to check the switches?
{
SwitchMillis = millis(); //re-initilize Timer
for (currentSwitch = 0; currentSwitch < SWITCHES; currentSwitch++) {
if ((digitalRead(switches[currentSwitch]) != switchState[currentSwitch]) && (switchState[currentSwitch] == HIGH)) {
switch (currentSwitch) {
case 0:
digitalWrite(led1, HIGH);
break;
case 1:
digitalWrite(led1, LOW);
break;
} // end case
} // end current switch
} // end if changed
switchState[currentSwitch] = digitalRead(switches[currentSwitch]);
} // go thru switches
} // end loop