• @randunel, Espruino can support timeouts relatively accurately, but at much lower than 0.5ms it's very unlikely to hit the target. The point is to delay the pin mode setting until after the digitalPulse though - because the digitalPulse returns immediately and pulses the pin on an IRQ.

    Just a note on the code though - if you define a function (rather than a string) to be called from setWatch/setTimeout then when it executes, it has access to the variables in the function it was defined in:

    function a() {
     b=5;
     setTimeout(function() { print(b); }, 100);
    }
    

    I think that could let you tidy the code up quite a bit. It might also make sense to set up setWatch before the digital pulse, and to just write code to ignore the first pulse (as this could be why you're not seeing some of the bits?).

    As far as evaluating the final value, I'd suggest just using binary arithmetic. You've got 64 bit numbers, so you should easily have enough space for all 40 bits. So change:

    var bits = [];
    ...
            if (tt > 0.000044) {
                bits[i]=1;
            } else {
                bits[i]=0;
            }
            i++;
    ...
    var rh=eval("0b"+bits[2]+bits[3]+bits[4]+bit­s[5]+bits[6]+bits[7]+bits[8]+bits[9]);
    

    to

    var bits = 0;
    ...
            bits=(bits<<1) | (tt > 0.000044);
    ...
    var rh=(bits>>2)&0xFF;
    

    It'll be a bit faster too...

About

Avatar for Gordon @Gordon started