const int NUM_BTNS = 10;
const int BTN_PINS[] = {
12, 11, 10, 9, 8, 7, 6, 5, 4, 3
};
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
btnState[i] = digitalRead(BTN_PINS[i]); // check each button
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == 0) { // was just pressed
btnPressed = i + 1;
}
delay(20); // debounce
}
}
return btnPressed;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
}
void loop() {
int btnNumber = checkButtons();
if (btnNumber) {
Serial.print("Button ");
Serial.print(btnNumber);
Serial.println(" pressed.");
}
}