Hello again.
I tried to make a toggle button for the LCD Buttons. This is the first time coding for the LCD Buttons and I don’t know if this is the best way to make a toggle or not too. Is there a way to shorten it?
int buttonCount = 0;
if(nLCDButtons = 4)
{
buttonCount = buttonCount + 1;
}
if(buttonCount = 1)
{
//Action for first time it is pressed
}
else if(buttonCount = 2)
{
//Action for second time it is pressed
}
PS. I didn’t check the code if it works or anything yet.
First, you need to distinguish between declarations of equality (setting values with “=”) and checks for equality (boolean result from “==”).
Do you want to toggle between two options? That’s what I would usually describe “toggle” as doing. The way you’ve written it, you’re moving through options but not looping back. (Also, I assume this is inside a while() loop or something similar so it can keep checking. The bigger problem you’ll have, though, is that you’ll keep firing off your second if statement even if you don’t press the button again. Try something more like this (for two options, but I’ve left the int instead of making it a bool so you can expand to more options more easily):
int buttonCount = 0;
while(true) {
if(nLCDButtons == 4) {
if(buttonCount == 0) {
// First of two toggle options
buttonCount = 1;
}
else if(buttonCount == 1) {
// Second of two toggle options
buttonCount = 0;
}
}
}
Then there is something else you may have to deal with. If your toggle options don’t take much time, it’s going to loop back through this too quickly for the button press and register multiple button presses. The simplest solution is to throw in a delay at the end of the first if statement (nLCDButtons = 4). That still creates problems if you hold the button down for a while. Another option is to begin that if statement (nLCDButtons = 4) with a while loop that just waits for the button to be released, such as while (nLCDButtons = 4) {wait1MSec(10)}.
int buttonCount = 0;
while(true) {
if(nLCDButtons == 4) {
if(buttonCount == 0) {
// First of two toggle options
buttonCount = 1;
}
else if(buttonCount == 1) {
// Second of two toggle options
buttonCount = 0;
}
wait1MSec(30);
}
}
int buttonCount = 0;
while(true) {
if(nLCDButtons == 4) {
while(nLCDButtons == 4) {
wait1MSec(10);
}
if(buttonCount == 0) {
// First of two toggle options
buttonCount = 1;
}
else if(buttonCount == 1) {
// Second of two toggle options
buttonCount = 0;
}
}
}
@callen Good morning, I want it for 4 autonomous selections and so will need 4 options. Sorry for the inconvenience. Is it possible to just exten the button count if / else if thingy like buttoncount = 2, 3, 4 etc?
Yes. That’s why I left the int as is and left “else if” instead of just “else” at the end. You can add in as many else if’s as you’d like, and just cycle back to 0 after the last one. I added one more below so you can see the pattern. Alternatively, you could use switch-case, but I wanted to leave you with the if-else structure you had.
int buttonCount = 0;
while(true) {
if(nLCDButtons == 4) {
while(nLCDButtons == 4) {
wait1MSec(10);
}
if(buttonCount == 0) {
// First of two toggle options
buttonCount = 1;
}
else if(buttonCount == 1) {
// Second of two toggle options
buttonCount = 2;
}
else if(buttonCount == 2) {
// Second of two toggle options
buttonCount = 0;
}
}
}