You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • it is how setInteval (and setTimeout) is (are) defined... The nice thing in javascript is that an anonumous function can be created that wraps the actual function invocation.

    If you would have to implement it in an other language, you would also pass the reference to be invoked funciton for the time when the time comes and not right away.

    Unique javascript (and some other languages since the 70) is the functional / lambda / closure programming: code and context as a package are provided as an entity for execution (repeatedly) at a later time(see array example below). Therefore you do not need extra variables in line 4 and 5. At invocation those are generated and you can keep working with them in your intensity_step function...

    var ledSpecs = // array of led specifications with led and on/off status
    [ {led: D12, state: false}
    , {led: D13, state: false}
    , {led: D14, state: false}
    , {led: D15, state: false}
    ];
    
    var toggle = function(ledSpec) {
         digitalWrite(ledSpec.led, (ledSpec.state = !ledSpec.state)); // toggle
    };
    
    setInterval(function(){ // toggle every 333 ms all leds
        ledSpecs.forEach(toggle); 
    },333};
    

    ... or in short form with anonymous toggle function:

    var ledSpecs = // array of led specifications with led and on/off status
    [ {led: D12, state: false}
    , {led: D13, state: false}
    , {led: D14, state: false}
    , {led: D15, state: false}
    ];
    
    setInterval(function(){ // toggle every 333 ms all leds
        ledSpecs.forEach(function(ledSpec){// iterate over every element in the array
            digitalWrite(ledSpec.led, (ledSpec.state = !ledSpec.state)); // toggle
        });
    },333); 
    

    ...now, you can make a pulsing wave through your four leds (...n leds in a circle), by replacing state with intensity... by pulling intensity from a intensity constantly circulary shifting versus the led array```

    specArray.forEach(function(element,index­,array){
        /* analog out for current led as specified in element */
        /* your code using element,index,array... to set new intensity  */
    });
    
About

Avatar for allObjects @allObjects started