Files
ExploringUncertainties/button_test/button_test.ino
T

36 lines
917 B
Arduino

/*
- Wiring: connect each button between its pin and GND — no resistors needed.
- Open the Serial Monitor at 9600 baud in the Arduino IDE to see the output.
*/
const int NUM_BUTTONS = 12;
const int BUTTON_PINS[NUM_BUTTONS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
bool lastState[NUM_BUTTONS];
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
lastState[i] = HIGH;
}
Serial.println("Button test ready. Press a button!");
}
void loop() {
for (int i = 0; i < NUM_BUTTONS; i++) {
bool currentState = digitalRead(BUTTON_PINS[i]);
if (currentState == LOW && lastState[i] == HIGH) {
Serial.print("Button ");
Serial.print(i + 1);
Serial.print(" pressed! (pin D");
Serial.print(BUTTON_PINS[i]);
Serial.println(")");
}
lastState[i] = currentState;
}
delay(50); // simple debounce
}