Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

button.isPressed(); seems to stay latched. #53

Closed
Fishbone69 opened this issue Apr 1, 2023 · 1 comment
Closed

button.isPressed(); seems to stay latched. #53

Fishbone69 opened this issue Apr 1, 2023 · 1 comment

Comments

@Fishbone69
Copy link

Fishbone69 commented Apr 1, 2023

Hi. I am wiring a GUI for a digital watch/wearable. I am using the Button2 library and the isPressed() method to determine when selections are made. Perhaps my understanding of the the method is incorrect. I assume if I have a button e.g. buttonBack and I use
if (buttonBack.isPressed()){ insert code }

this will only be executed if the button is currently being pressed. What I am finding is that this seems to stay "latched" on many occasions and results in buttonBack.isPressed() evaluating to a value of 1 even if the button is not being pressed. I have played around with the debounce times and do not believe it is noise. I will include the loop portion of my code and delete some of the lines that are not relevant so you have an idea of what I am doing.
`void loop() {

switch (mode){
case 0 :

if (oldMode!=mode){
int numMenuItems = 4;
buttonUp.setClickHandler([scrollDirection, numMenuItems, bitmaps1=bitmaps1, menuLabels1=menuLabels1](Button2 &btn){
scrollMenu(RIGHT, numMenuItems, bitmaps, menuLabels,btn);
});
buttonUp.setLongClickDetectedHandler([scrollDirection, numMenuItems, bitmaps1=bitmaps1, menuLabels1=menuLabels1](Button2 &btn){
scrollMenu(RIGHT, numMenuItems, bitmaps, menuLabels,btn);
});
buttonUp.setLongClickDetectedRetriggerable(1);
buttonDown.setClickHandler([scrollDirection, numMenuItems, bitmaps1=bitmaps1, menuLabels1=menuLabels1](Button2 &btn){
scrollMenu(LEFT, numMenuItems, bitmaps, menuLabels,btn);
});
buttonDown.setLongClickDetectedHandler([scrollDirection, numMenuItems, bitmaps1=bitmaps1, menuLabels1=menuLabels1](Button2 &btn){
scrollMenu(LEFT, numMenuItems, bitmaps, menuLabels,btn);
});
buttonDown.setLongClickDetectedRetriggerable(1);
currentMenuSelection=0;

}
if (buttonBack.isPressed()){
mode=1;
oldMode=0;
break;
}
if (buttonEnter.isPressed()){
mode=currentMenuSelection+2;
break;
}

oldMode=mode;
loopbuttons();
break;

case 1:
if (buttonUp.isPressed()|buttonDown.isPressed()){
mode=0;
oldMode=1;
break;
}
printScreen();
loopbuttons();
break;

case 2: //settings
if (oldMode!=mode){

 }

if (buttonBack.isPressed()){
mode=0;
oldMode=1;
break;
}
oldMode=mode;
loopbuttons();
break;

case 3: //Alarm
if (oldMode!=mode){
}
if (buttonBack.isPressed()){
mode=0;
oldMode=1;
break;
}
oldMode=mode;
loopbuttons();
break;

case 4: //Stopwatch
if (oldMode!=mode){
}
if (buttonBack.isPressed()){
mode=0;
oldMode=1;
break;
}
oldMode=mode;
loopbuttons();
break;

case 5: //Flashlight
if (oldMode!=mode){
}
if (buttonBack.isPressed()){
mode=0;
oldMode=1;
break;
}
oldMode=mode;
loopbuttons();
break;
}

}

one thing of interest is that the code may encounter the ' if (buttonBack.isPressed()){' multiple times prior to seeing loopbuttons(); which is simply the following:
'void loopbuttons(){
buttonUp.loop();buttonEnter.loop();buttonDown.loop();buttonBack.loop();
if (alarmFlag==1){
alarmRing();
}
if ((millis()-lastButtonPressedTime)>(buttonInactiveSleepTime*1000)){
deepSleep(dummyButton);
}
}'
At first I thought this was the issue, but even If I add loopbuttons(); at various locations, it still doesn't seem to work. I can always use a button clickHandler, but that is a bunch more code than simply using isPressed();

@LennartHennigs
Copy link
Owner

Hey, isPressed() is only true during the fact. Without spending too much time reading your code wasPressed() might be the function that you are looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants