Creating a project with a servo and a sensor

Let's start with just a servo and the REPL, then we can add in a sensor. Use the diagram from the previous section as a reference to wire up a servo, and use pin 6 for signal.

Before we write our program, let's take a look at some of the options the Servo object constructor gives us. You can set an arbitrary range by passing [min, max] to the range property. This is great for low quality servos that have trouble at very low and very high values.

The type property is also important. We'll be using a standard servo, but you'll need to set this to continuous if you're using a continuous servo. Since standard is the default, we can leave this out for now.

The offset property is important for calibration. If your servo is set too far in one direction, you can change the offset to make sure it can programmatically reach every angle it was meant to. If you hear clicking at very high or low values, try adjusting the offset.

You can invert the direction of the servo with the invert property or initialize the servo at the center with center. Centering the servo helps you to know whether you need to calibrate it. If you center it and the arm isn't centered, try adjusting the offset property.

Now that we've got a good grasp of the constructor, let's write some code. Create a file called servo-repl.js and enter the following code:

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

var board = new five.Board();

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

  var servo = new five.Servo({
    pin: 6
  });

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

This code simply constructs a standard servo object for pin 6 and injects it into the REPL.

Then, run it using the following command line:

> node servo-repl.js

Your servo should jump to its initialization point. Now, let's figure out how to write the code that makes the servo move.

Exploring the servo API with the REPL

The most basic thing we can do with a servo is set it to a specific angle. We do this by calling the .to() function with a degree, as follows:

> servo.to(90);

This should center the servo. You can also set a time on the .to() function, which can take a certain amount of time:

> servo.to(20, 500);

This will move the servo from 90 degrees to 20 degrees in over 500 ms.

You can even determine how many steps the servo takes to get to the new angle, as follows:

> servo.to(120, 500, 10);

This will move the servo to 120 degrees in over 500 ms in 10 discreet steps.

The .to() function is very powerful and will be used in the majority of your Servo objects. However, there are many useful functions. For instance, checking whether a servo is calibrated correctly is easier when you can see all angles quickly. For this, we can use the .sweep() function, as follows:

> servo.sweep();

This will sweep the servo back and forth between its minimum and maximum values, which are 0 and 180, unless set in the constructor via the range property. You can also specify a range to sweep, as follows:

> servo.sweep({ range: [20, 120] });

This will sweep the servo from 20 to 120 repeatedly. You can also set the interval property, which will change how long the sweep takes, and a step property, which sets the number of discreet steps taken, as follows:

> servo.sweep({ range: [20, 120], interval: 1000, step: 10 });

This will cause the servo to sweep from 20 to 120 every second in 10 discreet steps.

You can stop a servo's movement with the .stop() method, as follows:

> servo.stop();

Tip

For continuous servos, you can use .cw() and .ccw() with a speed between 0 and 255 to move the continuous servo back and forth.

Now that we've seen the Servo object API at work, let's hook our servo up to a sensor. In this case, we'll use a photocell. This code is a good example for a few reasons: it shows off Johnny-Five's event API, allows us to use a servo with an event, and gets us used to wiring inputs to outputs using events.

First, let's add a photocell to our project using the following diagram:

Exploring the servo API with the REPL

A servo and photoresistor wiring diagram

Then, create a photoresistor-servo.js file, and add the following:

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

var board = new five.Board();

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

  var servo = new five.Servo({
    pin: 6
  });

  var photoresistor = new five.Sensor({
    pin: "A0",
    freq: 250

  });

  photoresistor.scale(0, 180).on('change', function(){
    servo.to(this.value);
  });

});

How this works is as follows: it's similar to the sensor code we wrote in the previous chapter! It's just during the data event that we tell our servo to move to the correct position based on the scaled data from our photoresistor. Run the following command line:

> node photoresistor-servo.js

Then, try turning the light on and covering up your photoresistor and watch the servo move!

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

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