setWatch strange results

Posted on
  • Hi

    I'm using setWatch on an input that has 50hz pulses which are ~0.9ms wide. I was expecting to see two callbacks alternating true/false per pulse, one for each edge - but I actually see multiple successive true or false values. This seems strange.

    My code

    '''
    var states=new Array();
    var index=0;

    function onpulse(e)
    {
    states.push(e);
    if (index<100) index++;
    }

    setWatch(onpulse,C5,{repeat:true,edge:'b­oth',debounce:0});
    '''

    I'm storing the state change data in an array to try to be sure I'm catching
    all the events - I thought that printing them might cause some to be lost.

    The output I get looks like this

    '''

    for(var i=0;i<100;i++) {print(states[i]);}
    {"lastTime":undefined,"time":41004.58265­3045654296875,"state":false}
    {"lastTime":41004.582653045654296875,"ti­me":41004.5826625823974609375,"state":fa­lse}
    {"lastTime":41004.5826625823974609375,"t­ime":41004.588489532470703125,"state":fa­lse}
    {"lastTime":41004.588489532470703125,"ti­me":41004.588512420654296875,"state":tru­e}
    {"lastTime":41004.588512420654296875,"ti­me":41004.58955669403076171875,"state":f­alse}
    {"lastTime":41004.58955669403076171875,"­time":41004.60842227935791015625,"state"­:false}
    {"lastTime":41004.60842227935791015625,"­time":41004.60844326019287109375,"state"­:true}
    {"lastTime":41004.60844326019287109375,"­time":41004.60946750640869140625,"state"­:false}
    {"lastTime":41004.60946750640869140625,"­time":41004.62811374664306640625,"state"­:false}
    {"lastTime":41004.62811374664306640625,"­time":41004.62813091278076171875,"state"­:true}
    {"lastTime":41004.62813091278076171875,"­time":41004.62916660308837890625,"state"­:false}
    {"lastTime":41004.62916660308837890625,"­time":41004.647785186767578125,"state":f­alse}
    {"lastTime":41004.647785186767578125,"ti­me":41004.64781093597412109375,"state":t­rue}
    {"lastTime":41004.64781093597412109375,"­time":41004.64885044097900390625,"state"­:false}
    {"lastTime":41004.64885044097900390625,"­time":41004.6675014495849609375,"state":­false}
    {"lastTime":41004.6675014495849609375,"t­ime":41004.66751956939697265625,"state":­true}
    {"lastTime":41004.66751956939697265625,"­time":41004.66753864288330078125,"state"­:true}
    '''

    What's puzzling me is that there doesn't seem to be any pattern - I was
    expecting true,false, true, false...

    Any ideas?

    Martin

  • It could be that the signal is very noisy... The odd values happen when Espruino is interrupted but the signal has changed so quickly that when it checks what the state is, it isn't what you'd expect.

    Try adding debounce: 1. to the end of the setwatch statement. .. That should help...

  • I've noticed this kind of behavior as well.

    Look at your data more closely - you're getting spurious callbacks while it's false... But when the signal goes high (which consistently happens every 20ms, just like it should for a 50hz signal), it is consistently followed by a record with the signal going low about 1ms (ie, 0.9 and a margin of error) later.

    Why not record the state of the last call, and discard any call that has the same state as the last one?

    (Edit: Oops - meant to just make that a normal post, not "in reply to...")

  • Guys

    Thanks... this isn't really a problem for my current signal processing problem - when the signal is off, I get zero events, so it's fine.

    Gordon - I tried debounce:0, but that had no effect. I'll try debounce:1

    Martin

  • yes - debounce:0 is the same as leaving debounce off, so it wouldn't have an effect.

    It might be worth checking the signal with an oscilloscope - it might be that putting a very small capacitor across it would be enough to stop any glitches.

  • I already tried the capacitor, but my electronics isn't quite good enough - it didn't help. Anyway, I don't actually need to solve the problem - I was just documenting it for others

    Thanks for the help.

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

setWatch strange results

Posted by Avatar for mgg1010 @mgg1010

Actions