A lot of time I can't solve one problem. The all code works as I want except incrementing and decrementing of global variable named "waiting". The double pushing of button should decrement the variable and triple pushing of button should increment the variable. But this global variable behave like local variable. It starts from initial meaning equal 2 every pushing of button. Thus double pushing of button decrement it to 1. Next double pushing of button makes the same result. Similar situation with triple pushing. Every triple pushing of button makes "waiting" variable equal 3.
Whole code and output of com port in attachment.
Here the piece of code that I meant:
if (butt1.isDouble()) {
if (setLev == 1) {
print(F("waiting before = "), &waiting);
--waiting;
print(F("waiting after = "), &waiting);
Serial.println(F("hold the button to save waiting variable"));
if (waiting <= 0) waiting = 1; // guard from assigning variable 0 value
}
}
if (butt1.isTriple()) {
if (setLev == 1) {
print(F("waiting before = "), &waiting);
++waiting;
print(F("waiting after = "), &waiting); // Triple click check. If true it raises of current value.
if (waiting > 5) waiting = 5; // guard from assigning variable big value
Serial.println(F("hold the button to save waiting variable"));
}
}
Here the piece of output of com port:
...
23:13:57.311 -> printF waiting before = 2
23:13:57.311 -> printF waiting after = 3
23:13:57.349 -> hold the button to save waiting variable
23:14:00.013 -> printF waiting before = 2
23:14:00.013 -> printF waiting after = 3
23:14:00.013 -> hold the button to save waiting variable
23:14:03.795 -> printF waiting before = 2
23:14:03.795 -> printF waiting after = 1
23:14:03.830 -> hold the button to save waiting variable
23:14:09.580 -> printF waiting before = 2
23:14:09.580 -> printF waiting after = 1
23:14:09.619 -> hold the button to save waiting variable
...
The code is so hard to follow I'm not sure what it is supposed to do. I would suggest removing functionality and debugging it in stages.
I observe that waiting only gets saved to EEPROM if setLev == 1. However, I also observe that in the following code segment if setLev is 1-7 then the waiting value will be read from EEPROM in order to evaluate the conditional. I suspect that is where it is getting reset.
if (setLev > 0 && setLev < 8) {
if (switchCounter < EEPROM.get(2, waiting)) {
if (millis() - timeCounter >= 250) {
digitalWrite(LED_BUILTIN, LEDflag); // turn on or turn off LED
timeCounter = millis(); // assigning the last millis() value
if (!LEDflag) ++switchCounter;
LEDflag = !LEDflag; // inverting of flag
}
}
else if (millis() - timeCounter >= 1000) {
switchCounter = 0;
}
}
ToddL1962:
The code is so hard to follow I'm not sure what it is supposed to do. I would suggest removing functionality and debugging it in stages.
I observe that waiting only gets saved to EEPROM if setLev == 1. However, I also observe that in the following code segment if setLev is 1-7 then the waiting value will be read from EEPROM in order to evaluate the conditional. I suspect that is where it is getting reset.
if (setLev > 0 && setLev < 8) {
if (switchCounter < EEPROM.get(2, waiting)) {
if (millis() - timeCounter >= 250) {
digitalWrite(LED_BUILTIN, LEDflag); // turn on or turn off LED
timeCounter = millis(); // assigning the last millis() value
if (!LEDflag) ++switchCounter;
LEDflag = !LEDflag; // inverting of flag
}
}
else if (millis() - timeCounter >= 1000) {
switchCounter = 0;
}
}