Skip to content
Rick Waldron edited this page Mar 22, 2023 · 6 revisions

The Leds class constructs a collection object containing multiple Led objects. Any method called on an Leds object will be called on each member of the Leds object with the same parameters.

See also:

Parameters

  • numsOrObjects An array of pins, Led parameter objects and/or Led objects:

    Property Type Value/ Description Default Required
    numsOrObjects Array An element for each Led. Any valid Led parameters will work yes

Component Initialization

###With Pins

// Create three basic Leds
//
new five.Leds([9, 10, 11]);

###With Objects

// Create two Leds
//
new five.Leds([{
  pin: 9
}, {
  pin: 10
}]);

Usage

Control all members simultaneously:

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

board.on("ready", function() {

  var leds = new five.Leds([3, 5, 6]);

  // Pulse all leds in the object.
  leds.pulse();
});

leds

Control a single Led in an Leds instance:

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

board.on("ready", function() {

  var leds = new five.Leds([9, 10]);

  // Pulse the Led on pin 9.
  leds[0].pulse();
});

Using multiple controllers in a single Leds instance:

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

board.on("ready", function() {

  var leds = new five.Leds([
    { controller: "PCA9685", pin: 0 }, // Attached to an Adafruit PWM shield
    { controller: "PCA9685", pin: 1 }, // Attached to an Adafruit PWM shield
    { pin: 3 } // Attached directly to the Arduino
  ]);

  // Pulse both leds.
  leds.pulse();
});

Two Controllers

Using Led objects in Leds:

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

board.on("ready", function() {

  var ledA = new five.Led(9);
  var ledB = new five.Led(10);

var lights = new five.Leds([ledA, ledB]);

  // Set leds independently
  ledA.brightness(20);
  ledB.brightness(255);

  // Pulse all Leds.
  lights.pulse();

});

API

All methods and properties in the Led API are available on Leds

Events

Events are emitted on the individual Led objects so listeners must be attached there. See Led events

Examples

Clone this wiki locally