byte buttonpin[] = {2, 3}; // button pin array
byte ledpin[] = {4, 5}; // led pin array
const byte pins = sizeof(buttonpin) / sizeof(buttonpin[0]);
bool state[pins]; // states
byte i; // index
void setup() {
Serial.begin(115200); // start serial comm
for (int i = 0; i < 2; i++) { // index
pinMode(buttonpin[i], INPUT_PULLUP); // configure button pins
pinMode(ledpin[i], OUTPUT); // configure led pins
}
display(0); // show state of pins
display(1); // show state of pins
}
void loop() {
if (!digitalRead(buttonpin[i])) { // read button pin
delay(150); // cheap button debounce
state[i] = !state[i]; // change state of button pressed
display(i); // show current states
}
i = !i; // change index
}
void display(byte i) { // format and display states
Serial.print("button ");
Serial.print(i); // button pin
Serial.print(" LED");
Serial.print(i); // led pin
Serial.print(" state ");
digitalWrite(ledpin[i], state[i]); // set led state
Serial.println(state[i]); // show led state
}