Book Image

Learning JavaScript Robotics

By : Kassandra Perch
Book Image

Learning JavaScript Robotics

By: Kassandra Perch

Overview of this book

Table of Contents (16 chapters)
Learning JavaScript Robotics
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a project with a motor and using the REPL


Grab your motor and board, and follow the diagram in the previous section to wire a motor. Let's use pin 6 for the signal pin, as shown in the preceding diagram.

What we're going to do in our code is create a Motor object and inject it into the REPL, so we can play around with it in the command line. Create a motor.js file and put in the following code:

var five = require('johnny-five');

var board = new five.Board();

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

  var motor = new five.Motor({
    pin: 6
  });

  this.repl.inject({
    motor: motor
  });
});

Then, plug in your board and use the motor.js node to start the program.

Exploring the motor API

If we take a look at the documentation on the Johnny-Five website, there are a few things we can try here. First, let's turn our motor on at about half speed:

> motor.start(125);

The .start() method takes a value between 0 and 255. Sounds familiar? That's because these are the values we can assign to a PWM...