Coding the project

Create a new file in your project folder called light-meter.js. Set up your normal scaffolding: requiring in Johnny-Five and Raspi-IO, setting up our Board object, and creating the board.on('ready') handler:

const Raspi = require('raspi-io')
const five = require('johnny-five')

const board = new five.Board({
io: new Raspi()
})

board.on('ready', () => {
})

Inside the board.on('ready') handler, construct your Servo and Light objects:

let servo = new five.Servo({
controller: "PCA9685",
pin: 0
})

let lightSensor = new five.Light({
controller: 'TLS2561'
})

Then, we need to build a lightSensor.on('change') handler. We're going to use the Sensor.scaleTo([min, max]) to scale the 0-255 input of the light sensor to the 0-180 output of the servo:

lightSensor.on('change', function(){
servo.to(this.scaleTo([0, 180]))
})

And that's it! Let's get it on the Pi and see it at work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.242.141