These slides: slides.cuban.tech/nodebots.html
Network: cubantech
Password: meet-ups
- This workshop is based heavily on a workshop by NY-Javascript (see bit.ly/nyjs-nodebots)
- ... which is based heavily on a workshop by Francis Gulotta and Rick Waldron
- You can find the slides for that workshop at gul.ly/3tjj
- You can sign up for one of their workshops on the Nodebots NYC Meetup page
Upcoming CubanTech Events
If you have trouble finding a component, let us know and we'll get you a replacement
- LEDs (Light Emitting Diodes)
- Buttons
- Servos
Feel free to select the components you like most and complete the challenges that most interest you
--
Download Node from the Internet
Download Node from LAN
--
mkdir nodebots
cd nodebots
--
npm install johnny-five
... with ...
--
TODO : Document Docker install
- Allows Johnny-Five to communicate with the Arduino over USB
- Most of the Arduinos already have Standard Firmata installed from previous workshops
- Let's check to make sure your Arduino already has Standard Firmata installed!
Create the file
- Go to your nodebots directory
- Create a file called test.js
- Copy the code below to it and save
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var led = new five.Led(13);
led.blink(500);
});
Run the code
node test.js
If it works, continue on! →
If not, press ↓ for instructions on flashing your Arduino with Standard Firmata
--
- Download the Arduino IDE
- Make sure your Arduino is connected via USB
- Open the Arduino IDE
- Select:
File > Examples > Firmata > StandardFirmataPlus
- Select:
Tools > Board > Arduino/Genuino Uno
- Select:
Tools > Port > <your Arduino>
- Click the Upload Button
- Retry making that LED blink
- LEDs (Light Emitting Diodes)
- Buttons
- Servos
- Long pin is positive (and goes to power)
- Short pin is negative (and goes to ground)
Save this to a file and run it
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
- You probably noticed the light blinks about every .5 seconds
- Change the code to blink at a different rate and then rerun it to make sure it works
- If you're stuck, press ↓ to see a potential solution
--
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
- Stands for Read Evaluate Print Loop
- Allows us to type code in our terminal and see it affect our robots
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-boss.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
});
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
- Press → to move on to the next component
- Press ↓ to scroll through the LED challenges
--
Try to solve them yourself before looking at the solution!
Press ↓ to scroll through the following challenges (and potential solutions)
- Multiple Lights
- Holiday Lights
- Binary Counter
--
Have 2 (or more) lights alternate blinking
--
--
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.
- Off
- Solid
- Blinking
- Pulsing (fading in and out)
- Different speeds of blinking, pulsing, or alternating
- Alternating which lights are on
--
--
--
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);
});
--
- Expose a function to the REPL that allows you to switch to the next setting from the REPL
- Add a button that when pressed will go to the next setting (N.B: you should complete the Button Component slides before attempting this)
--
--
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
});
});
--
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.
--
--
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);
});
--
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var leds = [new five.Led(9), new five.Led(10), new five.Led(11)];
var num = 0;
setInterval(function() {
var mask = 1;
for (var i = 0; i < leds.length; ++i, mask <<= 1) {
var led = led[i];
num & mask? led.on() : led.off();
}
num = (num + 1) % 8;
}, 1000);
});
--
Allow the user to enter a number through the REPL and display it in binary on the LEDs
--
You're on your own for this one!
- LEDs (Light Emitting Diodes)
- Buttons
- Servos
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
- Press → to move on to the next component
- Press ↓ to scroll through the button challenges
--
Try to solve them yourself before looking at the solution!
Press ↓ to scroll through the following challenges (and potential solutions)
- Light Switch
- Passcode
- Holiday Lights
--
Have pressing a button alternate turning an LED on and off
--
--
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.
--
--
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.
- Off
- Solid
- Blinking
- Pulsing (fading in and out)
- Different speeds of blinking, pulsing, or alternating
- Alternating which lights are on
--
You're on your own for this one!
- LEDs (Light Emitting Diodes)
- Buttons
- Servos
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
Type these commands in the REPL and watch how the servo reacts
> 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();
Now that you've got the basics of servos, you can either move on to the next component, or work on some servo challenges
Press → to move on to the next component Press ↓ to scroll through the servo challenges
--
Try to solve them yourself before looking at the solution!
Press ↓ to scroll through the following challenges (and potential solutions)
- Sprinkler
- Arrows
- Button
--
Make the servo rotate back and forth like a sprinkler
--
--
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
--
--
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
--
--
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!
- Thank you for coming!
- We'd love your feedback: bit.ly/nodebots-feedback
- Please put away kits (you can buy your own here)