These slides: bit.ly/nyjs-nodebots
Network:
Password:
If you have trouble finding a component, let us know and we'll get you a replacement
We have a bunch of mentors to help you out!
Feel free to flag us down with any questions you may have!
mkdir nodebots
cd nodebots
npm install johnny-five
nodebots
directorytest.js
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(13);
led.blink(500);
});
node test.js
File > Examples > Firmata > StandardFirmataPlus
Tools > Board > Arduino/Genuino Uno
Tools > Port > <your Arduino>
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
// "blink" the led in 500ms on-off phase periods
led.blink(500);
});
node blinky.js
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
// "blink" the led in 3000ms on-off phase periods
led.blink(3000);
});
node blinky.js
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
this.repl.inject({
led: led
});
});
node led-repl.js
> led.on();
> led.off();
> led.blink();
> led.stop();
> led.pulse();
The reason we're able to access the led
object in the REPL is because of this bit of code in the previous example. It exposes the led object to our REPL session.
this.repl.inject({
led: led
});
Breadboards allow us to quickly wire components together for prototyping
Now run one of your programs from before and make sure the LED still blinks
Now that you've got the basics of LEDs, you can either move on to the next component, or work on some LED challenges
Try to solve them yourself before looking at the solution!
Press ↓ to scroll through the following challenges (and potential solutions)
Have 2 (or more) lights alternate blinking
Potential Multiple Lights Solution - Hardware
Potential Multiple Lights Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led1 = new five.Led(10);
var led2 = new five.Led(11);
var flag = false;
setInterval(function() {
if (flag) {
led1.on();
led2.off();
} else {
led1.off();
led2.on();
}
flag = !flag;
}, 500);
});
Make an LED (or multiple LEDs) go through different settings like some holiday lights do. It should change the setting every few seconds. Below are some example settings. You can see an example on the next slide.
Potential Holiday Lights Solution - Hardware
Potential Holiday Lights Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
var setting = 0;
setInterval(function() {
led.stop(); // If we call pulse, we need to stop it
switch (setting) {
case 0:
led.pulse();
break;
case 1:
led.off();
break;
case 2:
led.on();
break;
}
setting = (setting + 1) % 3;
}, 3000);
});
Potential Holiday Lights Bonus 1 Solution - Hardware
Potential Holiday Lights Bonus 1 Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
var setting = 0;
function changeSetting() {
led.stop(); // If we call pulse, we need to stop it
switch (setting) {
case 0:
led.pulse();
break;
case 1:
led.off();
break;
case 2:
led.on();
break;
}
setting = (setting + 1) % 3;
}
this.repl.inject({
cs: changeSetting // Now we can call cs() from the REPL
});
});
Potential Holiday Lights Bonus 2 Solution
You're on your own for this one!
Using 3 LEDs, count from 0 to 7 in binary as shown below. On represents 1 and off repesents 0.
Potential Binary Counter Solution - Hardware
Potential Binary Counter Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led1 = new five.Led(9);
var led2 = new five.Led(10);
var led3 = new five.Led(11);
var num = 0;
setInterval(function() {
var binary = (num).toString(2);
binary.slice(-1) === "1" ? led1.on() : led1.off();
binary.slice(-2, -1) === "1" ? led2.on() : led2.off();
binary.slice(-3, -2) === "1" ? led3.on() : led3.off();
num = (num + 1) % 8;
}, 1000);
});
Allow the user to enter a number through the REPL and display it in binary on the LEDs
Potential Binary Counter Bonus Solution
You're on your own for this one!
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var button = new five.Button(2);
button.on("press", function() {
console.log("Button Pressed!");
});
button.on("hold", function() {
console.log("Button Held!");
});
button.on("release", function() {
console.log("Button Released!");
});
});
node button.js
Try pressing, releasing, and holding the button
You should see some output like this in the REPL
>> Button Pressed!
Button Released!
Button Pressed!
Button Released!
Button Pressed!
Button Held!
Button Held!
Button Released!
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
var button = new five.Button(2);
button.on("press", function() {
led.on();
});
button.on("hold", function() {
led.blink(50);
});
button.on("release", function() {
led.stop().off();
});
});
node button_led.js
The LED should go on when you press, off when you release, and blink when you hold
Now that you've got the basics of buttons, you can either move on to the next component, or work on some button challenges
Try to solve them yourself before looking at the solution!
Press ↓ to scroll through the following challenges (and potential solutions)
Have pressing a button alternate turning an LED on and off
Potential Light Switch Solution - Hardware
Potential Light Switch Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
var button = new five.Button(2);
var on = false;
button.on("press", function() {
if (on) {
led.off();
} else {
led.on();
}
on = !on;
});
});
Have 2 buttons and 1 LED. Make it so you have to press the buttons in a certain order to turn the LED on.
Potential Passcode Solution - Hardware
Potential Passcode Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(11);
var button1 = new five.Button(2);
var button2 = new five.Button(4);
var passcode = "12112";
var presses = "";
button1.on("press", function() {
presses += "1";
if (presses.indexOf(passcode) > -1) {
led.on();
}
});
button2.on("press", function() {
presses += "2";
if (presses.indexOf(passcode) > -1) {
led.on();
}
});
});
Make an LED (or multiple LEDs) go through different settings like some holiday lights do. It should change the setting every time the button is pressed. Below are some example settings.
Potential Holiday Lights Solution
You're on your own for this one!
Take your servo and add one of the attachments to it
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var servo = new five.Servo(11);
this.repl.inject({
servo: servo
});
});
node servo.js
> servo.to(10); // Move to 10 degrees
> servo.to(200); // Move to 200 degrees
> servo.value; // Get current position
> servo.min();
> servo.max();
> servo.range;
> servo.center();
> servo.sweep();
> servo.stop();
Now that you've got the basics of servos, you can either move on to the next component, or work on some servo challenges
Try to solve them yourself before looking at the solution!
Press ↓ to scroll through the following challenges (and potential solutions)
Make the servo rotate back and forth like a sprinkler
Potential Sprinkler Solution - Hardware
Potential Sprinkler Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var servo = new five.Servo(11);
var min = servo.range[0];
var max = servo.range[1];
var value = min;
function step() {
servo.to(value);
value = (value + 45) % max;
setTimeout(step, 500);
}
step();
});
Make pressing the left arrow button rotate the servo one way and pressing the right arrow button rotate the other way
Potential Arrows Solution - Hardware
Potential Arrows Solution - Code
npm install keypress
var five = require("johnny-five");
var keypress = require("keypress");
var board = new five.Board();
board.on("ready", function() {
var servo = new five.Servo(11);
process.stdin.on("keypress", function(ch, key) {
if (key && key.name === "left") {
servo.min();
} else if (key && key.name === "right") {
servo.max();
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
});
Have the servo sweep while a button is held down
Potential Button Solution - Hardware
Potential Button Solution - Code
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var servo = new five.Servo(11);
var button = new five.Button(2);
button.on("press", function() {
servo.sweep();
});
button.on("release", function() {
servo.stop();
});
});
Uh oh! We ran out of slides! Feel free to try out some of the other components in your kit while we add more!