Challenge 1

podpi_servo_banner.png
 
 

Welcome to your first challenge

Make sure to connect the light sensor just like previous lesson and a servo motor as shown in the magazine.

Here is the code

// load the Johnny-Five Library
var five = require('johnny-five');

// create a board variable to connect to
var board = five.Board();

// wait for the Arduino to respond
board.on("ready", function() {

  // add a Servo motor on Pin 11
  var servo = new five.Servo({pin:11});
  // add a sensor on analog Pin A0
  var light = new five.Sensor("A0");

  // when light data is received
  light.on("change", function() {
    console.log(this.value);
    // set the servo to 1/6 of the value
    servo.to(Math.abs(this.value/6));
  });
});

Note: We are dividing the value of the light received by 6 in order to map the 10-bit value (a value from 0 to 1023) to an angle ranging from 0 to 180.