• 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!

About

Avatar for allObjects @allObjects started