You are reading a single comment by @maze1980 and its replies. Click here to read the full conversation.
  • Thanks Gorden. It's a nice function and really fast. Just tried it with a pixel runner left/right.

    //runner (left/right)
    
    var pin = NodeMCU.D1;
    pinMode(pin, "output");
    var neopixel = require("neopixel");
    const leds = 64;
    
    const ledsx3 = 3*leds;
    //array with rgb values as Uint8ClampedArray
    const base = new Uint8ClampedArray(ledsx3);
    var show = new Uint8ClampedArray(ledsx3);
    var dot1 = new Uint8ClampedArray(3);
    dot1[0] = 128;
    dot1[1] = 128;
    dot1[2] = 128;
    var dot2 = new Uint8ClampedArray(3);
    dot2[0] = 128;
    dot2[1] = 0;
    dot2[2] = 0;
    var dot3 = new Uint8ClampedArray(3);
    dot3[0] = 0;
    dot3[1] = 0;
    dot3[2] = 128;
    
    //
    //prepare
    //
    var prepare = function() {
      for (var i=0; i<ledsx3; i++) {
        base[i] = 0;
      }
    };
    
    //
    //animate
    //
    var dir1 = 3;
    var pos1 = 0;
    var dir2 = -6;
    var pos2 = leds;
    var dly3 = 0;
    var pos3 = 3*Math.round(Math.random()*leds);
    var start, duration;
    var animate = function() {
      start = getTime();
      show.set(base);
      show.set(dot1, pos1);
      show.set(dot2, pos2);
      show.set(dot3, pos3);
      neopixel.write(pin, show);
      pos1 = pos1 + dir1;
      if (pos1 >= ledsx3) dir1 = -3;
      else if (pos1 <= 0) dir1 = 3;
      //duration of all code above: 0.006s
      pos2 = pos2 + dir2;
      if (pos2 >= ledsx3) dir2 = -6;
      else if (pos2 <= 0) dir2 = 6;
      //duration of all code above:  0.007s
      dly3++;
      if (dly3 === 7) {
        dly3 = 0;
        pos3 = 3*Math.round(Math.random()*leds);
      }
      duration = getTime()-start;
      //duration of all code above:  0.008s
    };
    
    
    var onInit = function() {
      setInterval(function() { animate(); }, 10);
    };
    
    onInit(); //don't save() this line
    

    Execute at your on risk, epilepsy warning.

About

Avatar for maze1980 @maze1980 started