Book Image

Hands-On Robotics with JavaScript

By : Kassandra Perch
Book Image

Hands-On Robotics with JavaScript

By: Kassandra Perch

Overview of this book

JavaScript has an effective set of frameworks and libraries that provide support for embedded device programming and the robotics ecosystem. You’ll be able to put your JavaScript knowledge to work with this practical robotics guide. The book starts by guiding you in setting up an environment to program robots with JavaScript and Rasberry Pi 3. You will build beginner-level projects, such as a line-following robot, and then upgrade your robotics skills with a series of projects that help you get to grips with the Johnny-Five library. As you progress, you’ll learn how you can improve your projects by enabling advanced hardware components and programming concepts. You’ll even build an advanced AI-enabled robot, connect its NodeBots to the internet, create a NodeBots Swarm, and explore Message Queuing Telemetry Transport (MQTT). By the end of this book, you will have enhanced your robot programming skills by building a range of simple to complex projects.
Table of Contents (19 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Creating our night-light


Now that we know our light sensor works, we can add an LED and create our night-light.

Wiring up the LED

Connect the short leg of your LED to a ground rail using a 330K ohm resistor, and wire the long leg to GPIO #5, also known as P1-29:

Coding this project

Create a file in the same folder as the other files from this chapter, and copy the contents of print-light-sensor-readings.js into it.

In the start of the board.on('ready') handler, add a constructor for our LED:

let light = new five.Led('P1-29')

And in the lightSensor.on('change') function, replace the console.log statement with the logic that will turn the LED on and off:

if(this.value <= 25) {
  light.on()
} else {
  light.off()
}

And we're ready to run! Load the folder onto your Pi, navigate to the folder in your Pi's SSH session, and run:

sudo node night-light.js

When you cover the light sensor with your thumb, the LED should light up, as shown in the following image:

 

 And when you remove your thumb (in a well-lit...