/*
MobiFlight Button Test
Checks 8 push buttons and sends a serial output when any change state.
"Extra" parts shown as examples of input / output devices.
Click+CTRL causes a button to remain depressed.
AnonEngineering 7/23/25
Beerware
*/
const int NUM_BTNS = 8;
const int BTN_PINS[] = {23, 25, 27, 29, 47, 45, 43, 41};
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
int btnValues[NUM_BTNS];
int oldBtnValues[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
if (btnState[i] == LOW) { // was just pressed
btnValues[i] = 1;
} else {
btnValues[i] = 0;
}
delay(20); // debounce
}
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
Serial.println("Button demo\n");
}
void loop() {
checkButtons();
for (int i = 0; i < NUM_BTNS; i++) {
if (btnValues[i] != oldBtnValues[i]) {
oldBtnValues[i] = btnValues[i];
Serial.print("PB");
Serial.print(i);
Serial.print("=");
Serial.println(btnValues[i]);
}
}
}