Skip to main content

Servo Motor

Analog (pwm)
output
Small RAM
0.1.0

#Description

The servo motor is powerful and can be turned to a specific position. It can only be turned around 180 degrees. This could help to build an open/close mechanism or if you need a controlled position.

#Units

  • Percent%
  • Degree°

#Functions



#JavaScript / Node.js

ClassServoMotorextends AnalogActor

ServoMotor component for controlling servo motors. Extends AnalogActor with servo-specific functionality (0-180 degrees).

#Constructor options

OptionTypeRequiredDescriptionInherited from
startAnglenumberoptional—
minUsnumberoptional—
maxUsnumberoptional—
minValuenumberoptional—AnalogActor
maxValuenumberoptional—AnalogActor
invertbooleanoptional—DigitalActor
pinnumber | DigitalPinrequired—DigitalActor

#Methods

to(degrees: number)→ this

Move servo to a specific angle (in degrees). Overloads: - to(angle) - Immediate move - to(angle, ms) - Timed move over ms - to(angle, ms, rate) - Timed move with rate control

get angle→ number

Get current angle of the servo. Alias for value getter with servo-specific naming.

sweep()→ this

Sweep the servo back and forth. Overloads: - sweep() - Sweep full range (0-180) - sweep(low, high) - Sweep between low and high - sweep(options) - Sweep with full options

stopAfterMs(delayMs: number)→ this

Stop any current animation after a delay.

stopAfterSweepCycles(cycles: number)→ this

Stop a sweep after N full cycles (low->high->low == 1).

detach()→ this

Detach the servo (stop sending PWM signals).

write(angle: number)→ this

Override write method to use servo-specific command. Maps to the 'to' method for immediate positioning.

pulse(interval: number = 1000)→ thisfrom AnalogActor

Pulse the component at a given interval (Arduino-side animation)

fadeTo(targetValue: number, duration: number = 1000)→ thisfrom AnalogActor

Fade to a target value over time (Arduino-side animation)

on()→ thisfrom DigitalActor

Turn the component on (no arguments) or register an event listener.

off()→ thisfrom DigitalActor

Turn the component off

toggle()→ thisfrom DigitalActor

Toggle the component state

set(value: 0 | 1)→ thisfrom DigitalActor

Set the value of the component

blink(interval: number = 500)→ thisfrom DigitalActor

Blink the component at a given interval (Arduino-side animation)

stop()→ thisfrom DigitalActor

Stop any running animation (blink, pulse)

get isOn→ booleanfrom DigitalActor

Check if the component is currently on

get isAnimating→ booleanfrom DigitalActor

Check if an animation is running

get value→ number | undefinedfrom ComponentBase

Get the current value of the component.

#Example

JavaScript
import { Board, ServoMotor } from "@blokdots/components";

const board = new Board("/dev/ttyACM0");
await board.open();

const servoMotor = new ServoMotor({ board, pin: "D5" });


#C++ (Arduino)

ClassServoMotorextends ComponentBase

Actor-style wrapper around Arduino Servo library

#Required libraries

  • #include <Servo.h> — Servo

#Setup

C++
#include "blokdots.h"

ServoMotor myComponent(5);

#Constructor

ServoMotor(pin, invert, startAngle, minUs, maxUs)

Basic constructor (no EventBus)

ParameterTypeDefault
pinuint8_trequired
invertboolfalse
startAngleuint8_t90
minUsuint16_t544
maxUsuint16_t2400
ServoMotor(bus, pin, evtChange, invert, startAngle, minUs, maxUs)

Constructor with EventBus for trigger support (angle change event)

ParameterTypeDefault
busEventBusrequired
pinuint8_trequired
evtChangeuint8_trequired
invertboolfalse
startAngleuint8_t90
minUsuint16_t544
maxUsuint16_t2400

#Methods

to(degrees)

to(degrees [, ms [, rate]])

ParameterTypeDefault
degreesintrequired
to(degrees, ms)
ParameterTypeDefault
degreesintrequired
msuint16_trequired
to(degrees, ms, rate)
ParameterTypeDefault
degreesintrequired
msuint16_trequired
rateuint16_trequired
write(angle)

Keep your existing write() API (manual control cancels animations)

ParameterTypeDefault
angleintrequired
angle()→ int
emittedValue()→ int

For triggers: current value as int

detach()
attached()→ bool
sweep()
sweep(low, high)
ParameterTypeDefault
lowintrequired
highintrequired
sweep(low, high, interval, step)
ParameterTypeDefault
lowintrequired
highintrequired
intervaluint16_trequired
stepuint8_trequired
sweep(options)
ParameterTypeDefault
optionsconst SweepOptionsrequired
stopAfterMs(delayMs)

Stop any current animation after delayMs (0 disables)

ParameterTypeDefault
delayMsuint32_trequired
stopAfterSweepCycles(cycles)

Set to 0 to disable.

ParameterTypeDefault
cyclesuint16_trequired
stop()

stop() Stop any active animation (and clears auto-stop)