Button Library - State Changed Question

hi, i am curious as to how the stateChanged() function in the button library works?
i need to use a momentary switch to send one thing with the first press then another thing with the second press, then back to the first and so on...just to cycle though 2 different messages.

i have been able to achieve it with a bunch of code but the button library makes things so much easier/neater. plus i am already using it for the uniquePress() function.

i am just not sure how to set up the stateChanged as there aren't any examples that i could find.

Which button library are you using?

AlphaBeta's: Arduino Playground - Button Library

In that case, stateChanged is a function that takes no arguments, and returns true if the state of the button has changed since it was last checked, or false if it hasn't.

Button b(10, PULLUP);

int changeCnt = 0;
void loop()
{
   if(b.stateChanged())
      changeCnt++;
}

Note that this does not tell you what the change was (pressed to released or released to pressed), only that a change occurred.

hmm wont that just send out "message 1" when the button is pressed and then send "message 2" when the button is released?

i am after "message 1" to be sent when the button is pressed, then nothing when the button is released. then on the second press of the button "message 2" would be sent.

i hope that makes sense, its getting late!

See the uniquePress() function :slight_smile:
Then, increment a counter inside an if check, and act upon it:

if(b.uniquePress()) {
      changeCnt++;
}
//act upon change count

awesome, i will give that a shot!