• I want to retain the data in memory. I could always transfer the data into a new array and then pull/destroy the data from new that array, but that seems inefficient?

    It depends how big your array is going to be. If it's only a few items long like your example, it's really not going to make any real difference. It'll only use the data for a short period of time, and then it's gone.

    If you do want to copy the array, all you need to do is newArray=rando.slice()

    That works out well for you as well, because the Uint8Array you were using can't have shift used on it (because it's a typed array). slice will conveniently convert it to a normal array for you.

    If I do a for counter with that code, would I still accomplish the overall goal?

    I'm not sure I know exactly what you mean, but a for loop will execute all at once like in your initial code, which isn't what you want - you want something that executes after each pulse, so it can decide what to do next at that point.

    I've just reworked the code above so it doesn't mess with the array:

    var rando = [16, 17, 1, 9, 8, 7, 15, 18, 19, 1];
    
    function doFlashes(rando) {
      var idx = 0; // index in rando
      var cnt = 0; // number of blinks
      
      function blinker() {
        if (idx>=rando.length) return; // nothing to do
        if (cnt < rando[idx]) {
          cnt++;
          digitalPulse(LED1,1,100);
          setTimeout(blinker, 200);
        } else {
          idx++;
          cnt = 0;
          setTimeout(blinker, 1000);
        }
      }
      
      blinker();
    }
    
    doFlashes(rando);
    

    It's basically the same, but instead of comparing everything against 0 and empty arrays it's using idx and cnt to look up the data in rando.

    Realistically, that's probably a lot more understandable anyway!

About

Avatar for Gordon @Gordon started