Skip to main content

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.

#Units

  • State

#Functions



#JavaScript / Node.js

ClassButtonextends ComponentBase

Base class for button and switch components with software debouncing and hold detection.

#Constructor options

OptionTypeRequiredDescription
invertbooleanoptional
isPullupbooleanoptional
isPulldownbooleanoptional
holdTimenumberoptional
pinnumber | DigitalPinrequired

#Events

EventParametersDescription
changevalue: number
down
press
up
release
held

#Methods

updateOptions(options: { invert?: boolean })

Update runtime options such as invert direction.

get isPressedboolean

Check if the button is currently pressed

get valuenumber | undefinedfrom ComponentBase

Get 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)

ClassButton

#Setup

C++
#include "blokdots.h"

Button myComponent(eventBus, 5, MY_EVENT, MY_EVENT);

#Constructor

Button(bus, pin, evtDown, evtUp, pullup, debounceMs, invert, sampleIntervalMs)
ParameterTypeDefault
busEventBusrequired
pinuint8_trequired
evtDownuint8_trequired
evtUpuint8_trequired
pullupbooltrue
debounceMsuint32_t30
invertboolfalse
sampleIntervalMsuint16_t2

#Methods

isPressed()bool
emittedValue()int
onHold(holdMs, cb, ctx)int

Returns slot index, or -1 if full.

ParameterTypeDefault
holdMsuint32_trequired
cbEventBus::Handlerrequired
ctxvoidnullptr
setHoldMs(slot, ms)

Optional: update hold time later

ParameterTypeDefault
slotintrequired
msuint32_trequired
begin()

See the Button reference for the full API.