code is untested.
edit* moving it into setup and loop to make it clearer
boolean toggleState; // hold current state of switch
boolean lastToggleState; // hold last state to sense when switch is changed
long toggleTimer = millis(); // // debounce timer
void setup(){
// setup stuff here
}
void loop(){
toggleState = digitalRead(toggleSwitchPin);
if (millis() - toggleTimer > 100){ // debounce switch 100ms timer
if (toggleState && !lastToggleState) { // if switch is on but was previously off
lastToggleState = true; // switch is now on
toggleTimer = millis(); // reset timer
// do stuff here
}
if (!toggleState && lastToggleState){ // if switch is off but was previously on
lastToggleState = false; // switch is now off
toggleTimer = millis(); // reset timer
// do stuff here
}
}
}
If you want the same stuff to happen in each case then make a separate function and just call to the function in each case.
There are more efficient ways to do this as well but this is one of the easiest to understand in my opinion.