You are reading a single comment by @graf and its replies. Click here to read the full conversation.
  • Could someone please explain me how are the IDs allocated? Or even better why it's not like I thought? :)

    In this code below I would expect the ledTimer to get the same value (ID) over and over again but that doesn't happen.

    var btnPin = BTN;
    var ledPin = LED1;
    var ledTimer = null;
    digitalWrite(ledPin, 0);
    
    var btnWatcher = setWatch(function () {
      if (ledTimer) {
        clearTimeout(ledTimer);
        ledTimer = null;
      }
      digitalWrite(ledPin, 1);
      console.log("before: " + ledTimer);
      ledTimer = setTimeout(function () {
        digitalWrite(ledPin, 0);
        ledTimer = null;
      }, 4000);
      console.log("after: " + ledTimer);
    }, btnPin, {repeat: true, edge: "rising", debounce: 70});
    

    Now when I press the button every 5 seconds I get what I expected:

    before: null
    after: 2
    before: null
    after: 2
    ...and so on
    

    But when I press the button every 3 seconds then it's not what I expected:

    before: null
    after: 2
    before: null
    after: 4
    ...and +2 every time
    
About

Avatar for graf @graf started