setwatch doesn't work stable

Posted on
  • Hello,

    I'm using setwatch to detetch keypress events. I wired up a pin as button. Voltage flows as "5V -> Button -> E2 Pin"
    I'm using STMF32F4 Discovery.

    Here is my code :

    var button1=E2;
    
    pinMode(button1, "input_pulldown");
    
    
    setWatch(function(e) {
           console.log(e);
            }, button1, { repeat: true, edge: "both",debounce:10});
    

    Some times it prints console while I'm not pressing button. Some times prints 2-3 times on a button press. Some times it doesn't print anything. So, I can't understand which is the real button press event;

    Here is the output :

    { "state": true, "lastTime": undefined, "time": 2106.83884495238 } 
    { "state": false, "lastTime": 2106.83884495238, "time": 2107.13843809523 }
    { "state": true, "lastTime": 2107.13843809523, "time": 2102.44481219047 } 
    { "state": false, "lastTime": 2102.44481219047, "time": 2108.03721752380 }
    { "state": false, "lastTime": 2108.03721752380, "time": 2106.63911619047 }
     { "state": true, "lastTime": 2106.63911619047, "time": 2106.43938742857 } 
    { "state": true, "lastTime": 2106.43938742857, "time": 2107.33816685714 } 
    { "state": false, "lastTime": 2107.33816685714, "time": 2107.53789561904 } 
    { "state": true, "lastTime": 2107.53789561904, "time": 2107.13843809523 } 
    { "state": true, "lastTime": 2107.13843809523, "time": 2108.03721752380 } 
    { "state": false, "lastTime": 2108.03721752380, "time": 2107.13843809523 } 
    { "state": false, "lastTime": 2107.13843809523, "time": 2122.91701028571 } 
    { "state": false, "lastTime": 2122.91701028571, "time": 2122.91701028571 } 
    { "state": false, "lastTime": 2122.91701028571, "time": 2126.01280609523 } 
    { "lastTime": 2126.01280609523, "time": 2126.81172114285, "pin": E2, "state": true } 
    { "state": false, "lastTime": 2126.81172114285, "time": 2127.11131428571 } 
    { "state": true, "lastTime":2127.11131428571, "time": 2127.11131428571 } 
    { "state": true, "lastTime": 2127.11131428571, "time": 2125.81307733333 } 
    { "state": false, "lastTime": 2125.81307733333, "time": 2132.90344838095 } 
    { "state": true, "lastTime": 2132.90344838095, "time": 2125.91294171428 } 
    { "state": true, "lastTime": 2125.91294171428, "time": 2133.30290590476 } 
    { "state": false, "lastTime": 2133.30290590476, "time": 2133.50263466666 } 
    { "state": true, "lastTime": 2133.50263466666, "time": 2132.40412647619 } 
    { "state": true, "lastTime": 2132.40412647619, "time": 2131.90480457142 } 
    { "state": false, "lastTime": 2131.90480457142, "time": 2133.60249904761 }
    
  • smilar error occurs on the boards button BTN1, but it is more stable.

  • Assuming the firmware is ok, I would go for a not really working button... or - because the trigger is on both edges, it may not work by definition (which would though contradict the reference doc which implies both edges work by design).

    So far, I used buttons - setWatch() - only in one way...

    If needed it two ways, I do the not-repeat one one edge, but on event, do the other edge... (see Software Buttons - Many buttons from just one hardware button. Note: Because it was very early on in my Espruino experience, I did not use the times delivered with the event... today I would do so and it for sure would render in more terse code...).

    In this post about Resistive Touchscreen directly (no touch controller) I use setWatch() too with no glitches what so ever...

    Tat's why I go for a bad button (assume too that your non-standard board and firmware has not 'sicknesses'... and also that your wiring does not add additional bouncing when operating the button...).

  • I have coded a class for this problem. Works great and stable for me. I'm sharing it.

    watchClass=function(pin,callback){
        var lastTime=0;
        lastState=1;
        var interval=setInterval(function(){
            currentState=digitalRead(pin);
            currentTime=getTime();
            if (currentState!=lastState) {
                var e={
                    state:currentState,
                    lastTime:lastTime,
                    time:currentTime
                }
                lastTime=currentTime;
                lastState=currentState;
                callback(e);
            }
        },30);
    }
    pinMode(E2, "input_pulldown");
    x=new watchClass(E2,function(e){
    console.log(e);
    });
    
  • I'm not sure why you'd get 'noise' when the button isn't pressed unless there was a short somewhere (are you able to check and see if the pulldown actually worked?).

    With only a 10ms debounce you'd definitely expect some bouncing to occur when the button was pressed though - I'd probably suggest 50ms as a good number.

    While your code will work, it'll mean that the CPU isn't able to sleep and save power. While not a problem on the F4DISCOVERY (the board is power-hungry anyway!) I wouldn't recommend using the code on something where you care about power consumption :)

  • @Gordon
    Thanks for your response. I think there is no short circuit. There is only one button plugged on the board, no thing else.

    Debounce doesn't help me, I have tried various variables. from 1 to 300.

    I'm trying more than one button, I think it is not a mechanical problem.

    Power saving is not important for me at the moment, I'm pluging it to direct electric source.

  • Polling eats away from doing more of the other stuff... so figuring out where the noise comes from seems to me beneficial anyway. Of course, it all depends... ...on how much other stuff is running and how time critical it is...

    Did you time/profile the class?

  • @allObjects

    No I didn't check time/profile, I don't have any idea how to check it.
    Timing is not critical for me, that's why I will continue on this way.
    I'm going to check if this problem occurs only on my board and write it here.

  • Uh - wait, is time passing at the correct speed?
    Time and lastTime are supposed to be in seconds - that means it had been on for half an hr when you got that data?

    { "state": true, "lastTime": 2107.13843809523, "time":2102.44481219047 } 
    

    How can time be earlier than lastTime?

  • Yes, I have copied and paste it here. All outputs are correct.
    I don't know how can It be.

  • I'd manually try a few getTime()'s at the console, and make sure that it increases at a rate of one second per second.

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

setwatch doesn't work stable

Posted by Avatar for fobus @fobus

Actions