The Switch class constructs objects that represent a single Switch attached to the physical board.

Parameters

  • pin A Number or String address for the pin.

  • options An object of property parameters.

    PropertyTypeValue/DescriptionDefaultRequired
    pinNumber, StringAny Pin. The Number or String address of the Switch pinyes
    typeString"NO" or "NC". Indicate if the switch is "normally open" or "normally closed""NO"no

Shape

Property NameDescriptionRead Only
idA user definable id value. Defaults to a generated uid.No
pinThe pin value.No
isClosedBoolean indicating whether the switch is closed.Yes
isOpenBoolean indicating whether the switch is open.Yes

Component Initialization

Any

new five.Switch(8);

// Options object with pin property
new five.Switch({
  pin: 8
});

Switch SPDT

Switch SPDT

Usage

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  var spdt = new five.Switch(8);
  var led = new five.Led(13);

  spdt.on("open", function() {
    led.off();
  });

  spdt.on("close", function() {
    led.on();
  });
});

Events

  • open The open event will emit when the circuit opens.
  • close The close event will emit when the circuit closes.

Collection

Switch supports a Switches collection class, which allows multiple Switch instances to be controlled via a single instance object. Events emitted by instances of the Switch class are forwarded through instances of the Switches class. The handler receives the instance that emitted the event as the first parameter.

new five.Switches([ 2, 3, 4, 5 ]);
new five.Switches([ { pin: 2 }, { pin: 3 }, { pin: 4 }, { pin: 5 } ]);
new five.Switches([ switch1, switch2, switch3 ]);

Examples

Hi! The Johnny-Five community is building new projects every day. We made this newsletter to tell you about what's new, what's good, and what's next for Open Source robotics. Join us in exploring what we can make together.

Fork me on GitHub