TinkerBlock UNO kit/Lights and buttons/06. Lab: single, double and triple clicks
Lights and buttons · 06 of 21

Lab: single, double and triple clicks

One click toggles the XL LED, two dim it, three set it blinking. Presses that come close together are one group, and a group is counted once the button has been up for 400 ms. That wait is the price of a double click: a single click cannot act until it is sure no second one is coming.

The last of the button labs, with the same modules, sockets and debounce as the long press. The timing uses millis() as in millis() instead of delay(), and the action is a function, act(), as in if, for and functions.

Where they plug in

ModuleSocketPin
TK04 push buttonD6/D7D7
TK01 XL LEDD2/D3D3

A group ends with a silence

When two clicks are one double click
2
Clicks
Gap between clicks200 ms
Wait
400 ms
Counted as
2
Last action at
840 ms
Each click lands inside the wait after the one before, so 2 clicks are one group. The sketch counts 2 at 840 ms, once the wait after the last release has run out.

Each release starts a 400 ms wait, shaded in the figure. A press inside the wait joins the group; a wait that runs out with the button up closes it, and only then does the sketch count. Pick two clicks and slide the gap past 400 ms: the double click falls apart into two singles, each acted on separately.

Pick one click. It acts 400 ms after its release, not at it, because until the wait runs out a second click could still arrive. That delay is what supporting a double click costs every single click. A longer GAP_MS is kinder to slow clickers and makes the single click slower; a shorter one does the opposite. There is no setting that is both.

The sketch

  • Press: clicks++. Every debounced change to HIGH adds one to the group.
  • Release: releasedAt = now. The wait starts at the release, so a press held for 300 ms still leaves the full 400 ms for the next one.
  • The end of a group. clicks > 0 && state == LOW && now - releasedAt >= GAP_MS: something was clicked, the button is up, and the wait is over. act(clicks) runs once and clicks goes back to 0.
  • act(n) prints the count and sets mode: 1 toggles off and on, 2 sets DIM, 3 sets BLINK. The last lines of loop() turn mode into an analogWrite on D3 every pass.

Testing state == LOW matters: without it, a group could close in the middle of a held press, 400 ms after the release before it, and act while the finger is still down.

Make it yours

  • Give four clicks a job. Add an else if (n == 4) to act(); the count already reaches it.
  • Combine with the long press. Copy the long-press check from the last lab and have it clear clicks, so a hold never becomes a click.
  • Tune GAP_MS to your own hand. The Serial Monitor prints each group; raise or lower the gap until double clicks never split and singles feel quick enough.

The code

Click_Count.ino

Every debounced press adds one to clicks; every release stamps releasedAt. When the button is up, clicks is above 0 and GAP_MS has passed since the release, the group is over: act(clicks) sets the LED's mode and clicks goes back to 0.

/*
  Single, double and triple clicks on one button.
                                          TinkerBlock UNO kit / tkbu

  Presses close together are one group. A group ends when the
  button has been up for GAP_MS; then the sketch counts it.
    1 click   the XL LED on or off
    2 clicks  dim
    3 clicks  blink
  A single click is only known once the gap has passed, so it
  always acts GAP_MS after the release.

  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 (a ~ pin).

  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 GAP_MS = 400;
const unsigned long BLINK_MS = 150;
const int DIM_LEVEL = 24;

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

int reading = LOW, state = LOW;
unsigned long lastChange = 0;
unsigned long releasedAt = 0;
int clicks = 0;             // presses in the group so far

void act(int n) {
  Serial.print(n);
  Serial.println(n == 1 ? " click" : " clicks");
  if (n == 1) mode = (mode == OFF) ? ON : OFF;
  else if (n == 2) mode = DIM;
  else if (n == 3) mode = BLINK;
  else Serial.println("  nothing does that yet");
}

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) clicks++;      // one more in this group
    else releasedAt = now;            // the gap starts here
  }

  // Up, and quiet for GAP_MS since the last release: count it.
  if (clicks > 0 && state == LOW && now - releasedAt >= GAP_MS) {
    act(clicks);
    clicks = 0;
  }

  if (mode == BLINK) analogWrite(LED, (now / BLINK_MS) % 2 ? 255 : 0);
  else if (mode == DIM) analogWrite(LED, DIM_LEVEL);
  else analogWrite(LED, mode == ON ? 255 : 0);
}

The gap is measured from the last release, not the last press, so a slow press does not eat into it. A fourth click in a group is counted and printed, and nothing is assigned to it yet.

View on GitHub · kits/tkbu/double-click/Click_Count/Click_Count.ino @ v1.14

When it does not work

A double click comes out as two single clicks

The gap between the clicks was longer than GAP_MS, 400 ms, so the first group closed before the second press. Click faster, or raise GAP_MS to 500 or 600. Every single click then waits that much longer before it acts.

A single click feels slow

It is, by design: it acts GAP_MS after the release, because until then a second click could still arrive. Lower GAP_MS to 250 or 300 if your clicks are quick. A device that only needs single clicks should not use this sketch.

Clicks count double, or a triple shows up as four

Bounce is getting through. Raise DEBOUNCE_MS from 20 to 30 or 50; at these speeds a press is still far longer than that. A cable that is not latched at both ends can do the same.

The LED does not dim on a double click

It is on a pin without ~. Keep the XL LED in the D2/D3 socket, which puts it on D3, where analogWrite(LED, 24) is a real dim rather than off.

Where this goes next

The first analogue module: a thumbwheel on A0 sets the LED's brightness.

Lab: a knob that dims the LED →

Edit this page — content/books/tkbu/double-click.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 →