2024-05-30 02:51 AM
Hello everyone,
Im working on a Program that i have written in a StateDiagram way. I have 3 buttons wired to my micro, with a strategy to avoid boucing.
this a part of my program where if the button is PRESSED then we add 1 to the variable "Day". The problem is that because the while(1) loop is very quick, the incrementation is very fast. I have experienced to put a Delay after but this is not precise at all and a too short or to long press on the button would ruin it.
SO, do you have a solution for me that even by pressing longly, the value of Day would not increment more than 1 time ?
I hope ive made myself understandable, im wainting for your suggestion
if(PbState[1] == PRESSED)
{
Day++;
}
Solved! Go to Solution.
2024-05-30 04:02 AM
@waclawek.jan wrote:You want to increment upon *change* not upon *state*, i.e.
Indeed.
@willow Or, in other words, on the event - not the state.
But, in the case of setting date & time, you probably do want a state of "button is held" - in which case the value auto-increments...
Like the auto-repeat on a keyboard ...
2024-05-30 03:54 AM
You want to increment upon *change* not upon *state*, i.e.
static uint8_t previousPbState1 = NOT_PRESSED;
if ((previousPbState1 == NOT_PRESSED) && PbState[1] == PRESSED)) {
Day++;
}
previousPbState1 = PbState[1];
JW
2024-05-30 04:02 AM
@waclawek.jan wrote:You want to increment upon *change* not upon *state*, i.e.
Indeed.
@willow Or, in other words, on the event - not the state.
But, in the case of setting date & time, you probably do want a state of "button is held" - in which case the value auto-increments...
Like the auto-repeat on a keyboard ...
2024-05-30 05:23 AM
In deed by doing this way it changes everything thank you very much !