Brain Coral Tutorial: animation sequences LPD8806 pixel strip with lpd8806-asyncfx

Version 7

    In order to communicate programmatically with the LED strip, you need to install an npm module called lpd8806-asyncfx. You can do this by executing npm install lpd8806-asyncfx in your terminal. This may take a few minutes, as it also installs a few module dependencies.


    There are several animation sequences defined in this library. In this next program, we’ll run the rainbow sequence.


    Start by creating a new file named “1-3-rainbow.js”. The first thing we need to do is reference the LED library. This library requires one parameter: the number of LEDs on the strip. Ours has 10, so we pass the number 10 when creating our reference to the module. Create a variable named “leds” and assign it the value of the library:


    var leds = require('lpd8806-asyncfx')(10);


    Next, we’ll set up the reference to galileo-io just as we did before:


    var Galileo = require('galileo-io');
    var board = new Galileo();
    
    
    
    
    
    
    
    


    For now, set up a placeholder for the “ready” function:


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

     

    We want to call a function called “rainbow” that is part of the LED library. Before calling this function, or any of the methods attached to the leds object, we need to set up what happens when we close our Node server. The reason we do this is to turn off the LEDs when Node isn’t running. If we were to not do this, the LEDs will continue to be lit while the Node server is down.

     

    Using the global "process" object

    Node uses a globally accessible object called “process”. It is an instance of EventEmitter. We will use it to trigger an “exit” event that will turn off our LEDs when our Node server shuts down.


    We specifically want to listen for the “SIGINT” signal that “process” emits. The signal “SIGINT” is triggered when a Node server shuts down using “ctrl” + “c” key combination. When this event fires, we want to trigger the “exit” event of the “process” object. For example:

    process.on('SIGINT', function() {
        // Trigger exit process
        process.exit(0);
    });
    
    
    
    
    
    
    
    

     

    The above code snippet would be on its own, outside of the “ready” event of our galileo-io instance. When process.exit() is fired, we want to turn off the LEDs. We do this by creating a function to listen for the “exit” event of the “process” object. When the node server is shut down, we want to turn off the LEDs by calling the “off” method of the leds object. Also within the “ready” state of our Galileo, we want to call the “rainbow” function so that when our server starts, the rainbow sequence is initiated.

     

    Inside the “ready” event of our Galileo instance, we will place the following function:

     

    board.on('ready', function() {
        leds.rainbow(1, 25);
        process.on('exit', function(){
              leds.off();
        });
    });
    
    
    
    
    
    

     

    Here is the full program:

     

    var leds = require('lpd8806-asyncfx')(10);
    var Galileo = require('galileo-io');
    var board = new Galileo();
    
    board.on('ready', function() {
        leds.rainbow(1, 25);
        process.on('exit', function(){
              leds.off();
        });
    });
    
    // After nodejs shuts down (ctrl+c), this runs so it can turn off the LED's
    process.on('SIGINT', function() {
        // Trigger exit process
        process.exit(0);
    });
    
    
    
    
    
    

     

    Using the “SIGINT” and “exit” signals to trigger a function that stops the “rainbow” animation sequence and turns off the LED strip. The “rainbow” function takes two parameters: brightness and speed. We set brightness to 1, where 0.0 is off and 1 is fully bright. The color will update every 25 milliseconds.

     

    .
    < Connect the LPD8806 stripToggle animation sequences >