OK, here's some baseline code:
//Radio Buttons 22
const int redledPin = 11; //LED Pin #
const int greenledPin = 13;
const int redbutton = 2; //button Pin #
const int greenbutton = 3;
char redbuttonstate = 0;
char greenbuttonstate = 0;
unsigned long redbuttoncount = 0; //button debounce timer
unsigned long greenbuttoncount = 0;
//"marker" chooses which counter to check
// by Paul___B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
if (millis() - *marker >= interval) {
*marker += interval; //move on ready for next interval
return true;
}
else return false;
}
//Deal with a button read; true if button pressed and debounce is a new event
//Uses reading of button input, debounce store, state store and debounce interval.
//by Paul___B of Arduino Forum
boolean buttondown(char button, unsigned long *marker, char *buttonstate, unsigned long interval) {
switch (*buttonstate) { //odd states if was presses, >= 2 if debounce in progress
case 0: //button up so far,
if (button == HIGH) return false; //nothing happening!
else {
*buttonstate = 2; // record that is now pressed
*marker = millis(); // note when was pressed
return false; //and move on
}
case 1: //button down so far,
if (button == LOW) return false; // noting happening!
*buttonstate = 3; //record that is now released
*marker = millis(); //note when was released
return false; // and move on
case 2: //button was up, now down.
if (button == HIGH) {
*buttonstate = 0; // jackpot! update the state
return true; //because we have the desired event!
}
else
return false; //not done yet; just move on
case 3: // button was down mow up
if (button == LOW) {
*buttonstate = 1; //no, not debounced; revert the state
return false; // false alarm!
}
else {
if (millis() - *marker >= interval) {
*buttonstate = 0; //debounced; update state
return false; // but not the event we want
}
else
return false; //not done yet; just move on
}
default: //error; recover anyway
{
*buttonstate = 0;
return false; // definitly false!
}
}
}
void setup() {
pinMode(redledPin, OUTPUT);
pinMode(greenledPin, OUTPUT);
pinMode(redbutton, INPUT);
pinMode(greenbutton, INPUT);
digitalWrite (redledPin, LOW);
digitalWrite (greenledPin, LOW);
pinMode(redbutton, INPUT);
digitalWrite(redbutton, HIGH); //enable pullup resistor
pinMode(greenbutton, INPUT);
digitalWrite(greenbutton, HIGH); //enable pullup resistor
}
void loop() {
// Toggle LED if button is debounced
if (buttondown(digitalRead(redbutton), &redbuttoncount, &redbuttonstate, 10UL )) {
if (digitalRead(redledPin))
digitalWrite (redledPin, LOW); else
digitalWrite (redledPin, HIGH);
}
// Toggle LED if button is debounced
if (buttondown(digitalRead(greenbutton), &greenbuttoncount, &greenbuttonstate, 10UL )) {
if (digitalRead(greenledPin))
digitalWrite (greenledPin, LOW); else
digitalWrite (greenledPin, HIGH);
}
}
So what do you do with it for 13 buttons?
Well, take everything that refers to "redled" or "redbutton" and make copies of it with another "led" and "button" name or number such as "led3" and "button3" (if you run out of colours). But for 13 of them you are going to run out of pins first unless you use a mega2560. There are however ways of fixing that using shift registers.