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

Project – cat toy


In this project, we'll add a piece of paper to our motor, and then code some randomness to make it spin back and forth at varying speeds (cats get bored with a predictable toy, after all).

The wiring for this project is the same as the motor test; no need to change anything there.

Putting a piece of paper on the motor shaft

Either roll the sticky end of a long sticky note around the motor shaft, or tape a long strip of paper to it. It should look something like this:

After the relatively simple construction of our toy, let's code some randomness!

Coding the randomness to start/stop the motor

We want the motor to start at a random speed for anywhere from 1-10 seconds, then stop for 1-10 seconds, and repeat. We also want whether it goes forward or backward to be random. I limited the speed to 75—anything faster was too much for my cats!

In your cat-toy.js file, get rid of the board.repl.inject statement and add the following:

startMovement()

functionstartMovement(){
  letdirection...