Skip to main content
Graduate II
May 30, 2024
Solved

Set Date and Time

  • May 30, 2024
  • 1 reply
  • 1534 views

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

}

 

 

    This topic has been closed for replies.
    Best answer by Andrew Neil

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

     



     

    1 reply

    Super User
    May 30, 2024

    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

    Super User
    May 30, 2024

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

     



     

    willowAuthor
    Graduate II
    May 30, 2024

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