You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • There's a bit of a delay caused by executing the JavaScript itself (it's not that fast). If you instead do:

    function checkTime()
    {
        var t=getTime();
        if (startTime==0) startTime=t-1;
    ...
    
    function testTime()
    {
        startTime=0;
    ...
    

    So you're comparing subsequent setIntervals with the time from the first, you should get a much better accuracy.

    Espruino isn't pre-emptive, but it tries to schedule intervals so that on average they are very accurate. If you add a watch crystal to Espruino you can happily run it as a reasonably accurate clock using nothing more than setInterval(...,1000).

    So for instance something like this could happen:

    checkTime called at time 3.3 (0 sec)
    wasteTime called at time 4.2, takes 300ms
    checkTime called at time 4.5 (1.2 sec)
    checkTime called at time 5.3 (2 sec) <----- 
    

    Hope that helps. It's worth noting that by default Espruino uses the internal RC oscillator for time, which could be +/- 2%. For proper logging you'll want to add a watch crystal - see http://www.espruino.com/Clocks

About

Avatar for Gordon @Gordon started