Button release flag reflects stale data

Right now, the buttonReleased flag is not being cleared if we check to see if a button has been single, double, multi-clicked, etc. So, checking for buttonSingleClicked and than seconds later (hundreds/thousands of loop invocations later) the buttonReleased flag is still true even though it’s for a button push long, long ago (and far away).

Basically, once any button flag is set, the only way to clear it is to test it. This appears to be true for all of the button flag methods.

I would expect an inquiry on a button (press, release, click) to be consistent with other button flags within a loop() and stale press/release flag data would not be carried into future loop()s.

What are your thoughts on changing the behavior so the button press and release flags are cleared upon exiting a loop if any of the buttonClick methods are checked within the loop?

In any case, the API documentation should make developers aware that buttonPressed and buttonReleased may return a true value even though the buttonClick methods return false.

Below is the output from a simple program to demonstrate the behavior:
Press a button. After 5 secs check the “click flags”. After 10 seconds check the release flag.

Output format: “milliseconds: result”

6144: buttonPressed
11160: buttonSingleClicked
16158: buttonReleased 
18785: buttonPressed
18969: buttonPressed
23976: buttonDoubleClicked
28984: buttonReleased 
30074: buttonPressed
30243: buttonPressed
30474: buttonPressed
35475: buttonMultiClicked
40488: buttonReleased 
42295: buttonPressed
47308: buttonLongPressed
52325: buttonReleased 

Sample program:

#include "blinklib.h"
#include "blinkstate.h"
#include "Serial.h"

ServicePortSerial sp;

void setup() {
  // put your setup code here, to run once:
  sp.begin(500000);
}

unsigned long pressTime = 0;

void loop() {
  // put your main code here, to run repeatedly:
    if (buttonPressed()) {
        pressTime = millis();
        sp.print(pressTime);
        sp.println(": buttonPressed");
    }
    if((millis() - pressTime) > 5000) {
      if(buttonSingleClicked()) {
        sp.print(millis());
        sp.println(": buttonSingleClicked");
      }
      if(buttonDoubleClicked()) {
        sp.print(millis());
        sp.println(": buttonDoubleClicked");
      }
      if(buttonMultiClicked()) {
        sp.print(millis());
        sp.println(": buttonMultiClicked");
      }
      if(buttonLongPressed()) {
        sp.print(millis());
        sp.println(": buttonLongPressed");
      }
    }
    // check 10 secs later
    if((millis() - pressTime) > 10000) {
        if (buttonReleased()) {
          sp.print(millis());
          sp.println(": buttonReleased ");
        }
    }
}

I think this makes a lot of sense, and should make it into the API. Currently, the flag gets set back to false after checking the flag… in this new case, it might make sense to only reset the flag at the end of a loop. (i.e. buttonPressed() remains true for the entire loop if in fact the button was pressed.

There’s plenty more to respond to here, just wanted to let you know I am reading it an plan to respond, it hasn’t been simply into the ether :slight_smile:

Please forgive me for grave-digging this post, but was this issue explored further? I’ve been trying to trouble shoot an issue on my QuickMatch game, and it almost seems as though the “buttonPressed” flag is getting stuck for some of the tiles. I was thinking it was my code but I remembered seeing this issue about “stale data” a while back…