user27311: I am using the EasyButton library to detect button presses on an arduino.
I want do execute a function when the user

Presses a button
Presses and holds a button
Releases a button

Detecting when a user presses or holds a button is straight forward
#include <Arduino.h>
#include <EasyButton.h>
# define BUTTON_PIN 2;
EasyButton button(BUTTON_PIN);

void onPressedCallback() {
  Serial.println("Button was pressed and released");
}

void onHoldCallback() {
  Serial.println("Button was pressed and held");
}

void setup() {
  button.begin();
  button.onPressed(onPressedCallback);
  button.onPressedFor(buttonHoldDuration, onHoldCallback);
}

The easyButton library has the following events

onPressed
onPressedFor
onSequence

It has the following states

isPressed
isReleased
wasPressed
wasReleased
pressedFor
releasedFor

How would you trigger an event when a button is 'released'?
Full Documentation
https://easybtn.earias.me/docs/on-single-press-api
What I've tried

Check the state from inside an event

void onHoldCallback() {
  while (button.isPressed() {
     Serial.println("you are still holding the button");
  }
  Serial.println("Button was released");
}

This unfortunately creates an infinite loop since the button state is never refreshed (button.begin();)
Additional Resources

https://github.com/evert-arias/EasyButton/tree/main/examples
Source code for the system interupt https://github.com/evert-arias/EasyButton/blob/4e818410252e9518564fc55f8d4a976fac70a9b2/src/EasyButton.cpp#L55-L82


user37523: The onPressed() event Triggers a callback function when the button has been pressed and released.
Here's a test sketch to show what I mean. I've also added a function you could use to detect the moment the button is first pressed.
#include <EasyButton.h>

// EasyButton(uint8_t pin, 
//            uint32_t debounce_time = 35, 
//            bool pullup_enable = true, 
//            bool active_low = true)
EasyButton button(2);

void onReleased(){
  Serial.println("Button Releast");
}

void firstPressed(){
  Serial.println("First Pressed");
}

void setup(){

  // Test output to serial monitor.
  Serial.begin(9600);

  // Initialize the button.
  button.begin();

  // Add the callback function to be called
  // when the button is pressed then released.
  // NOTE: This function is only called AFTER
  // the button has been released.
  button.onPressed(onReleased);

}

void loop(){

  // Continuously read the status of the button.
  button.read();

  // Call a function the moment the button is pressed.
  if(button.wasPressed()){
    firstPressed();
  }

}


27311: <p>I am using the <a href="https://github.com/evert-arias/EasyButton" rel="nofollow noreferrer">EasyButton library</a> to detect button presses on an arduino.</p>
<p>I want do execute a function when the user</p>
<ol>
<li>Presses a button</li>
<li>Presses and holds a button</li>
<li>Releases a button</li>
</ol>
<p>Detecting when a user presses or holds a button is straight forward</p>
<pre><code>#include &lt;Arduino.h&gt;
#include &lt;EasyButton.h&gt;
# define BUTTON_PIN 2;
EasyButton button(BUTTON_PIN);

void onPressedCallback() {
  Serial.println(&quot;Button was pressed and released&quot;);
}

void onHoldCallback() {
  Serial.println(&quot;Button was pressed and held&quot;);
}

void setup() {
  button.begin();
  button.onPressed(onPressedCallback);
  button.onPressedFor(buttonHoldDuration, onHoldCallback);
}
</code></pre>
<p>The easyButton library <a href="https://easybtn.earias.me/docs/on-single-press-api" rel="nofollow noreferrer">has the following events</a></p>
<ul>
<li>onPressed</li>
<li>onPressedFor</li>
<li>onSequence</li>
</ul>
<p>It has the following states</p>
<ul>
<li>isPressed</li>
<li>isReleased</li>
<li>wasPressed</li>
<li>wasReleased</li>
<li>pressedFor</li>
<li>releasedFor</li>
</ul>
<p><strong>How would you trigger an event when a button is 'released'?</strong></p>
<p>Full Documentation</p>
<p><a href="https://easybtn.earias.me/docs/on-single-press-api" rel="nofollow noreferrer">https://easybtn.earias.me/docs/on-single-press-api</a></p>
<h2>What I've tried</h2>
<ol>
<li>Check the state from inside an event</li>
</ol>
<pre><code>void onHoldCallback() {
  while (button.isPressed() {
     Serial.println(&quot;you are still holding the button&quot;);
  }
  Serial.println(&quot;Button was released&quot;);
}
</code></pre>
<p>This unfortunately creates an infinite loop since the button state is never refreshed (button.begin();)</p>
<h3>Additional Resources</h3>
<ul>
<li><a href="https://github.com/evert-arias/EasyButton/tree/main/examples" rel="nofollow noreferrer">https://github.com/evert-arias/EasyButton/tree/main/examples</a></li>
<li>Source code for the system interupt <a href="https://github.com/evert-arias/EasyButton/blob/4e818410252e9518564fc55f8d4a976fac70a9b2/src/EasyButton.cpp#L55-L82" rel="nofollow noreferrer">https://github.com/evert-arias/EasyButton/blob/4e818410252e9518564fc55f8d4a976fac70a9b2/src/EasyButton.cpp#L55-L82</a></li>
</ul>
 37523: <p>The <code>onPressed()</code> event <a href="https://easybtn.earias.me/docs/on-single-press-api/" rel="nofollow noreferrer">Triggers a callback function when the button has been pressed and released</a>.</p>
<p>Here's a test sketch to show what I mean. I've also added a function you could use to detect the moment the button is first pressed.</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;EasyButton.h&gt;

// EasyButton(uint8_t pin, 
//            uint32_t debounce_time = 35, 
//            bool pullup_enable = true, 
//            bool active_low = true)
EasyButton button(2);

void onReleased(){
  Serial.println(&quot;Button Releast&quot;);
}

void firstPressed(){
  Serial.println(&quot;First Pressed&quot;);
}

void setup(){

  // Test output to serial monitor.
  Serial.begin(9600);

  // Initialize the button.
  button.begin();

  // Add the callback function to be called
  // when the button is pressed then released.
  // NOTE: This function is only called AFTER
  // the button has been released.
  button.onPressed(onReleased);

}

void loop(){

  // Continuously read the status of the button.
  button.read();

  // Call a function the moment the button is pressed.
  if(button.wasPressed()){
    firstPressed();
  }

}
</code></pre>
