Missing something setting up a button

Posted on
  • I've finally started playing with my Espruino, going through a few of the tutorials and trying a few things out.
    I'm having trouble replicating the BTN1 functionality on a external/extra button. I've connected a press button between pin C1 and 3.3v and my test code is

    function onInit() { 
    pinMode(C1, "input");
     }
    
     function buttonPressWatcher(e) {
        console.log("pressed!" + e.time); 
    }
    
    function buttonReleaseWatcher(e) {  
        console.log("Release!" + e.time); 
    }
    
    setWatch(buttonPressWatcher, C1, { repeat: true, edge: "rising", debounce:100}); 
    //setWatch(buttonReleaseWatcher, C1, { repeat: true, edge: "falling"}); 
    setWatch(buttonReleaseWatcher, C1, { repeat: true, edge: "falling", debounce:100}); 
    
    setWatch(buttonPressWatcher, BTN1, { repeat: true, edge: "rising", debounce:100});
    //setWatch(buttonReleaseWatcher, BTN1, { repeat: true, edge: "falling"}); 
    setWatch(buttonReleaseWatcher, BTN1, { repeat: true, edge: "falling", debounce:100});
    
    

    A press and release of BTN1 results in the logging of a press and release. A press of the button on C1 only results in the logging of the press.
    If I take the debounce parameter off the C1 setwatch command, I get a continual stream of "Release!" log messages.
    Any hints as to what I'm missing would be appreciated

  • Do you have a resistor pulling C1 down to ground as well? That's usually needed, otherwise the signal will just 'float' when the button isn't pressed.

    Espruino has resistors internally that can be used, so all you need to do is to replace your pinMode command with: pinMode(C1, "input_pulldown");.

    Also, I'd remove the pinMode command from onInit and would just execute it normally - Espruino will automatically remember the state of pins when you type save anyway, so there's no need to set them on startup.

  • Thanks, after doing both steps it's all working as expected now.

    For my own sanity I also tried putting my own resistor between C1 and ground instead of the internal own - probably unsurprising that works as well.

    With the benefit of hindsight I can see how the solution works, but I'm a little confused as to where the signal float is coming from. Is this to do with the quality of the switch or just the way of the world and something I'll learn to live with?

    Thanks again

  • It's just the way things are I'm afraid - the digital inputs are relatively sensitive, so pretty much any external input can change them. Without the resistor, even touching 3.3v with one hand and C1 with the other hand will be enough to register a button press.

    But without the resistor (even if the input held its last value) you'd only ever be able to turn the input on, not off. You'd need another button connected to GND for that :)

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

Missing something setting up a button

Posted by Avatar for Radar @Radar

Actions