8 Replies Latest reply on Jun 3, 2014 10:26 AM by rwaldron

    Node.js/Galileo-io analogWrite troubleshooting

    cyamada

      I've been trying to operate a simple RGB LED using Node/ Galileo-io. Everything works great on the Arduino side if I just upload code however, when I try and reproduce the sketch in Node it completely stops working. See below:

       

      var Galileo = require("galileo-io");

      var board = new Galileo();

       

      var pins = {

          red: 11,

          green: 10,

          blue: 9

      };

       

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

          this.pinMode(pins.red, this.MODES.OUTPUT);

          this.pinMode(pins.green, this.MODES.OUTPUT);

          this.pinMode(pins.blue, this.MODES.OUTPUT);

       

          setInterval(function() {

              cycleColors(1000);

          }, 1000);

      });

       

      function cycleColors(wait) {

          console.log('red');

          setColor(255, 0, 0); // red

          delay(wait);

       

          console.log('green');

          setColor(0, 255, 0); // green

          delay(wait);

       

          /* other combinations */

      }

       

      function setColor(r, g, b) {

          r = 255 - r;

          g = 255 - g;

          b = 255 - b;

       

          board.analogWrite(pins.red, r);

          board.analogWrite(pins.green, g);

          board.analogWrite(pins.blue, b);

      }

       

      // http://stackoverflow.com/questions/16873323/javascript-sleep-wait-before-continuing

      function delay(milliseconds) {

          var start = new Date().getTime();

          for (var i = 0; i < 1e7; i++) {

              if ((new Date().getTime() - start) > milliseconds) {

                  break;

              }

          }

      }

       

      I've also iteratively going through each output (analogWriting each one to a specific value just trying to reproduce a single color) however, nothing seems to be working. The outputs will sometimes be 5V or around 2.7V even if I'm analogWriting the pin to 0. Does anyone have any ideas?

        • 1. Re: Node.js/Galileo-io analogWrite troubleshooting
          rwaldron

          Hello! I'm the creator of Galileo-IO, hopefully I can help answer your questions...

           

          My first piece of advice is to get rid of the delay function entirely—this is process blocking and defeats the entire purpose of writing the program in JavaScript to run on node.js. While that function is "delaying", it's also preventing everything else from executing. The following will cycle from one color to the next every second without blocking the process:

           

          var Galileo = require("galileo-io");

          var board = new Galileo();

           

          var pins = {

            red: 11,

            green: 10,

            blue: 9

          };

           

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

            this.pinMode(pins.red, this.MODES.OUTPUT);

            this.pinMode(pins.green, this.MODES.OUTPUT);

            this.pinMode(pins.blue, this.MODES.OUTPUT);

           

            var colors = [

              [0xff, 0x00, 0x00], // red hex:   ff0000

              [0x00, 0xff, 0x00], // green hex: 00ff00

              [0x00, 0x00, 0xff]  // blue hex:  0000ff

            ];

            var color = 0;


            setInterval(function() {

              setColor(colors[color++]);

              if (color === colors.length) {

                color = 0;

              }

            }, 1000);

          });

           

           

          function setColor(rgb) {

            board.analogWrite(pins.red, rgb[0]);

            board.analogWrite(pins.green, rgb[1]);

            board.analogWrite(pins.blue, rgb[2]);

          }

           

           

          Next, notice that I removed the piece of code that was subtracting the rgb values from 255—that was causing your hex values to become incorrect, eg:

           

           

          setColor(255, 0, 0); // red


          function setColor(r, g, b) {

            r = 255 - r;  // this would be 255 - 255 = 0

            g = 255 - g; // this would be 255 - 0 = 255

            b = 255 - b; // this would be 255 - 0 = 255

           

            // the values are now: 0, 255, 255.... this color is cyan, not red.

           

            board.analogWrite(pins.red, r);

            board.analogWrite(pins.green, g);

            board.analogWrite(pins.blue, b);

          }

           

           

          I'm about 200 miles away from my Galileo board, so if the example I gave above still has problems, I will have to follow up on analogWrite giving strange values · Issue #8 · rwaldron/galileo-io · GitHub on Thursday morning.

          • 2. Re: Node.js/Galileo-io analogWrite troubleshooting
            Wilkins

            Thanks for the tip about the delay function. I ran the updated code and it almost works. It seems as though the individual colors don't stay on by themselves, but rather other colors are dimly lit while the main color is on.

            • 3. Re: Node.js/Galileo-io analogWrite troubleshooting
              rwaldron

              You can ignore my comment about the colors being incorrect—I didn't realize that was for common anode leds. I will be able to do hands on troubleshooting when I get home on Thursday

              • 4. Re: Node.js/Galileo-io analogWrite troubleshooting
                rwaldron

                Was the RGB Led diffused? When I used the diffused Leds, the color is crummy and washed out.

                • 5. Re: Node.js/Galileo-io analogWrite troubleshooting
                  Wilkins

                  Yeah, they are. The confusing part is that they look great when ran from Arduino (ex:https://learn.adafruit.com/downloads/pdf/adafruit-arduino-lesson-3-rgb-leds.pdf). I figured since it's just analog, it'd be the same on the node side.

                  • 6. Re: Node.js/Galileo-io analogWrite troubleshooting
                    rwaldron

                    So here's the update from working with this all day:

                     

                    - I made no changes to Galileo-io

                    - I successfully controlled the RGB color value state of several RGB leds, including a set of individual Leds "posing" in place of an RGB Led.

                     

                    Here's the code I used:

                     

                    var argv = require("optimist").default({ anode: false }).argv;

                    var Galileo = require("galileo-io");

                    var board = new Galileo();

                    var pins = {

                      red: 11,

                      green: 10,

                      blue: 9

                    };

                    var names = Object.keys(pins);

                     

                    // if --anode=false is passed, it will appear

                    // to be "truthy"

                    argv.anode = argv.anode === "true";

                     

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

                      console.log( "CONNECTED" );

                     

                      this.pinMode(pins.red, this.MODES.OUTPUT);

                      this.pinMode(pins.green, this.MODES.OUTPUT);

                      this.pinMode(pins.blue, this.MODES.OUTPUT);

                     

                      var colors = [

                        [0xff, 0x00, 0x00], // red hex code:   ff0000

                        [0x00, 0xff, 0x00], // green hex code: 00ff00

                        [0x00, 0x00, 0xff], // blue hex code:  0000ff

                      ];

                     

                      var color = 0;

                     

                      setInterval(function() {

                        setColor(colors[color], names[color]);

                        color++;

                        if (color === colors.length) {

                          color = 0;

                        }

                      }, 500);

                    });

                     

                    function setColor(rgb, name) {

                      rgb = rgb.map(function(eightbit) {

                        return argv.anode ? eightbit ^ 0xff : eightbit;

                      });

                     

                      board.analogWrite(pins.red, rgb[0]);

                      board.analogWrite(pins.green, rgb[1]);

                      board.analogWrite(pins.blue, rgb[2]);

                     

                      console.log("Set to: %s ", name, rgb);

                    }

                     

                     

                    The most noticeable thing about this code is that it's basically the same thing. Here are videos of each RGB set up I tested: Dropbox - analog-write

                    • 7. Re: Node.js/Galileo-io analogWrite troubleshooting
                      Wilkins

                      This is really great, thank you. Much appreciated.