Respond to button presses
This project also works with a sound-trigger, motion-trigger, remote-trigger or roller-switch in place of the button.
Connect power bit to button bit, button bit to d0 on Arduino bit, d5 on Arduino bit to bargraph bit.
var five = require("johnny-five"),
board, led, button,
buttonCounter = 0;
board = new five.Board();
board.on("ready", function() {
led = new five.Led(5);
button = new five.Button(0);
button.on("press", function(value){
buttonCounter++;
if (buttonCounter % 2 === 0) {
led.off();
} else {
led.on();
}
console.log("button has been pressed " + buttonCounter + " times");
});
});
You can find a copy of this code in press/press.js
Run the code from the terminal e.g.
node press/press.js
When you press the button, the bargraph will toggle between on and off. The count of how many times the button has been pressed will be printed to the console.
The modulo operator (%) is used to check whether the number of button presses is divisible by 2 to determine what action to perform on each press. You can add more actions to cycle through by increasing this value and adding additional else if
statements e.g.
if (buttonCounter % 3 === 0) {
// led off
led.off();
} else if (buttonCounter % 3 === 1) {
// drop to half brightness
led.brightness(128);
} else {
// led on
led.on();
}