Button
Digital
input
Medium RAM
0.1.0
#Description
Buttons are the very basic inputs used everywhere. They can function as input at your keyboard, start element from your coffee machine or even as touch sensor at your door.
#Links
#Units
- State
#Functions
#JavaScript / Node.js
Base class for button and switch components with software debouncing and hold detection.
#Constructor options
| Option | Type | Required | Description |
|---|---|---|---|
invert | boolean | optional | — |
isPullup | boolean | optional | — |
isPulldown | boolean | optional | — |
holdTime | number | optional | — |
pin | number | DigitalPin | required | — |
#Events
| Event | Parameters | Description |
|---|---|---|
change | value: number | — |
down | — | — |
press | — | — |
up | — | — |
release | — | — |
held | — | — |
#Methods
updateOptions(options: { invert?: boolean })Update runtime options such as invert direction.
get isPressed→ booleanCheck if the button is currently pressed
get value→ number | undefinedfrom ComponentBaseGet the current value of the component.
#Example
JavaScript
import { Board, Button } from "@blokdots/components";
const board = new Board("/dev/ttyACM0");
await board.open();
const button = new Button({ board, pin: "D5", isPullup: false, isPulldown: false, holdTime: 1000 });
button.on("change", (value) => {
console.log("Button changed:", value);
});See the Button reference for the full API including all methods and events.
#C++ (Arduino)
Class
Button#Setup
C++
#include "blokdots.h"
Button myComponent(eventBus, 5, MY_EVENT, MY_EVENT);#Constructor
Button(bus, pin, evtDown, evtUp, pullup, debounceMs, invert, sampleIntervalMs)| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
pin | uint8_t | required |
evtDown | uint8_t | required |
evtUp | uint8_t | required |
pullup | bool | true |
debounceMs | uint32_t | 30 |
invert | bool | false |
sampleIntervalMs | uint16_t | 2 |
#Methods
isPressed()→ boolemittedValue()→ intonHold(holdMs, cb, ctx)→ intReturns slot index, or -1 if full.
| Parameter | Type | Default |
|---|---|---|
holdMs | uint32_t | required |
cb | EventBus::Handler | required |
ctx | void | nullptr |
setHoldMs(slot, ms)Optional: update hold time later
| Parameter | Type | Default |
|---|---|---|
slot | int | required |
ms | uint32_t | required |
begin()See the Button reference for the full API.