Arduino library · Input

HotButton: an example that runs

Yes. You can use the HotButton library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Pushbutton). The example below, "A click, a short-long pattern and a one-second hold", runs on the Arduino Uno R3.

Built in on Mokxi: #include <HotButton.h> and it builds, with nothing to install. Free, and no account needed.

What HotButton does

Update(), then isSingleClick() to isQuadrupleClick(), pressedFor(ms) and event(SHORT, LONG) for patterns of presses. That is what HotButton, by Rop Gonggrijp, covers. The sketch below uses it on the Arduino Uno: “A click, a short-long pattern and a one-second hold”.

Rop Gonggrijp's HotButton library (0.1.1): clicks, long presses and whole patterns of presses from one button.

Use the pushbutton part between the pin and GND with the pull-up on (the second argument true), or between the pin and 5 V with a pull-down and active HIGH.

The calls follow release 0.1.1 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameHotButton
AuthorRop Gonggrijp
Include#include <HotButton.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesPushbutton

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <HotButton.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "HotButton" by Rop Gonggrijp from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add HotButton to lib_deps in PlatformIO. Mokxi follows the calls of release 0.1.1, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

HotButton myButton(2, true)

Makes myButton, the HotButton object the calls below are made on.

myButton.update()

Updates it.

myButton.isSingleClick()

Returns true or false for "is single click".

myButton.isDoubleClick()

Returns true or false for "is double click".

Example: A click, a short-long pattern and a one-second hold

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <HotButton.h>

HotButton myButton(2, true);   // pull-up on: pressed is LOW

void setup() {
  Serial.begin(115200);
  Serial.println("ready");
}

void loop() {
  myButton.update();
  if (myButton.isSingleClick()) Serial.println("The button was clicked.");
  if (myButton.isDoubleClick()) Serial.println("The button was double-clicked.");
  if (myButton.event(SHORT, LONG)) Serial.println("short, then long");
  if (myButton.pressedFor(1000)) Serial.println("held for a second");
}

Wiring

  • Arduino Uno R3 2 to Pushbutton 1a
  • Arduino Uno R3 GND to Pushbutton 2a

Open it in the editor, wired and ready to run

Try HotButton without installing anything

Open the example, change the sketch, and keep your version in a free account.