const int NUM_BTNS = 5;
const int BTN_PINS[] = {12, 11, 10, 9, 8};
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
void checkButtons() {
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
Serial.print("Button ");
Serial.print(i + 1);
if (btnState[i] == LOW) { // was just pressed
Serial.println(" pressed.");
} else { // was just released
Serial.println(" released.");
}
delay(20); // debounce
}
}
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
oldBtnState[i] = HIGH; // pins idle HIGH
}
}
void loop() {
checkButtons();
}