Electromagnet
#Description
The electromagnet can create a magnetic field when power is applied to it. The magnetic field can then manipulate objects or materials affected by magnetism. Under ideal circumstances, the Grove Electromagnet can hold up to 1kg.
The Grove component has a protective circuit in front of the electromagnet. Do not connect your own electromagnets without a circuit protecting against blowback voltage or you might damage your board.
#Links
#Units
- State
#Functions
#JavaScript / Node.js
Base class for digital output components (on/off control, blink, toggle).
#Constructor options
| Option | Type | Required | Description |
|---|---|---|---|
invert | boolean | optional | — |
pin | number | DigitalPin | required | — |
#Methods
on()→ thisTurn the component on (no arguments) or register an event listener.
off()→ thisTurn the component off
toggle()→ thisToggle the component state
set(value: 0 | 1)→ thisSet the value of the component
blink(interval: number = 500)→ thisBlink the component at a given interval (Arduino-side animation)
stop()→ thisStop any running animation (blink, pulse)
get isOn→ booleanCheck if the component is currently on
get isAnimating→ booleanCheck if an animation is running
get value→ number | undefinedfrom ComponentBaseGet the current value of the component.
#Example
import { Board, DigitalActor } from "@blokdots/components";
const board = new Board("/dev/ttyACM0");
await board.open();
const digitalActor = new DigitalActor({ board, pin: "D5" });
digitalActor.on();
digitalActor.off();
digitalActor.toggle();See the DigitalActor reference for the full API including all methods and events.
#C++ (Arduino)
#Setup
#include "blokdots.h"
DigitalActor myComponent(5);#Constructor
DigitalActor(pin, invert, startOn)| Parameter | Type | Default |
|---|---|---|
pin | uint8_t | required |
invert | bool | false |
startOn | bool | false |
DigitalActor(bus, pin, evtChange, invert, startOn)Constructor with EventBus for trigger support
| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
pin | uint8_t | required |
evtChange | uint8_t | required |
invert | bool | false |
startOn | bool | false |
#Methods
on()off()toggle()emittedValue()→ intGet the current state value (0 or 1) for triggers
stop()→ DigitalActor&Stop any running animation (blink/pulse)
blink(intervalMs, durationMs)→ DigitalActor&durationMs=0 => blink forever
| Parameter | Type | Default |
|---|---|---|
intervalMs | uint16_t | 500 |
durationMs | uint32_t | 0 |
blink(onMs, offMs, durationMs)→ DigitalActor&durationMs=0 => blink forever
| Parameter | Type | Default |
|---|---|---|
onMs | uint16_t | required |
offMs | uint16_t | required |
durationMs | uint32_t | 0 |
pulse(onForMs)→ DigitalActor&One-shot pulse: ON for onForMs then OFF (no repeating)
| Parameter | Type | Default |
|---|---|---|
onForMs | uint32_t | required |
See the DigitalActor reference for the full API.