You are reading a single comment by @Kim and its replies. Click here to read the full conversation.
  • I have myself some pulsing LEDs ... but the journey there wasn't all that smooth though.

    First, the code:

    function fakeAnalogWrite(value) {
      if (value >= 0 && value <= 1) {
        var on_time = (value*25)|0;
        on_time += 1;
        digitalPulse(LED1, 1, on_time);
        digitalPulse(LED1, 0, 27 - on_time);
      } else {
        digitalPulse(LED1, 0, 26);
      }
    }
    
    
    // pulse LED1 from on to off every *freq* seconds
    function pulse(freq) {
      while (true) {
        for (i = 0; i <= Math.PI + 1; i = i + (Math.PI)/(freq*27)) {
          fakeAnalogWrite(Math.sin(i));
        }
      }
    }
    

    I would be happy to write a tutorial for this code, but first I want to know if any improvements can be suggested.

    The problem with this code is that it halts all other things that are going on. Which isn't all that efficient. So any suggestions on how to improve this would be appreciated. I realise that this can be easily implemented on a lower level (and it should be implemented on a lower level), but the example is great in finding out how well this compact version of JavaScript works. And I have a number of suggestions:

    1. a delay function would be great. I had to do a lot of tinkering to get this to work, and essentially I'm abusing the digitalPulse by setting it off for some time. A dedicated delay function should be better, as it can be improved over time to allow other functions to run while this part is just waiting.
    2. using setTimeOut was pure misery. I honestly failed to write any of it down, but I got bugs, missing functions, memory overflow ... . If someone is brave enough to rewrite it using setTimeOut, maybe we can get started with making it a bit more useful :).
    3. there does not appear to be tail recursion. This is a shame. It is so easy to detect (just a function at the end) and it makes code so much more compact. Any plans on implementing this (soon)?
    4. Using the LEDs could be easier. Why not add some prototype functions to them such as on() and off()? Okay, seems like you have set() and reset(), but those names aren't that obvious :).

    quick update:
    the problem with setTimeoutis that these things don't seem to work:

    function pulse(freq) {
      setTimeout(doPulse(freq), 1000);
    }
    

    which fails with ERROR: Function or String not supplied!.

    another update:
    This is already a bit better and at least allows you to stop the execution:

    function doPulse(freq) {
      for (i = 0; i <= Math.PI + 1; i = i + (Math.PI)/(freq*27)) {
        fakeAnalogWrite(Math.sin(i));
      }
      // send an extra short pulse on to turn the LED off
      // the more obvious LED1.reset(); does not seem to work
      digitalPulse(LED1, 1, 1);
    }
    
    
    function pulse(freq) {
      setInterval(function() { doPulse(freq); }, freq*1000);
    }
    

    But some messy stuff is going on with the pulse at the end ;).

About

Avatar for Kim @Kim started