A
Arduino2mo ago
EMAN

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.
No description
7 Replies
EHTspark
EHTspark2mo ago
} else {
if (buttonState3 == LOW) {
if (onAndOff == true) {
onAndfOff = false;
myDFPlayer.start();
Serial.println("DFPlayer resume"));
}
}
}
} else {
if (buttonState3 == LOW) {
if (onAndOff == true) {
onAndfOff = false;
myDFPlayer.start();
Serial.println("DFPlayer resume"));
}
}
}
EMAN
EMANOP2mo ago
Okay it permanately pauses but I can't resume it
EHTspark
EHTspark2mo ago
Sorry, made a spelling error: onAndfOff = false; should be: onAndOff = false;
ABOS80
ABOS805w ago
I would save the event in an variable so it can be checked at the next event.
EMAN
EMANOP5w ago
What do you mean by variable. is onAndOff not a variable?
Gilhad
Gilhad5w ago
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)
EMAN
EMANOP5w ago
okay thanks, will do.

Did you find this page helpful?