I have a button "long press" routine that uses a while loop. I reuse the same exact code (which references global variables) in two separate functions within my program, in one it works, in the other it exits after one test cycle and proceeds... this is very odd.
I tried rewriting it as a single function, called by both other functions, but I still don't see WHY it would fail?
guess what? even as a separate function it fails the same way...
Any ideas?
int runstate = 0; //this variable is used elsewhere, but in each case it is re-initialized in my working and non-working while loops
byte st1 = 0; //these are used only by the long press checker code.
byte st2 = 0;
//other setup....
//the code is large, so I'm only including the failing parts...
void longpresser() {
sbounce.update();
st1 = sbounce.read();
if (st1 == LOW) {
delay(2000);
sbounce.update();
st2 = sbounce.read();
if (st2 == st1) {
runstate = 1;
}
}
}
void resetter() { //resets the arduino via pin ****THIS ONE WORKS*****
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15,5);
display.println(F("Hold RED To Reset"));
display.display();
runstate = 0;
while (runstate == 0) {
//WORKS CORRECTLY WEATHER I USE THIS CODE OR THE FUNCTION
//sbounce.update();
//byte st1 = sbounce.read();
//if (st1 == LOW) {
// delay(2000);
// sbounce.update();
// byte st2 = sbounce.read();
// if (st2 == st1) {
// runstate = 1;
// }
//}
longpresser();
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(5,5);
display.println(F("Resetting Winder..."));
display.display();
delay(2000);
yardsToWind = 100;
yardsWound = 0;
windingstatus = 0;
softreset();
}
void starter() { //when beginning wind is called for ****THIS ONE EXITS UNEXPECTEDLY****
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15,5);
display.println(F("Hold RED To Start"));
display.display();
runstate = 0;
while (runstate == 0) {
//EXITS UNEXPECTEDLY WEATHER I USE THIS CODE, OR THE FUNCTION
//sbounce.update();
//byte st1 = sbounce.read();
//if (st1 == LOW) {
// delay(2000);
// sbounce.update();
// byte st2 = sbounce.read();
// if (st2 == st1) {
// runstate = 1;
// }
//}
longpresser();
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(5,5);
display.println(F("Beginning Winding..."));
display.display();
delay(2000);
windingstatus = 1;
infoshow();
runner();
}
I can provide the full code if needed, but I just don't understand how this is even possible...