Linear Encoder

The Adafruit NeoSlider is a slide potentiometer with four NeoPixels along its track, controlled over I2C via the seesaw chip. The slider reports an absolute position value and can be lit independently of the position. See the Adafruit learn guide for wiring and hardware details.

Connection

Call startLinearEncoder() inside preload(). The device is ready to use by the time setup() runs.

let slider;

function preload() {
  slider = startLinearEncoder(name?, addressOrIndex?);
}

name string | false | null "linearEncoder"

Controls automatic event callback registration. Any function on the sketch named name + EventName (event name capitalised) is called whenever that event fires. With the default name:

function linearEncoderChanged(event) { /* slider moved */ }

Pass false or null to disable automatic registration and use addEventListener instead.

addressOrIndex number 0x30

The I2C address of the device, or an index (0–15) into the address list. Sixteen addresses are available, set by the A0, A1, A2, and A3 solder jumpers on the back of the board:

AddressIndexA0A1A2A3
0x300openopenopenopen
0x311closedopenopenopen
0x322openclosedopenopen
0x333closedclosedopenopen
0x344openopenclosedopen
0x355closedopenclosedopen
0x366openclosedclosedopen
0x377closedclosedclosedopen
0x388openopenopenclosed
0x399closedopenopenclosed
0x3A10openclosedopenclosed
0x3B11closedclosedopenclosed
0x3C12openopenclosedclosed
0x3D13closedopenclosedclosed
0x3E14openclosedclosedclosed
0x3F15closedclosedclosedclosed

Methods

getValue() number

Returns the current slider position as an integer from 0 (one end) to 1023 (the other end).

Methods (NeoPixel)

getBrightness() number

Returns the current brightness level as an integer from 0 (off) to 255 (full brightness). The default is 128.

getLength() number

Returns the number of NeoPixels on the device.

getPixelColor(index?) ColorObject

Returns the last known color of the pixel at index (defaults to 0) as a { red, green, blue } object with values from 0 to 255.

getPixelColors(offset?, length?) ColorObject[]

Returns an array of { red, green, blue } objects for the pixels starting at offset (default 0). If length is omitted, all pixels from the offset to the end are returned.

setBrightness(value) Promise<void>

Sets the global brightness for all pixels. value is clamped to 0–255 and rounded to the nearest integer.

setPixelColor(color) Promise<void>

setPixelColor(index, color) Promise<void>

Sets the color of a single pixel. When called with one argument the first pixel (index 0) is used. color accepts a { red, green, blue } object, a CSS hex string (e.g. "#ff0000"), or a packed 24-bit integer.

setPixelColors(colors) Promise<void>

setPixelColors(offset, colors) Promise<void>

Sets the colors of multiple pixels in one call. When called with one argument the update starts at pixel 0; provide an offset to start further along. Each entry in colors accepts the same formats as setPixelColor. Large updates are automatically split into chunks of 64 pixels.

Methods (BaseDevice)

connect() Promise<Response>

Opens the USB connection and sends the start command to the firmware. Calling it multiple times is safe — subsequent calls return the same promise. In p5.js you do not need to call this manually; startXxx() calls it for you inside preload().

Properties

Properties (BaseDevice)

id { type, address }

Identifies the device. type is the DeviceType enum value, address is the I2C address the device was started with.

isConnected boolean

true once the USB connection has been established and the firmware has acknowledged the device. In p5.js this is always true by the time setup() runs, because startXxx() waits for the connection inside preload().

Events

changed(event) changed

Fires whenever the slider position changes.

Event Argument

All event callbacks receive a single event argument. The event detail is available at event.detail and contains:

event.detail.value number — The current slider position (0–1023) at the time of the event.