You are reading a single comment by @maze1980 and its replies. Click here to read the full conversation.
  • @allObjects: Line 18 doesn't matter as it was just used to get an idea of the execution time simulating some random merging of per-calculated values. I'll use the require(), that's good.

    @Gordon: I didn't get what function do you mean using ".set". I'd say setting RGB values at runtime is most likely too slow in most cases.

    Having this thread resurrected, I did a quick rainbow implementation.

    //rainbow effect
    
    const pin = NodeMCU.D5;
    pinMode(pin, "output");
    var neopixel = require("neopixel");
    
    //array with rgb values as Uint8ClampedArray
    var rgb = [];  //length = 255/step = max. number of leds
    const step = 3;  //integer n, use every n-th rainbow color, effect will be n times faster
    //const leds = 64; //the number of leds, used by the slower version only
    
    //
    //getRainbowColor(position = 0..255)
    //returns color of rainbow at the position as rgb in a Uint8ClampedArray
    //
    var getRainbowColor = function (position) {
    var rgb = new Uint8ClampedArray(3);
      if(position < 85) {
       rgb[0] = position * 3;
       rgb[1] = 255 - position * 3;
       rgb[2] = 0;
      } else if (position < 170) {
       position -= 85;
       rgb[0] = 255 - position * 3;
       rgb[1] = 0;
       rgb[2] = position * 3;
      } else {
       position -= 170;
       rgb[0] = 0;
       rgb[1] = position * 3;
       rgb[2] = 255 - position * 3;
      }
      return rgb;
    };
    
    //
    //prepare ()
    //
    //prefill the rgb array
    var prepare = function() {
      for (var i=0; i<255; i += step) {
        rgb.push(getRainbowColor(i));
      }
    };
    
    //
    //animate
    //
    var tmp;
    var animate = function() {
      //rgb.push(rgb.shift());                   //rotate the rgb array by one value
      rgb.unshift(rgb.pop());                    //rotate the rgb array by one value, a little bit faster
      neopixel.write(pin, rgb);                  //faster version
      //neopixel.write(pin, rgb.slice(1, leds)); //slower version, technically cleaner
    };
    
    
    var onInit = function() {
      prepare();
      setInterval(function() { animate();}, 10); //10ms, if using serial/bt/wifi this might too fast
    };
    
    onInit(); //don't save() this line
    

    If you have any suggestions on how to make the code faster please give me a hint. And if you want it to https://www.espruino.com/WS2811 feel free to copy it.

About

Avatar for maze1980 @maze1980 started