How do I detect two simultaneous button presses?

Posted on
  • Hi,
    How do I detect two simultaneous button presses? I've tried putting a setWatch() inside another setWatch(), but the inner watch still activates even when I don't trigger the outer one. Any tips?

    Thanks!

  • Hi! It's basically impossible for you to press both buttons at exactly the same time, so instead you just need to shift thinking a bit - call the same function when either button is pressed, then check the state of both in that function:

    function check() {
      var a = BTN1.read();
      var b = BTN2.read();
      if (a&&b) console.log("Both pressed!");
    }
    setWatch(check, BTN1, {repeat:true, edge:"rising", debounce:20});
    setWatch(check, BTN2, {repeat:true, edge:"rising", debounce:20});
    
  • Just what @Gordon says... though: since you never really press them at the same time but have watches on both, you get two events in the event queue... first one is served and you check for both buttons, but you set a flag for the button you just checked so that when its press event will about to be served, you do not count it as a press, but just clear the flag... ;-)... so the code may look like this:

    // Watch Id, Flag, Callback and set watch for BTN1 and BTN2
    var w1Id = null;
    var b1F = false;
    function b1Cb() {
      if (b1F) {
        b1F = false; // ignore press event, both but reset flag
      } else {
        if (b2F = BTN2.read()) { // read and set flag for other btn (BTN2)
          console.log("Both pressed, but BTN1 made the race!");
          // ...do what to do when both buttons have been pressed simultaneously
        } else {
          console.log("Just BTN1 has been pressed!")
          // ...do what to do when BTN1 has been pressed
        }  
      }
    }
    var w2Id = null;
    var b2F = false;
    function b2Cb() {
      if (b2F) {
        b2F = false; // ignore press event, both but reset flag
      } else {
        if (b1F = BTN1.read()) { // read and set flag for other btn (BTN1)
          console.log("Both pressed, but BTN2 made the race!");
          // ...do what to do when both buttons have been pressed simultaneously
        } else {
          console.log("Just BTN2 has been pressed!")
          // ...do what to do when BTN2 has been pressed
        }  
      }
    }
    
    function r() { // run - enter r() in console (while testing)
      b1F = false;
      w1Id = setWatch(b1Cb, BTN1, {repeat:true, edge:"rising", debounce:20});
      b2F = false;
      w2Id = setWatch(b2Cb, BTN2, {repeat:true, edge:"rising", debounce:20});
    }
    
    function h() { // halt or pause - enter p() in console (while testing)
      if (w1Id) { clearWatch(w1Id); w1Id = null; }
      if (w2Id) { clearWatch(w2Id); w2Id = null; }
    }
    

    ...this is though not the full truth but the truth either... but I let you figure that out yourself... hint: what is when you press BTN1 and hold it... and then 'later' you also press BTN2 - while BTN1 is still held? ...so you may watch both edges as well as define windows where presses are presses and when they are separate ones, even though presses may overlap...

    Enjoy!

    PS: Code not tested (yet)... but it is a nice 'problem' to solve!

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

How do I detect two simultaneous button presses?

Posted by Avatar for BootySnorkeler @BootySnorkeler

Actions