Sub topic 13
Extending the Pattern: Long Press and Repeat
A debounced button can report more than a simple press. It can also report:
- A short press, when the button is released before a time limit.
- A long press, when the button stays down long enough.
- Repeat events, while the button remains down after the long-press threshold.
This chapter assumes that you know how setup() and loop() work, how INPUT_PULLUP inverts button logic, and how timestamp-based debounce works. The button below connects pin 2 to GND.
Model the Button as States
With INPUT_PULLUP, HIGH means released and LOW means pressed. The important events happen at transitions:
- The stable state changes from released to pressed.
- The stable state remains pressed long enough to become a long press.
- The repeat interval expires while the button is still pressed.
- The stable state changes from pressed to released.
The sketch stores the time when the stable press began. It compares the current time with that value using unsigned subtraction.
Choose the Press Pattern
The behavior should be explicit before you write code. For example, this chapter uses these values:
| Event | Condition | Action |
|---|---|---|
| Short press | Released before 1,000 ms | Print `short press` |
| Long press | Held for 1,000 ms | Print `long press` once |
| Repeat | Still held, every 250 ms after long press | Print `repeat` |
A repeat action should usually happen only after the long-press event. Otherwise, a held button might perform both short-press and repeat actions.
A useful flag, longPressReported, prevents the long-press event from firing on every pass through loop(). The same flag also tells the release handler whether to report a short press.
Complete Example
const uint8_t BUTTON_PIN = 2;
const unsigned long DEBOUNCE_MS = 30;
const unsigned long LONG_PRESS_MS = 1000;
const unsigned long REPEAT_MS = 250;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChangeTime = 0;
unsigned long pressedAt = 0;
unsigned long lastRepeatTime = 0;
bool longPressReported = false;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
unsigned long now = millis();
// Restart the debounce timer when the raw reading changes.
if (reading != lastReading) {
lastChangeTime = now;
lastReading = reading;
}
// Accept the reading only after it has remained unchanged.
if (now - lastChangeTime >= DEBOUNCE_MS &&
reading != stableState) {
stableState = reading;
if (stableState == LOW) {
// A new stable press has started.
pressedAt = now;
lastRepeatTime = now;
longPressReported = false;
} else {
// The stable press has ended.
if (!longPressReported) {
Serial.println("short press");
}
}
}
if (stableState == LOW) {
unsigned long heldFor = now - pressedAt;
if (!longPressReported && heldFor >= LONG_PRESS_MS) {
longPressReported = true;
lastRepeatTime = now;
Serial.println("long press");
}
if (longPressReported &&
now - lastRepeatTime >= REPEAT_MS) {
lastRepeatTime = now;
Serial.println("repeat");
}
}
}The code checks the held-button behavior after processing a possible state change. This means the first long-press check uses the time of the stable press. The subtraction form remains correct when millis() rolls over.
The repeat code updates lastRepeatTime to now after each event. This produces one repeat approximately every 250 milliseconds. If the loop is delayed, it does not produce a large burst of missed repeats.
Common Errors
- Testing
digitalRead(BUTTON_PIN) == HIGHas pressed when usingINPUT_PULLUP. - Reporting a short press immediately when the button goes down. A short press is known only after release.
- Omitting
longPressReported, which causes the long-press action to run continuously. - Using
delay()for the long-press threshold or repeat interval. That prevents other tasks from running. - Resetting the repeat timer on every loop pass instead of only when a repeat event occurs.
Exercise
How would you change the example so that repeating begins immediately when the long press is reported, instead of waiting for the next repeat interval?
Show answer
Perform the repeat action in the same block that reports the long press, or set lastRepeatTime to a time that makes the repeat condition true immediately.