The Button class constructs objects that represents a single Button attached to the physical board.

Parameters

  • pin A Number or String address for the Button pin (digital).

    var button = new five.Button(8);
    
    // Attached to an analog pin
    var button = new five.Button("A0");
    
  • Options An object of property parameters.

    PropertyTypeValue/DescriptionDefaultRequired
    pinNumber, StringDigital Pin. The Number or String address of the pin the button is attached to, ie. 5 or “I1”yes
    invertBooleantrue, false. Invert the up and down values. This is useful for inverting button signals when the pin itself doesn’t have built-in pullup resistor capabilities.falseno
    isPullupBooleantrue, false. Initialize as a pullup buttonfalseno
    isPulldownBooleantrue, false. Initialize as a pulldown buttonfalseno
    holdtimeNumberTime in milliseconds that the button must be held until emitting a "hold" event.500msno

Shape

Property NameDescriptionRead Only
idA user definable id value. Defaults to a generated uidNo
pinThe pin address that the Button is attached toNo
downValue0 or 1, depending on invert or pullupNo
upValue0 or 1, depending on invert or pullupNo
holdtimemillisecondsNo

Component Initialization

Basic

new five.Button(2);

button breadboard

Inverted

new five.Button({
  pin: 2, 
  invert: true
});

button breadboard

Usage

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

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

  // Create a new `button` hardware instance.
  var button = new five.Button(2);

  button.on("hold", function() {
    console.log( "Button held" );
  });

  button.on("press", function() {
    console.log( "Button pressed" );
  });

  button.on("release", function() {
    console.log( "Button released" );
  });
});

Events

  • hold The button has been held for holdtime milliseconds

  • down, press The button has been pressed.

  • up, release The button has been released.

Collection

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

new five.Buttons([ 2, 3, 4, 5 ]);
new five.Buttons([ { pin: 2 }, { pin: 3 }, { pin: 4 }, { pin: 5 } ]);
new five.Buttons([ button1, button2, button3 ]);

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