Joystick
#Description
The Joystick is very similar to a joystick you might find on a gamepad. You can get data from the x and y axis as analog readings.
On the Grove Joystick the button is not connected. If you are using another joystick or you solder something on the Grove Joystick, you can use the button but you will have to set it up separately.
This component requires 2 pins.
#Links
#Functions
#JavaScript / Node.js
Joystick component for reading X/Y position and button state. This component reads from two analog pins for X and Y axes, and derives button press state from X axis saturation (Seeed Grove joystick behavior).
#Value type
interface JoystickValue {
x: number;
y: number;
pressed: boolean;
}#Constructor options
| Option | Type | Required | Description |
|---|---|---|---|
pin1 | AnalogPin | required | — |
pin2 | AnalogPin | required | — |
invertX | boolean | optional | — |
invertY | boolean | optional | — |
range | number | optional | — |
axisThreshold | number | optional | — |
detectButton | boolean | optional | — |
#Events
| Event | Parameters | Description |
|---|---|---|
change | value: JoystickValue | — |
down | — | — |
up | — | — |
#Methods
get x→ numberGet current X position.
get y→ numberGet current Y position.
get pressed→ booleanGet current button press state.
get value→ JoystickValue | undefinedfrom ComponentBaseGet the current value of the component.
#Example
import { Board, Joystick } from "@blokdots/components";
const board = new Board("/dev/ttyACM0");
await board.open();
const joystick = new Joystick({ board, pin1: "D5", pin2: "D5" });
joystick.on("change", (value) => {
console.log("Joystick changed:", value);
});#C++ (Arduino)
Grove - Thumb Joystick (Seeed): press shorts X axis -> X ~ 1023 when clicked. Reads X/Y together and derives button state from X. :contentReference[oaicite:1]{index=1} Events: - evtMove: coalesced movement event (use moveX()/moveY() in handlers) - evtDown / evtUp: button press transitions (debounced) Optional: onHold() (fires once per press when held >= holdMs)
#Setup
#include "blokdots.h"
Joystick myComponent(eventBus, 0, 0, MY_EVENT, MY_EVENT, MY_EVENT);#Constructor
Joystick(bus, pinX, pinY, evtMove, evtDown, evtUp, invertX, invertY, range, axisThreshold, sampleIntervalMs, minMoveEmitMs, quantizeStep, clickThreshold, clickDebounceMs, minBtnEmitMs)| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
pinX | uint8_t | required |
pinY | uint8_t | required |
evtMove | uint8_t | required |
evtDown | uint8_t | required |
evtUp | uint8_t | required |
invertX | bool | false |
invertY | bool | false |
range | int | 50 |
axisThreshold | int | 6 |
sampleIntervalMs | uint16_t | 5 |
minMoveEmitMs | uint16_t | 20 |
quantizeStep | int | 0 |
clickThreshold | int | 1020 |
clickDebounceMs | uint32_t | 30 |
minBtnEmitMs | uint16_t | 0 |
#Methods
x()→ intPreferred outputs (TRANSFORMED values for triggers, snapshot tied to last emit)
y()→ intrawX()→ intMost recent sample (raw ADC, may be newer than last emitted move)
rawY()→ intmoveX()→ intSnapshot tied to evtMove (use these in evtMove handlers!)
moveY()→ intisPressed()→ boolButton state (debounced)
getAxisThreshold()→ intAxis threshold (for trigger compatibility)
emittedValue()→ const char*Format: "scaledX:scaledY:pressed" (e.g., "-25:15:1") - transformed values -range..+range
onHold(holdMs, cb, ctx)→ intFires once per press when held >= holdMs (same semantics as your Button::onHold)
| Parameter | Type | Default |
|---|---|---|
holdMs | uint32_t | required |
cb | EventBus::Handler | required |
ctx | void | nullptr |
setHoldMs(slot, ms)| Parameter | Type | Default |
|---|---|---|
slot | int | required |
ms | uint32_t | required |