maximum polling frequency?

Posted on
  • I just got my Espruino and wanted to see if i can make a touch switch from it. I wrote a small program to read analog values of pin C7 (random choice here) ..
    while it works fairly reliably when I touch the pin, i do this by setting up an interval to check the value every 200ms. I'm wondering what the resolution of the polling is. Can I poll every 100ms? every 10ms?

  • Hi MikeD,

    The timer Espruino uses at the moment is the 32kHz RTC, so while theoretically you could do that, you're limited by the speed of execution. You can test with:

    counter = 0;
    var i = setInterval(function() { counter++; },10);
    setInterval(function() { console.log(counter);counter=0; },1000);
    // then...
    changeInterval(i,5);
    changeInterval(i,2);
    changeInterval(i,1);
    changeInterval(i,0.5);
    

    and it'll show you how many times per second it's actually been able to manage.

    At the moment you're limited to about 4.5kHz = 0.22ms for simple functions like that example. The more complicated the function the slower it is though.

  • By contrast, I just tried this on a PC and it can manage roughly 240Hz compared to Espruino's 4500Hz :)

  • so will a setInterval with a period of 20-25 work well ? I know on desktop, this will typically be fairly awful cause javascript has a poor resolution on most browsers. How would the espruino handle multiple intervals of such a short period?

  • It should handle it very well as long as your functions don't take too long to execute. The accuracy is good - but try it and see :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

maximum polling frequency?

Posted by Avatar for MikeD @MikeD

Actions