nodebots.io

johnny-five.io





Follow Along
gul.ly/3tjj




Francis Gulotta

Rick Waldron




@reconbot

@rwaldron




Important Points
To Consider


The only dumb question is the one you never asked.


Doesn't work?
It's probably the hardware.



Let's learn about
the kit!



Be Kind, Rewind.

Buy Your Own Here!

The Box

Arduino Uno

Breadboard: Solderless wiring

Breadboard: Electrical Connections

Typical "Projects" Booklet

node-ardx.org by Anna Gerber

Wiring Recipes

Servo: Actuate Physical Movement

Attach The Horn!

Resistors: Reduce Current Flow, lower Voltage Level

LEDs: Light Up Your Life

Buttons, Relay, Potentiometer, Buzzer, Shift Register

Buttons, Potentiometer

Sensor: Temperature

Jumper Wires!





Ready To Make Stuff?

Quick Preparation Stuff

mkdir nodebots;

cd nodebots;

npm init -y;

npm install johnny-five;

open .
  




Return to hardware





Let's Create Output



Connect The Arduino to your computer
via USB.

Don't do this:

$ node
> var j5 = require("johnny-five");
undefined
> var board = new j5.Board();
undefined
> 1432581579628 Looking for connected device
> var board = new j5.Board();
undefined
> 1432581593535 Device(s) /dev/cu.usbmodem1411
1432581593554 Connected /dev/cu.usbmodem1411

undefined
> 1432581594739 Looking for connected device

undefined
>
undefined
  




Make a new file

Turn on the LED

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

board.on("ready", function() {
  var led = new five.Led(11);

  led.on();
});
  

node led-on.js

Make it blink

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

board.on("ready", function() {
  var led = new five.Led(11);

  led.blink();
});
  

node led-blink.js

Change the blink rate.

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

board.on("ready", function() {
  var led = new five.Led(11);

  led.blink(1000); // <-- ms on/off phases
});
  

node led-blink-slow.js

Manual, live control

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

Type these commands in the REPL

> led.on();

> led.off();

> led.blink();

> led.stop();

> led.pulse();

  



Create Digital Input

0|1

LOW = 0 = 0V

HIGH = 1 = ~5V

GND = G = Ground

VCC = V = Voltage

Ohms = Ω = Resistance



V = Voltage measured in volts

I = Current measured in amps

R = Resistance measured in ohms/Ω

Ohm's Law

Ohm's Law Applied

Pull Up Resistor

Limit current flow between VCC and INPUT: a HIGH signal when the button is up, and a LOW signal when the button is down. Pressing the button closes the circuit to GND

Pull Down Resistor

Limit current flow between VCC and GND, resulting in a LOW signal when the button is up, and a HIGH signal when the button is down.

Respond to Button Press & Release

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("release", function() {
    console.log("Button Released!");
  });
});

node button.js

Convert Input to Output

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("release", function() {
    led.off();
  });
});

node button.js

Multiple States of Input to Multiple States of Output

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.js




Observe Analog Input



0-1023

~5V in 10 bits

Side Quest!

Open Developer Tools Console in either Firefox, Chrome or Edge, paste this:


0b1111111111


Hit <enter>

Binary Logarithm: Inverse of POW 2
Number of bits equals integral part of



let bits = n => Math.log2(n) + 1 | 0;


bits(1023) === 10; // true

Receive Values From a Sensor

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

board.on("ready", function() {
  var rotary = new five.Sensor("A0");

  rotary.on("change", function() {
    console.log("Sensor changed!", this.value);
  });
});

node sensor.js




npm install barcli

(say: barklee)

Analog Data

var Barcli = require("barcli");
var five = require("johnny-five");
var board = new five.Board({
  debug: false, repl: false
});
board.on("ready", function() {
  var range = [0, 100];
  var graph = new Barcli({
    label: "Potentiometer",
    range: range,
  });
  var rotary = new five.Sensor("A0");

  rotary.scale(range).on("change", function() {
    graph.update(this.value);
  });
});

node sensor.js




Convert 10-bit Input
to 8-bit Output

PWM

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave, a signal switched between on and off.
- Arduino.cc



8-bit PWM

255 "steps" from 0% to 100% duty cycle


0b00000000 (0) 0b01000000 (64) 0b01111111 (127*) 0b10111111 (191) 0b11111111 (255*)





0b1111111111 0b0011111111





0b1111111111 >> 2 0b0011111111 0b11111111      255 


Convert 10-bit Analog Input to 8-bit PWM Output

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

board.on("ready", function() {
  var led = new five.Led(11);
  var rotary = new five.Sensor("A0");

  rotary.on("change", function() {
    // Analog sensors produce a 10-bit value,
    // but led brightness is an 8-bit PWM value.
    // Convert by shifting the value's bits
    // two places to the right.
    led.brightness(this.value >> 2);
  });
});

node sensor.js




Convert 10-bit Input
to Degrees

Scale [0, 1023] => [0, 180]

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

board.on("ready", function() {
  var servo = new five.Servo(11);
  var rotary = new five.Sensor("A0");

  // scale accepts a single array or two arguments
  rotary.scale(0, 180).on("change", function() {
    servo.to(this.value);
  });
});

node sensor.js




Observe The Environment

Temperature

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

board.on("ready", function() {
  var temp = new five.Temperature({
    controller: "TMP36",
    pin: "A0"
  });

  temp.on("change", function() {
    // "change" will occur as a result of analog line noise.
    // Write a filtering mechanism to fine tune the output.
    console.log(this.celsius + "°C");
  });
});

node temperature.js




Convert Temperature to RGB

Temperature Display

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

board.on("ready", function() {
  var temp = new five.Temperature({
    controller: "TMP36",
    pin: "A0"
  });

  var rgb = new five.Led.RGB({
    pins: [3, 5, 6],
    isAnode: true
  });

  temp.on("change", function() {
    // Write an algorithm that converts temperature (F, K or C)
    // into an RGB hex value.
    rgb.color(??????);
  });
});



Experimentation
Time!

johnny-five.io

github.com/rwaldron/johnny-five

twitter.com/rwaldron/lists/nodebotics

Bocoup

@bocoup

bocoup.com





Bonus Material





Pages in the ARDX Book to ignore

4, 5, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33