Lights and buttons · 05 of 21

Lab: a long press

One button, two meanings. A tap turns the XL LED on or off; holding the button for a second sets it blinking, at the moment the second is up and while the finger is still down. The release after a long press must then do nothing, and one flag makes sure it does.

The same two modules as the counter, in the same sockets, and the same debounce. What is new is a clock: millis() read at the press and compared on every pass of loop(), the pattern in millis() instead of delay().

Where they plug in

ModuleSocketPin
TK04 push buttonD6/D7D7
TK01 XL LEDD2/D3D3

When to decide

When the sketch decides
short press
How long the button is held400 ms
Held
400 ms
Decided at
400 ms
Then
on/off
Released at 400 ms, before 1000 ms: a short press, decided at the release. A short press can only be known once the button comes up, because until then it might still become a long one.

Slide the hold time. Under a second, the press is short, and the sketch can only know that when the button comes up: until then it might still become a long one. Past a second, the sketch does not wait for the release. It acts at 1000 ms with the finger still down, and the blink starting is the sign that the hold has worked. A long press that waits for the release leaves the reader holding on and guessing.

The sketch

  • pressedAt = now when the debounced button goes down, and longDone = false: a fresh press, nothing decided yet.
  • The release branch runs when the button comes up. If longDone is still false, the press was short: the LED goes from off to on or back, and the Serial Monitor prints how long it was held.
  • The long-press check runs on every pass: button down, not yet decided, and now - pressedAt >= HOLD_MS. It sets mode = BLINK and longDone = true once, so holding on for five seconds still makes one long press.
  • The LED is a function of mode. In BLINK it is (now / BLINK_MS) % 2, which flips every 150 ms without a delay(), so the button is still read in between.

Make it yours

  • Change the threshold. HOLD_MS of 600 feels quick; 2000 is hard to trigger by accident, which suits a reset or an erase.
  • Add a very long press. A second flag and a second check at, say, 3000 ms give a third meaning, the way many devices power off.
  • Show the hold. Fade the LED up with analogWrite(LED, (now - pressedAt) / 4) while the button is down, so the reader sees the second filling; D3 is a ~ pin, so it works.

The code

Long_Press.ino

The debounced button gives two moments: pressed, which starts a clock in pressedAt, and released. A release before HOLD_MS is a short press. A separate check, run every time round loop(), fires the long press as soon as the button has been down for HOLD_MS.

/*
  A short press and a long press on one button.
                                          TinkerBlock UNO kit / tkbu

  A short press turns the XL LED on or off. Holding the button
  for a second sets it blinking, at the moment the second is up,
  while the button is still down; the release that follows does
  nothing. A short press stops the blink.

  Wiring: the TinkerBlock UNO shield on the UNO R3.
    TK04 push button in the D6/D7 socket, so it is on D7.
    TK01 XL LED in the D2/D3 socket, so it is on D3.

  Arduino IDE
    Tools > Board: Arduino Uno
    Tools > Port:  the CH340 port (it vanishes when you unplug)
    Serial Monitor at 115200 baud. No library needed.
*/

const int BUTTON = 7;
const int LED = 3;
const unsigned long DEBOUNCE_MS = 20;
const unsigned long HOLD_MS = 1000;
const unsigned long BLINK_MS = 150;

const int OFF = 0, ON = 1, BLINK = 2;
int mode = OFF;

int reading = LOW, state = LOW;
unsigned long lastChange = 0;
unsigned long pressedAt = 0;
bool longDone = false;      // this press has already acted

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON, INPUT);   // the TK04 has its own pull-down
  pinMode(LED, OUTPUT);
}

void loop() {
  unsigned long now = millis();
  int r = digitalRead(BUTTON);
  if (r != reading) {
    reading = r;
    lastChange = now;
  }
  if (reading != state && now - lastChange >= DEBOUNCE_MS) {
    state = reading;
    if (state == HIGH) {              // pressed: start the clock
      pressedAt = now;
      longDone = false;
    } else if (!longDone) {           // let go before HOLD_MS
      mode = (mode == OFF) ? ON : OFF;
      Serial.print("short press, ");
      Serial.print(now - pressedAt);
      Serial.println(" ms");
    }
  }

  // Decide a long press while it is still held, not at release.
  if (state == HIGH && !longDone && now - pressedAt >= HOLD_MS) {
    longDone = true;
    mode = BLINK;
    Serial.println("long press");
  }

  if (mode == BLINK) {
    digitalWrite(LED, (now / BLINK_MS) % 2 ? HIGH : LOW);
  } else {
    digitalWrite(LED, mode == ON ? HIGH : LOW);
  }
}

longDone is the flag. It is set when the long press fires and cleared at the next press, and the release only counts as a short press while it is clear. Without it, every long press ends with a short one that switches the LED off.

View on GitHub · kits/tkbu/hold-to-act/Long_Press/Long_Press.ino @ v1.14

When it does not work

A long press starts the blink and then the LED goes off

The release is being counted as a short press too. Check the release branch tests !longDone and that the long-press check sets longDone = true. That one flag is what keeps the two meanings apart.

A long press only happens when I let go

The long press is being decided in the release branch, from the hold time. Decide it in its own check instead, outside the debounce block: button down, longDone false, and now - pressedAt at least HOLD_MS. Then it fires on time with the finger still down.

Quick taps are sometimes missed

A tap shorter than DEBOUNCE_MS, 20 ms, is never believed. Real taps are far longer than that, so a missed one is usually the cable: check it is latched at both ends and the button is in the D6/D7 socket.

The short-press time printed is a little longer than I pressed

It is measured between debounced edges, and each edge is believed 20 ms after the pin settles. Both ends are late by the same amount, so the length is close; the prints are a guide, not a stopwatch.

Where this goes next

Three meanings from one button, counted by how many presses come close together.

Lab: single, double and triple clicks →

Edit this page — content/books/tkbu/hold-to-act.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

TinkerBlock UNO R3 Starter Kit, 12 Modules

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →