Node powered
cat remote control


This project is a walk through simple control of a laser with 2 servo, allowing you to pan/tilt it, we will also explore simple control through a simple web server, all via javascript, thanks to firmata and johnny five. on top of that it will allow you to remotely control a cat while on a hangout with him, for example.

it is roughly a guideline, and I encourage you to tweak it rather than follow it, if you find it too simple extend it as you with this is still the old javascript you work with every day, so extend it, commit changes, have fun, makes some cat happy !

Happy nodebot's day

Tool list

  • little screwdriver
  • Blu-Tack or similar
  • wire
  • third hand (optional, but handy)
  • solder iron and solder (optional)

the solder iron is used only to solder the laser cables, to make them easier to pin to to breadboard

Hardware list

  • 1 arduino uno board
  • 1 bread board with power line (or not)
  • few leds and 220 ohm resistor
  • 2 9g servo
  • 1 Laser Diode - 5mW whatever color
  • 1 servo bracket (optional)

the servo brackets are a bit expensive, so if you don't plan to build a permanent laser tower you can use blue tack or similar for a temporary version

Software requirement

  • node
  • git
  • your favorite javascript editor
  • firmata uploaded to you arduino (see here for details)

Step one, at the beginning there was one led

first things first, to control a led we will need to link it to our board.

beginners note always unplug the arduino before mounting components, I'm sure you knew about it, but, just in case :)

if you look at your arduino, you have 3 main lines of pins,
  1. power, where you will draw current to power your creations
  2. just below it, you have the analog line, it is an input you can "read" info from a potentiometer with it, by example
  3. digital pins, those pins can be configured either as input, either as output.

Note we are using two kind of electronic components here.

the led, is an polarized component, so it has 2 different pins, the anode (+) is the longer one, and the cathode (-) is the smaller one, you need to connect your led's cathode to the ground, and the anode to your' arduino digital pin 13, allowing you to control via javascript.

the resistor is not polarized, so we don't have to worry about the side we link power to it, we use it to lower the current that the arduino is delivering to the led thanks to ohm's law

the plan is to create a closed circuit linking the ground of the arduino's power line to the 13th digital pin.
there is no particular order to link things, but i usually start with the onboard components, so put the led on the board parallel to the power line, if you have one, then connect the anode (+, longest leg) to the resistor that you will also place in parallel to the power line,this resistor is used to reduce the amount of current in the led, that would not stand the 5v provided by the arduino.
We will then connect the led cathode (-) to breadboard ground power line (if you have one), then from the breadboard's ground to the arduino's ground
now we need to link the leg of the resistor that do not connect the led to the 13th digital pin of the arduino

our circuit is now closed, so it is time to jump on the computer

and now ladies and gentlemen ... javascript

if you can read this, i can assume you cloned this repository if not go here first

open a terminal in the cloned repo and create a file, say myCat.js, open it in your editor

let's take control of this led with command line !

    
    var five = require("johnny-five"),  // johnny-five, enable us to talk to sir arduino
        board = new five.Board(),       // it will contain objects that will allow us to manipulate hardware
        onlineLed;                      // our fist piece of hardware, a little led

    // borad initialisation
    board.on("ready", function() {      // remember jquery ?

        // in this section we will create an instance of the led object for our led
        onlineLed = new five.Led(13);   // our led linked to the digital pin13

        // inject our hardware in the Repl, so we can talk with it in the command line
        board.repl.inject({
            onlineLed: onlineLed
        });
    });
    

now node myCat.js and let's see what we can do!

Note if you look at the led while the board boots, you can see that it is blinking, and that it blinks at the same time that the arduino onboard led is blinking, this is because the 13 th pin is link to this onboard led, that tells us the status of the board (remember your pc's beeps ?)

so now we have a REPL prompt, then ...
then, we can call our led instance, to see what this is all about, onlineLed.on() and, tada a new led is lit !

if you want to see more, you could dig in the /node_modules/johnny-five/eg/led-*.js files, but for now you should know about onlineLed.on(), onlineLed.off(), and onlineLed.on() onlineLed.pulse() (to remember the good old blink times)

Step Two, adding motion

now that we can control light, let's star controlling motion as well, first thing first, lets connect our servomotors to the breadboard.

Note not all servo have the same pin configuration as the one in the diagram. check yours first.

these 9g servos have 3 pins, positive, negative, and PWM, Pulse Wave Modulation is used to control the angle of your servo sending faked analog wave to the servo to move it to the specified degree.

so connect positive and negative pins to the breadboard's power line, then connect the pwm's servos to your arduino digital pins, 10 for the pan, 9 for the tilt

let's finish the hardware part before coding more. Next is the laser.

Step Three, laser action

laser diagram TODO: explanation

  • why transistor
  • transistor how to
  • sample code ? (redundant with led code)

Step Four, Coding action

explain furnished code

  • cat code
  • server code
  • possible extentions

additional infos

Resources

PWM

PWM stand for Pulse Width Modulation, it is a trick to get analog like result with your digital pins, do do so it emit a square wave by altering low and high current each cycle is divided in 256 parts, here you are my precious 8bits !!!
more about pwm here

pwm waves

+----------------------------------------------------------------------------------+
| 0% cycle (0)                                                                     |
|----------------------------------------------------------------------------------|
|                                                                                  |
|                                                                                  |
|                                                           5v                     |
|                                                                                  |
|                                                                                  |
|                                                                                  |
|                                                                                  |
|                                                                                  |
|+-------------------------------------------------------+  0v                     |
|                                                                                  |
+----------------------------------------------------------------------------------+
+----------------------------------------------------------------------------------+
| 25% cycle (64)                                                                   |
|----------------------------------------------------------------------------------|
|                                                                                  |
|+--+          +--+          +--+          +--+             5v                     |
||  |          |  |          |  |          |  |                                    |
||  |          |  |          |  |          |  |                                    |
||  |          |  |          |  |          |  |                                    |
||  |          |  |          |  |          |  |                                    |
||  |          |  |          |  |          |  |                                    |
||  |          |  |          |  |          |  |                                    |
|+  +----------+  +----------+  +----------+  +----------+  0v                     |
|                                                                                  |
+----------------------------------------------------------------------------------+
+----------------------------------------------------------------------------------+
| 50% cycle (127)                                                                  |
|----------------------------------------------------------------------------------|
|                                                                                  |
|+------+      +------+      +------+      +------+         5v                     |
||      |      |      |      |      |      |      |                                |
||      |      |      |      |      |      |      |                                |
||      |      |      |      |      |      |      |                                |
||      |      |      |      |      |      |      |                                |
||      |      |      |      |      |      |      |                                |
||      |      |      |      |      |      |      |                                |
|+      +------+      +------+      +------+      +------+  0v                     |
|                                                                                  |
+----------------------------------------------------------------------------------+
+----------------------------------------------------------------------------------+
| 75% cycle (191)                                                                  |
|----------------------------------------------------------------------------------|
|                                                                                  |
|+---------+   +---------+   +---------+   +---------+      5v                     |
||         |   |         |   |         |   |         |                             |
||         |   |         |   |         |   |         |                             |
||         |   |         |   |         |   |         |                             |
||         |   |         |   |         |   |         |                             |
||         |   |         |   |         |   |         |                             |
||         |   |         |   |         |   |         |                             |
|+         +---+         +---+         +---+         +---+  0v                     |
|                                                                                  |
+----------------------------------------------------------------------------------+
+----------------------------------------------------------------------------------+
| 100% cycle (255)                                                                 |
|----------------------------------------------------------------------------------|
|                                                                                  |
|                                                                                  |
|+-------------------------------------------------------+  5v                     |
|                                                                                  |
|                                                                                  |
|                                                                                  |
|                                                                                  |
|                                                                                  |
|                                                                                  |
|                                                            0v                    |
|                                                                                  |
|                                                                                  |
+----------------------------------------------------------------------------------+