EasyButton expected behaviour

I have been using EssyButton for a while and now faced an odd situation which I bring here: I setup OnPressed and OnPressedFor for the same button instance, expecting to trigger either one of them. I mean, if the button pressed shortly then no long event should be triggered - this works well. But when holding down the button and triggering the long press, both events are dispatched, with a leading long press event followed by the short press.

Is it a glitch or am I doing something "off specs". Any clues/tips?

I can not reproduce this behaviour with the following sketch.

#include <EasyButton.h>

EasyButton button(2);

void onPressed() {
  Serial.println("Button has been pressed and released.");
}

void onPressedForDuration() {
  Serial.println("Button has been pressed for the given duration.");
}

void setup() {
  Serial.begin(9600);

  button.begin();
  button.onPressed(onPressed);
  button.onPressedFor(2000, onPressedForDuration);
}

void loop() {
  button.read();
}

Tested with a simulator, you can play with it yourself too.

Same finding here. After I realized the OP is using EasyButton not ezButton. :expressionless:

So... @arnaldodg, post the code that does what you claim so we can see for ourseffs.

a7

Sorry, folks. Here it is:

#include <Adafruit_SSD1306.h> // v2.5.1
#include <EasyButton.h>  // v.2.0.1
#include <ArduinoSort.h>
#include <EEPROM.h>  // v.2.0.0

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDR 0x3C /// <- 0x3C for 128x32 ; 0x3D for 128x64 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);

...
#define xyz
...

// Event Types
#define ET_NONE 0
#define ET_BACK 1
#define ET_SELECT 2
#define ET_LEFT 3
#define ET_RIGHT 4
#define ET_PAGELEFT 5
#define ET_PAGERIGHT 6
byte evtType = ET_NONE;

#define BTN_LEFT_PIN  3//2
#define BTN_RIGHT_PIN 5//3
#define BTN_ENTER_PIN 2//5

#define LONG_TRIGGER 250

// Button Instances
EasyButton btnLeft(BTN_LEFT_PIN); // EasyButton btnLeft(int BTN_LEFT_PIN, uint32 debounce, bool pullup, invert);
EasyButton btnRight(BTN_RIGHT_PIN);
EasyButton btnEnter(BTN_ENTER_PIN);

// Button Callbacks
//
void onBtnLeftPressed() {
  evtType = ET_LEFT;
}

void onBtnRightPressed() {
  evtType = ET_RIGHT;
}

void onBtnLeftLongPressed() {
  evtType = ET_PAGELEFT;
}

void onBtnRightLongPressed() {
  evtType = ET_PAGERIGHT;
}

void onBtnEnterPressed() {
  evtType = ET_SELECT;
}

void onBtnEnterLongPressed() {
  evtType = ET_BACK;
}

void setup(){
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDR); //0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)

  display.clearDisplay();
  display.display();

  ...
  display cmds
  ...

  // INIT Buttons !!!
  btnLeft.begin();
  btnRight.begin();
  btnEnter.begin();

  // SETUP
  btnLeft.onPressed(onBtnLeftPressed);
  btnLeft.onPressedFor(LONG_TRIGGER, onBtnLeftLongPressed);
  btnRight.onPressed(onBtnRightPressed);
  btnRight.onPressedFor(LONG_TRIGGER, onBtnRightLongPressed);

  btnEnter.onPressed(onBtnEnterPressed);
  btnEnter.onPressedFor(LONG_TRIGGER, onBtnEnterLongPressed);
}


void loop() {

  btnLeft.read();
  btnRight.read();
  btnEnter.read();

  if (evtType!=ET_NONE) processEvent();

}

void processEvent(){
  // dispatch actions based on EventType
  ...
   
  // resolve action/event
  evtType = ET_NONE;
}

Arduino Nano (328P)
VSCode dev-env Arduino v0.4.12

I webt to some trouble (fun!) to make an executable sketch out of your heavily redacted code.

See it, play with it here:

The problem is in the code you haven't posted. Or in the wiring which you haven't shared. Hint.

a7

Thanks for your support, @alto777.
Here goes the SCH (pdf attch).

What do you mean with the a7 hint? Analog7 is not being used, though.

Just make sure, can we use Short and Long press evt altogether on the same button?
Is it a possible intended use of the EasyButton lib?

Anyhow, I will benchtest the code as you advised, and try to isolate the behavior context.
My guess is that there is something related to the OLED display lib routines... that is something not included in your test bench. What do you think?

Best regards, AD
mCart.pdf (32.4 KB)

Did you take my link, run the code you (mostly) wrote and observe that yes, shirt and long press events are fine? Go do that now if not. There is nothing like a real test, even using a simulator. :expressionless:

The hint was just to make you realize we need to see a schematic. "a7" is how I end a post, short for @alto777. Sry.

EasyButton is fine. Demonstration of your intended use deployment works well. The schematic looks, at a glance, plausible, but may have an answer.

Personally I would turn up the time it takes for a long press to be the thing. But that is not a problem: one will always get one or the other event, one event per press.

I remain saying the problem is in your real code. Hint. Which is to say, post the code where you think you are getting multiple messages or events for a single click, no matter the duration, of a given button.

Someone else will have to find the problem if three is one in you schematic.

a7

Nice! Just like I stamp "AD" you coined "a7" =]

I ran your sketch and it works great, no glitches.
Should follow your recommendations after a few tests you made me think of.

regards, AD

Hello there.

After @alto777 's advices I dblchecked everything under isolate contexts. The only odd thing I've found was that pull-ups were enabled on EasyButton button inits. I am also using pull-ups in my CKT so I disabled the internal pull-ups and somehow the buttons are behaving correctly. I can't tell for sure but that seems to be the "bug".

Should post here any other consistent findings.

rgds, AD

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.