Raspberry Pi - Servo

For this example, similar in structure to the led_brightness example, we're going to take a Raspberry Pi, and modify the angle of an attached servo such that it's continually turning back and forth.

Before we get started, make sure to have the cylon-raspi module installed.

First, let's require Cylon:

var Cylon = require('cylon');

Now we can start defining our robot:

Cylon.robot({

We'll be connecting to an Raspberry Pi, and using a servo attached to the Raspberry Pi on pin 11.

  connections: {
    raspi: { adaptor: 'raspi' }
  },

  devices: {
    servo: { driver: 'servo', pin: 11 }
  },

We'll start defining the work for our robot next:

  work: function(my) {

We'll define variables to hold our servo's angle, and the rate at which that angle will change:

    var angle = 30,
        increment = 40;

Every second, we'll increment the angle, set the servo to run at that angle, and log the angle we're running at to the console. We'll also make sure to change the increment if the angle is at the upper/lower bounds of the values supported:

    every((1).seconds(), function() {
      angle += increment;
      my.servo.angle(angle);
      console.log("Current Angle: " + (my.servo.currentAngle()));

      if ((angle === 30) || (angle === 150)) { increment = -increment; }
    });
  }

And with all that done, we can now start our robot:

}).start();