For this example, similar in structure to the led_brightness example, we're going to take an Arduino, and modify the speed of an attached motor such that it continually speeds up and slows down.

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

First, let's require Cylon:

var Cylon = require('cylon');

Now we can start defining our robot:


We'll be connecting to an Ardunio, using the Firmata protocol, and a motor attached to the Arduino on pin 3.

  connections: {
    arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }

  devices: {
    motor: { driver: 'motor', pin: 3 }

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

  work: function(my) {

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

    var speed = 0,
        increment = 5;

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

    every(0.05.seconds(), function() {
      speed += increment;

      console.log("Current Speed: " + (my.motor.currentSpeed()));

      if ((speed === 0) || (speed === 255)) { increment = -increment; }

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