It's about time!

ED9-C-P02-03 thumb.jpg
 

Page 9 - Code

Use the code below to move the servo motor left and right.

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

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

  var angle = 90;
  var step  = 10;

  var servo = new five.Servo(3);
  var br    = new five.Button("A0");
  var bl    = new five.Button("A2");

  br.on("press", function() {
    angle = angle + step;
    servo.to(angle);
    console.log('going to ' + angle);
  });

  bl.on("press", function() {
    angle = angle - step;
    servo.to(angle);
    console.log('going to ' + angle);
  });

});

Solution for Page 12

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

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

  var angle = 90;
  var servo = new five.Servo(3);
  var br    = new five.Button("A0");
  var bl    = new five.Button("A2");
  var stick = new five.Sensor("A1");
  var flap  = new five.Servo(5);

  stick.on("change", function() {
     flap.to(Math.round(this.vlue/6));
  });

  br.on("press", function() {
    if ( angle < 180 ) {
       angle += 10;
       console.log('going to ' + angle);
       servo.to(angle,1000);
    } else {
       console.log('Reached maximum angle');
    }
  });

  bl.on("press", function() {
    if ( angle > 0 ) {
       angle -= 10;
       console.log('going to ' + angle);
       servo.to(angle);
    } else {
       console.log('Reached minimum angle');
    }
  });

});

Code for Dashboard Clock

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

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

  var ss = 0;
  var servo_ss = new five.Servo(3);
  var servo_mi = new five.Servo(5);
  var servo_hh = new five.Servo(6);

  setInterval(function() {
    console.log('get time');
    var d = new Date();
    var hh = d.getHours();
    var mi = d.getMinutes();
    var ss = d.getSeconds();
    servo_ss.to(ss*3,990);
    servo_mi.to(mi*3,990);
    servo_hh.to(hh*3,990);

    console.log(hh + ":" + mi + ":" + ss);

  },1000);

});

Water Level Sensor - Code

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

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

  var s5 = new five.Sensor({pin:"A5",freq:500});
  var s4 = new five.Sensor({pin:"A4",freq:500});
  var s3 = new five.Sensor({pin:"A3",freq:500});
  var servo = new five.Servo({pin:3,invert:false});
  var v3, v4, v5 = 10;

  s3.on("data", function() {
    if ( this.value < 100 ) v3 = 45;
    else v3 = 5;
  });
  s4.on("data", function() {
    if ( this.value < 100 ) v4 = 55;
    else v4 = 5;
  });
  s5.on("data", function() {
    if ( this.value < 100 ) v5 = 65;
    else v5 = 5;
  });

  setInterval(function() {
    var total = v3 + v4 + v5;
    console.log("TOTAL=" + total );
    servo.to(total,800);
  }, 900 );
});
 

Dashboard Templates

Blank Template

Colored Template

Send us pictures of your own dashboard when completed by email at pi@podpi.com or tweet us a picture @Pod_Pi