Reputation: 53
I am programming arduino boards in javascript. I am trying to connect multiple arduino boards using the johnny-five library. I followed the johnny-five documentation and I can strobe the led 13 on both boards at once.
However my issue is I want to control leds one at a time. How can I specifically initialise LEDs on each board?
Upvotes: 1
Views: 1218
Reputation: 1391
There are many examples in the docs for the Boards
(note the plural) class. I've copied the following directly:
The Boards
class constructs a collection object containing multiple board objects. If no arguments are passed, Board
objects will be created for every board detected in the order that the system enumerates them.
See also: Board
The easiest way to initialize multiple board objects is to call the Boards
constructor function with new
. Don't worry about knowing your device's path or COM port, Johnny-Five will figure out which USBs are in use by compatible boards automatically.
// Create 2 board instances with IDs "A" & "B"
// (ports will be initialized in device enumeration order)
new five.Boards([ "A", "B" ]);
Or
// Create two board instances on ports
// "/dev/cu.usbmodem621" and
// "/dev/cu.usbmodem411"
new five.Boards([ "/dev/cu.usbmodem621", "/dev/cu.usbmodem411" ]);
Or
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
new five.Boards(ports);
Once the board objects have been initialized, they must connect to the physical boards with a set of handshake steps, once this has completed, the boards are ready to communicate with the program. This process is asynchronous, and signified to the program via a "ready" event.
// Create 2 board instances with IDs "A" & "B"
new five.Boards([ "A", "B" ]).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
});
Override this by providing explicit port paths:
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
new five.Boards(ports).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
});
A basic, but complete example usage of the Boards
constructor:
// Create 2 board instances with IDs "A" & "B"
new five.Boards([ "A", "B" ]).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
// |this| is an array-like object containing references
// to each initialized board.
this.each(function(board) {
// Initialize an Led instance on pin 13 of
// each initialized board and strobe it.
new five.Led({ pin: 13, board: board }).strobe();
});
});
Override this by providing explicit port paths:
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
new five.Boards(ports).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
// |this| is an array-like object containing references
// to each initialized board.
this.each(function(board) {
// Initialize an Led instance on pin 13 of
// each initialized board and strobe it.
new five.Led({ pin: 13, board: board }).strobe();
});
});
NOTE When using multiple boards, all device classes must be initialized with an explicit reference to the board object that they will be associated to. This is illustrated in the previous code example.
each(callback(board, index)) Call a function once for each board object.
...
One thing not currently mentioned, which I will update to do so, is that this
inside of the ready
handler is an array-like object containing references to each initialized board, in the order that they were created:
var ports = [
{ id: "A", port: "/dev/cu.usbmodem621" },
{ id: "B", port: "/dev/cu.usbmodem411" }
];
// Create 2 board instances with IDs "A" & "B"
new five.Boards(ports).on("ready", function() {
// Both "A" and "B" are initialized
// (connected and available for communication)
this[0]; // <-- this is board A reference
this[1]; // <-- this is board B reference
});
Upvotes: 3
Reputation: 3070
I guess you could access each board using their id
s. Or you can instantiate two boards with different names such as:
var five = require("johnny-five"),
boardNumberOne = new five.Board();
boardNumberTwo = new five.Board();
Upvotes: -1