Sub topic 8
Reacting to a Press Once, Not Continuously
Assume you know basic Arduino sketches, pinMode(), digitalRead(), digitalWrite(), if statements, and boolean variables. This chapter uses a pushbutton connected between pin 2 and GND. The pin uses Arduino's internal pull-up resistor:
pinMode(2, INPUT_PULLUP);With this wiring, the logic is inverted:
| Button condition | Pin reading |
|---|---|
| Released | `HIGH` |
| Pressed | `LOW` |
Why a continuous test repeats
A loop runs many thousands of times per second. This code checks the current state:
if (digitalRead(2) == LOW) {
// Handle the press
}While the button remains pressed, the condition is true on every pass through loop(). The action repeats continuously. That is useful for actions such as “move while held,” but it is wrong for an action that should happen once, such as toggling a light or counting a press.
To run an action once, detect a state change. A change from HIGH to LOW is called a falling edge. It means the input was released and has just become pressed.
Remembering the previous state
Edge detection compares two values:
- The current reading from
digitalRead(). - The reading from the previous loop iteration.
After comparing them, save the current reading as the previous reading for the next iteration.
const uint8_t BUTTON_PIN = 2;
int previousButtonState = HIGH;
bool ledState = LOW;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
// Start with the actual input state.
previousButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
int currentButtonState = digitalRead(BUTTON_PIN);
// INPUT_PULLUP means HIGH -> LOW is a press.
if (previousButtonState == HIGH && currentButtonState == LOW) {
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
}
previousButtonState = currentButtonState;
}The LED changes only when the input changes from HIGH to LOW. Keeping the button pressed does not create another falling edge, so the LED does not keep toggling.
Initializing previousButtonState in setup() matters. If the button is already pressed when the board starts, the first loop should not mistake the startup condition for a new press.
Understanding the event flow
State changes are not debouncing
A mechanical button can produce several fast HIGH and LOW transitions when its contacts touch. The edge detector treats each falling transition as a press. Therefore, this pattern prevents repetition while a stable button is held, but it does not guarantee one event per physical press.
For real buttons, add timestamp-based debouncing around this state-change logic. Debouncing accepts a new state only after the reading has remained unchanged for a short period.
The same pattern can detect release events. With INPUT_PULLUP, a release is a rising edge:
if (previousButtonState == LOW && currentButtonState == HIGH) {
// Handle one release event.
}You can also detect either edge by checking whether the two states differ:
if (currentButtonState != previousButtonState) {
// The input changed.
}Then inspect currentButtonState to determine whether the change was a press or a release.
Exercise
With INPUT_PULLUP, which comparison detects one button press?
Show answer
previousButtonState == HIGH && currentButtonState == LOW.