You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Cool, great and quick response. Comment on the code: interesting... very clear to understand... I see that you already use PWM / analog output... I do not know about STM32F4 discovery board chip, whether it has HW or SW PWMs. HW PWMs should be a bit more performing; @Gordon can give the facts (...: provided them while writing this post). Another option could be the Waveform with single or double buffer... never used it, so I'm not sure if the buffer setting overhead eats up the gain. What about compiling the function? Have no experience with that... setup may have to be dfferent...

    From the code I read it is a pulsing - intensity from 0 to 1 and back in 0.01 steps - a step every 2ms... thus pulsing 2-1/2 times per second.

    Interesting in the code is its quasi object-orientation: a logical new by returning a seperate intensity_step function every time you invoke glow() function (taking advantage of the closure / lambda / functional programming capability of Javascript).

    I have some alternaties... if you'd like to know... ;\ - regarding performance difference? --- Would be interesting to know.

    To stick with your approach, passing in the increment/decrement stp with the led, an extra layer of function call is removed in setInterval();

    var pulse = function(led,stp) {
      var ity = 0;
      return function(){
        analogWrite(led, 
          (ity += stp) < 0
           ? ity = 0 + (stp = - stp)
           : (ity > 1)
             ? ity = 1 + (stp = - stp)
             : ity
         );
      };
    };
    setInterval(pulse(D13,0.01),2);
    setInterval(pulse(D14,0.01),2);
    setInterval(pulse(D12,0.01),2);
    setInterval(pulse(D15,0.01),2);
    

    What timing differences - if any - can you notice with this code?

    PS: Code has an issue not going all the way to 0 or 1 when 1 / step not a whole number.

About

Avatar for allObjects @allObjects started