How do I make a button press stay on?
I have a button to pause ongoing music form my DIY MP3 player, however to make it pause I have to hold it. I would like to just press once and it stays off, then again and it's on. So like a switch.

7 Replies
Okay it permanately pauses but I can't resume it
Sorry, made a spelling error:
onAndfOff = false;
should be:
onAndOff = false;
I would save the event in an variable so it can be checked at the next event.
What do you mean by variable. is onAndOff not a variable?
Generally I would debounce that button (in HW or SW) and when there will be "press" (last state High, actual state Low) I will just change the value of onAndOff (onAndOff = ! onAndOff;)
So it would use also variable like lastButtonState3, and on begin of loop (before testing butons) I will set it lastButtonState3= buttonState3;
There is a lot of methods, how to debounce button, it is needed for the button value in program will be pressed/released as user it means, without noise made by bouncing contacts.
Yes, it makes more buttons on one pin possible, at the price of more complicated detecting which button is pressed, but still the same principle can be used (my note just suppose, that status (pressed/released) can be determined somehow)
okay thanks, will do.