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 pin! Okay, let's tell our motor to coast to a stop:

> motor.stop();

Note that while this function will cause the motor to coast to a stop, there is a dedicated .brake() method. However, this requires a dedicated break pin, which can be made available using shields and certain motors.

If you happen to have a directional motor, you can tell the motor to run in reverse using .reverse() with a value between 0 and 255:

> motor.reverse(125);

This will cause a directional motor to run in reverse at half speed. Note that this requires a shield.

And that's about it. Operating motors isn't difficult and Johnny-Five makes it even easier. Now that we've learned how this operates, let's try a servo.

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

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