marcobrianza / ClickButton

A simple button Arduino library to get short and long clicks, multiple clicks (double click, triple click etc.). Click-and-hold is also possible.
63 stars 28 forks source link

More than 1 sec Long press Not working #12

Open SrGalindo opened 2 years ago

SrGalindo commented 2 years ago

Hello, First of all thanks for this library, it's very interesting. I'm trying the MultiClicks example in a Esp32.

I found that when I do more than once short click it always calls the same function. In the following code "2 something" is displayed in loop in the serial monitor.

if(LEDfunction == 2){
     doSomethingWhenTwoShortClicks();
  } 

void doSomethingWhenTwoShortClicks(){
    Serial.println("2 something");
}

Also found that it never reaches the long press for more than 1 sec. The following code always would call doSomethingLongPress();

if(LEDfunction == -1){
     doSomethingLongPress();
  } 

if(LEDfunction == -2){
     doSomethingLongPressTWO();
  } 
canova-marco commented 2 months ago

Hi, it's been a while since you posted this but I have had the same problem just today. There are some errors in the library. I fixed this issue making these changes to the file ClickButton.cpp. Line 152 (I've commented the original code):

// Check for "long click"
  //if (depressed && (now - _lastBounceTime > longClickTime)) <-- Error in parenthesis
  if (depressed && (now - _lastBounceTime) > longClickTime)
  {
    // negative count for long clicks
    //clicks = 0 - _clickCount;
    clicks = 0 - (now - _lastBounceTime)/longClickTime; // Calculate the seconds from press to release
    _clickCount = 0;
    if(clicks != 0) changed = true;
  }

It works for me. Hope it helps

canova-marco commented 2 months ago

You can also modify the last IF condition if you don't want the button state changes while being pressed:

if(clicks != 0 && clicks!=Prev_clicks) changed = true;

The whole code block will become:

// Check for "long click"
  //if (depressed && (now - _lastBounceTime > longClickTime))
  if (depressed && (now - _lastBounceTime) > longClickTime)
  {
    int Prev_clicks=clicks;
    // negative count for long clicks
    //clicks = 0 - _clickCount;
    clicks = 0 - (now - _lastBounceTime)/longClickTime;
    _clickCount = 0;
    if(clicks != 0 && clicks!=Prev_clicks) changed = true;
  }