Ezbutton shows release before press

I using the simple button event sample. Here is the code

#include <ezButton.h>

ezButton button(38);  // create ezButton object that attach to pin 7;

void setup() {
  Serial.begin(115200);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed())
    Serial.println("The button is pressed");

  if(button.isReleased())
    Serial.println("The button is released");
}

This is the serial ouput

The button is released

The button is pressed

The isReleased fires before the isPressed. Why? These are momentary on switches.

Hello ruger2019

This is the current state of the switch by starting the sketch.

I have set the pins high before the loop, and low before the loop, does not seem to make any difference.

How do I get the "correct" event?

Hello ruger2019

Take a view into the libary how this case is handled.

I am using the example from the library. Does isPressed mean is release in arduino speak? Not sure I am tracking.

Because the ezButton uses INPUT_PULLUP, your button needs to be wired between pin and GND. Did you?

Your two events in the library

bool ezButton::isPressed(void) {
	if(previousSteadyState == HIGH && lastSteadyState == LOW)
		return true;
	else
		return false;
}

bool ezButton::isReleased(void) {
	if(previousSteadyState == LOW && lastSteadyState == HIGH)
		return true;
	else
		return false;
}

So isPressed() will return true if the button went from HIGH to LOW and isReleased() returns true when the button goes from LOW to HIGH.

That is the missing part of the puzzle. I have a resistor in the wrong spot. I reversed the LOW and HIGH in the event handlers and all seems to work.

Will rewire when I can.

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