Button press and release timing? (Not HOLD)

It it possible to get timing specific actions from a button press and a release? (Not a HOLD).

As in, when I press a button, something happens, then when I release it, something else happens (even if it's 3ms later).

I'm using the Button library at the moment, to handle debouncing issues, but can't really wrap my head around how to code that.

I've got this at the moment:

void loop(){
  if (button.uniquePress()){
    digitalWrite(SWITCH, HIGH);
    digitalWrite(SWITCH, LOW);
    digitalWrite(SWITCH, HIGH);
  } 
  if (button.stateChanged()){
    digitalWrite(SWITCH, LOW);
  }
}

But I don't know if this will do what I want as it says stateChanged, (and wasPressed) go off the previous isPressed, but I don't have any isPressed in my code at all, so I don't know if it means "Press" call.

The main thing is I need the timing of the events to stay intact (to what the actual button press is), so short button presses and long presses would have the second event delayed differently.

The piece of code above is meant to, when the initial button is pressed, it closes/opens/closes a transistor switch, then when the button is released, it opens it again. I'm trying to build something that sort of sends a "fake" double-tap. So when you press the button, it sends a "tap" (close/open), before sending the actual "tap" (close, then open whenever I let go).

I was just thinking, depending on how a transistor switch works, particularly in this context, but can I just use isPressed, and it will constantly return true, and subsequently digitalWrite(HIGH), over and over and over, for as long as the button is pressed, then use an "else" to digitalWrite(LOW), over and over and over again.

Like so:

if (button.isPressed()){
digitalWrite(SWITCH, HIGH);
} else {
digitalWrite(SWITCH, LOW);
}
}

So basically while the button is pressed, it would be spamming the transistor with a HIGH reading, and when the button isn't pressed, it will be spamming the transistor with a low reading.

It's not exactly elegant or anything, but would something like that work?

Actually, for the bit of code in my first post, I can see how that wouldn't work as it's sending several commands while the button is held down, so spamming that would not be desirable.

Try this:

button.isPressed(); //update internal button states
if (button.stateChanged())
{
  if (button.wasPressed())
  {
    //something happens
  }
  else 
  {
    //something else happens
  }
}

:slight_smile:

Ah, so you can just have blank.isblank on it's own?

Awesome, thanks!

So for multiple buttons, I would just have a chunk of .isPressed at the start of the loop?

void loop(){
  button1.isPressed();
  button2.isPressed();
  button3.isPressed();
  if (button1.stateChanged()){
    if (button1.wasPressed()){
    digitalWrite(SWITCH, HIGH);
  } else {
    digitalWrite(SWITCH, LOW);
    }
  }
  if (button2.stateChanged()){
    if (button2.wasPressed()){
    digitalWrite(SWITCH, HIGH);
    digitalWrite(SWITCH, LOW);
    digitalWrite(SWITCH, HIGH);
  } else {
    digitalWrite(SWITCH, LOW);
  }
  }
}