-
Notifications
You must be signed in to change notification settings - Fork 0
Board
The Board
class constructs objects that represent the physical board itself. All device objects depend on an initialized and ready board object.
Johnny-Five has been tested on, but is not limited to, the following boards:
See also: Multi-Board Support
- options Optional object of themselves optional parameters.
Property Name | Type | Value(s) | Description | Required |
---|---|---|---|---|
id | Number, String | Any | User definable identification | no |
port | String | "/dev/ttyAM0", "COM1" | Path or name of device port/COM | no |
{
ready: ...A boolean value that indicates the readiness of the physical board
firmata: ...A reference to the protocol layer
id: ...A user definable id value. Defaults to a generated uid
pins: ...An array of all pins produced by the capabilities query
type: ...A string identifier of the board type, one of: "UNO", "MEGA", "LEONARDO" or "UNKOWN"
repl: ...A reference to the repl session object
port: A string illustrating the device path or COM address
}
The easiest way to initialize a board object is to call the Board
constructor function with new
. Don't worry about knowing your device's path or COM port, Johnny-Five will figure out which USB the board is plugged into and connect to that automatically.
var five = require("johnny-five"),
board = new five.Board();
You may optionally specify the port by providing it as a property of the options object parameter:
var five = require("johnny-five"),
board = new five.Board({ port: "/dev/tty.usbmodemNNNN" });
or
var five = require("johnny-five"),
board = new five.Board({ port: "COM1" });
Once the board object has been initialized, it must connect to the physical board with a set of handshake steps, once this has completed, the board is ready to communicate with the program. This process is asynchronous, and signified to the program via a "ready" event.
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// The board can now communicate with this program.
});
A basic, but complete example usage of the Board
constructor:
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Create an Led on pin 13
var led = new five.Led(13);
// Strobe the pin on/off, defaults to 100ms phases
led.strobe();
});
-
repl This is a property of the board object that represents the automatically generated REPL that's created when a Johnny-Five program is executed. This object has an
inject
method that may be called as many times as desired. -
repl.inject(object) Inject objects or values, from the program, into the REPL session.
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Initialize an Led object that can be controlled from the REPL session
this.repl.inject({
led: new five.Led(13)
});
});
/*
From the terminal...
> node program.js
1375291815062 Board Connecting...
1375291815081 Serial Found possible serial port /dev/whatever-this-port-is
1375291815082 Board -> Serialport connected /dev/whatever-this-port-is
1375291816115 Board <- Serialport ready /dev/whatever-this-port-is
1375291816116 Repl Initialized
>>
(Since the led object is available here...)
>> led.on();
>> led.off();
*/
-
pinMode(pin, mode) Set the
mode
of a specificpin
, one of INPUT, OUTPUT, ANALOG, PWM, SERVO. Mode constants are exposed via thePin
class
Mode | Value | Constant |
---|---|---|
INPUT | 0 | Pin.INPUT |
OUTPUT | 1 | Pin.OUTPUT |
ANALOG | 2 | Pin.ANALOG |
PWM | 3 | Pin.PWM |
SERVO | 4 | Pin.SERVO |
board.on("ready", function() {
// pin mode constants are available on the Pin class this.pinMode(13, five.Pin.INPUT); });
- **analogWrite(pin, value)** Write an analog value (0-255) to a digital `pin`.
```js
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Assuming an Led is attached to pin 9, this will turn it on at full brightness
// PWM is the mode used to write ANALOG signals to a digital pin
this.pinMode(9, five.Pin.PWM);
this.analogWrite(9, 255);
});
-
analogRead(pin, handler(voltage)) Register a handler to be called whenever the board reports the voltage value (0-1023) of the specified analog
pin
.
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Assuming a sensor is attached to pin "A1"
this.pinMode(1, five.Pin.ANALOG);
this.analogRead(1, function(voltage) {
console.log(voltage);
});
});
-
digitalWrite(pin, value) Write a digital value (0 or 1) to a digital
pin
.
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Assuming an Led is attached to pin 13, this will turn it on
this.pinMode(13, five.Pin.OUTPUT);
this.digitalWrite(13, 1);
});
-
digitalRead(pin, handler(value)) Register a handler to be called whenever the board reports the value (0 or 1) of the specified digital
pin
.
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Assuming a button is attached to pin 9
this.pinMode(9, five.Pin.INPUT);
this.digitalRead(9, function(value) {
console.log(value);
});
});
-
shiftOut(dataPin, clockPin, isBigEndian, value) Write a byte to
dataPin
, followed by toggling theclockPin
. Understanding Big and Little Endian Byte Order -
wait(ms, handler()) Register a handler to be called once in another execution turn and after the amount of time specified in milliseconds has passed.
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Assuming an Led is attached to pin 13
this.pinMode(13, five.Pin.OUTPUT);
// Turn it on...
this.digitalWrite(13, 1);
this.wait(1000, function() {
// Turn it off...
this.digitalWrite(13, 0);
});
});
- loop(ms, handler()) Register a handler to be called repeatedly in another execution turn, every time the specified milliseconds has lapsed.
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
var byte;
// Assuming an Led is attached to pin 13
this.pinMode(13, five.Pin.OUTPUT);
// Homemade strobe
this.loop(500, function() {
this.digitalWrite(13, (byte ^= 0x01));
});
});