Skip to main content

Joystick

Analog
input
Medium RAM
0.5.2

#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.

#Functions



#JavaScript / Node.js

ClassJoystickextends ComponentBase

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

TypeScript
interface JoystickValue {
  x: number;
  y: number;
  pressed: boolean;
}

#Constructor options

OptionTypeRequiredDescription
pin1AnalogPinrequired
pin2AnalogPinrequired
invertXbooleanoptional
invertYbooleanoptional
rangenumberoptional
axisThresholdnumberoptional
detectButtonbooleanoptional

#Events

EventParametersDescription
changevalue: JoystickValue
down
up

#Methods

get xnumber

Get current X position.

get ynumber

Get current Y position.

get pressedboolean

Get current button press state.

get valueJoystickValue | undefinedfrom ComponentBase

Get the current value of the component.

#Example

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

ClassJoystickextends ComponentBase

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

C++
#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)
ParameterTypeDefault
busEventBusrequired
pinXuint8_trequired
pinYuint8_trequired
evtMoveuint8_trequired
evtDownuint8_trequired
evtUpuint8_trequired
invertXboolfalse
invertYboolfalse
rangeint50
axisThresholdint6
sampleIntervalMsuint16_t5
minMoveEmitMsuint16_t20
quantizeStepint0
clickThresholdint1020
clickDebounceMsuint32_t30
minBtnEmitMsuint16_t0

#Methods

x()int

Preferred outputs (TRANSFORMED values for triggers, snapshot tied to last emit)

y()int
rawX()int

Most recent sample (raw ADC, may be newer than last emitted move)

rawY()int
moveX()int

Snapshot tied to evtMove (use these in evtMove handlers!)

moveY()int
isPressed()bool

Button state (debounced)

getAxisThreshold()int

Axis threshold (for trigger compatibility)

emittedValue()const char*

Format: "scaledX:scaledY:pressed" (e.g., "-25:15:1") - transformed values -range..+range

onHold(holdMs, cb, ctx)int

Fires once per press when held >= holdMs (same semantics as your Button::onHold)

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