cancel
Showing results for 
Search instead for 
Did you mean: 

Set Date and Time

willow
Associate III

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++;

}

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

@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 ...

 



 

View solution in original post

3 REPLIES 3

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


@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 ...

 



 

In deed by doing this way it changes everything thank you very much !