Button - state machine [solved]

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I tried very hard to understand state machine for button but I couldn't understand it. I saw the State Machine diagram is shown in Figure below. I am trying to understanding it. There are five states as follows. Button can be pressed or released or held continuously so these three state should be in state machine. I don't know what the B_UP state indicate in diagram. I need someone's help because I really don't understand how the state changes and what condition cause state change.

1633190159503.png

Update : suggest better name for states I don't think button UP make more sense
 
Last edited:

Irving

Joined Jan 30, 2016
3,843
Is this your attempt to create a state machine for button debouncing and counting the # of bounces?

There are, arguably 4 states: Button_released, Button_pressing, Button_pressed, and Button_releasing. Button_pressed, and Button_released are static 'output' states. the other two are time dependent transitional states that only exist in the timed 'debounce_window' between the static states and during which the button has no defined output state - or rather its output state is the last known output state.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Is this your attempt to create a state machine for button debouncing and counting the # of bounces?
Yes @Irving, I was trying to understand state machine for button debouncing and counting N of bounces. I don't understand what is happening in that state machine.

There are, arguably 4 states: Button_released, Button_pressing, Button_pressed, and Button_releasing. Button_pressed, and Button_released are static 'output' states. the other two are time dependent transitional states that only exist in the timed 'debounce_window' between the static states and during which the button has no defined output state - or rather its output state is the last known output state.
With an ideal (bounce-less) button, there are just 2 states - pressed and not-pressed;

With a real (bouncy) button, there is a 3rd state where we're waiting to see if it's still bouncing
 

Juhahoo

Joined Jun 3, 2019
302
I tried very hard to understand state machine for button but I couldn't understand it. I saw the State Machine diagram is shown in Figure below. I am trying to understanding it. There are five states as follows. Button can be pressed or released or held continuously so these three state should be in state machine. I don't know what the B_UP state indicate in diagram. I need someone's help because I really don't understand how the state changes and what condition cause state change.

View attachment 249329

Update : suggest better name for states I don't think button UP make more sense
Button down = pressed , button up = released.
Why do you count debounces?!, you need to count time when the button is kept pressed. When button is pressed continuously like 100ms(=no button debounces) button press is accepted, if still in debounce state, timer is reset.

Continuous button is when you have pushed the button like 2 seconds continuously. In release state your debounce state will ignore the release debouncing.



1633203709685.png
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I am trying to understand what happens in each state when button leave state and when it remain in same state.

As long as the button is not pressed, the button is in START state and as soon as the button is pressed, the button changes from START state to B_UP state.

When does the button leave the state B_UP and when does it remain in this state
 

Irving

Joined Jan 30, 2016
3,843
When does the button leave the state B_UP and when does it remain in this state
You need to differentiate between the external view of button state and the internal view. This is a good example of an object having an external persona and an internal state. Externally the button is up unless it's down. Internally when the first closure occurs it transforms to the closing state and bounces between that and the up state until it remains in one or the other. If it's final state is 'closing' it moves to the closed state both internally as well as externally. Usually this is determined by time.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Internally when the first closure occurs it transforms to the closing state and bounces between that and the up state until it remains in one or the other. If it's final state is 'closing' it moves to the closed state both internally as well as externally. Usually this is determined by time.
First we decide what the possible state should be for button

Can the possible states be something like :

Ideal
Open
Closed,
Closing 1, Closing 2, Closing 3.... Closing N
Opening 1, Opening 2, Opening 3..Opening N,

Does it make sense for you?
 

Juhahoo

Joined Jun 3, 2019
302
1633211626378.png
There is two loops, press and release debounce loop, and the release decision is wrong polarity in the pic: yes = no (green), else you never release the button. Same goes with the B_UP state machine, else your state changes even without pushing the button. Follow this state machine, does this make more sense to you?:


1633212046019.png
START

Stay in B_UP as long as the BUTTON is UP = YES
If B_UP is not UP, we move to B_PRESS state.
If debounce occur B_PRESS is NO and loop continues. (If button is released in this state BUTTON remains UP)
When debounce stops B_PRESS is accepted, BUTTON state is changed to DOWN.

Stay in B_DOWN as long as the BUTTON is DOWN = YES
If B_DOWN is NO button is no longer pressed.
If debounce occur B_RELEASE is NO and loop continues (if button remains pressed in this state BUTTON remains DOWN)
When debounce stops B_RELEASE is accepted, button state is changed to UP.
Jump to START

The same with another type of flow chart:

1633213863958.png
 
Last edited:

Irving

Joined Jan 30, 2016
3,843
Does it make sense for you?
No, you're over-thinking the problem. You should always use the minimum number of states. If you have multiple states of the same type you should implement as a single state with some counter or other means of differentiator.

Juhahoo shows the right approach.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
No, you're over-thinking the problem. You should always use the minimum number of states. If you have multiple states of the same type you should implement as a single state with some counter or other means of differentiator.
My attempt to show the possible states for button debouncing looks like

Button FSM1.png
.
Periodically check opening and closing of button for N times to get stable state
anyway as you suggested this approach is not good. I would try with different approach
 

Irving

Joined Jan 30, 2016
3,843
You can't use a count of N because you don't know how many times the contacts bounce nor the refresh rate of the loop. They could bounce anything from 0 to 30 or more times. It's more ususl to set a timer going on the first closure and reset it on subsequent bounces before setting the final resting state on expiry of the timer. Typically the timer is set for 20 - 50mS.
 

trebla

Joined Jun 29, 2019
542
Periodically check opening and closing of button for N times to get stable state
anyway as you suggested this approach is not good. I would try with different approach
With this approach you will detect lot of false button presses. The idea of debouncing is NOT to check button input during debouncing time. The real button and button variable states in your software are not same always.
Possible interactions between physical button state and button variable state are:

Button variable state :
1.IDLE - button press not detected, no active debounce process running, physical button state checked periodically
2.DETECTED - button press detected, physical button pressed
3.DEBOUNCE - if debounce timer running, physical button state unknown or if debounce timer ended running, physical button is in pressed state

If you combine variable states with the physical button states, you get system states similar to presented in your post #1
 

Juhahoo

Joined Jun 3, 2019
302
Since button has only two states, we can use toggle function:

Button state change is detected, timer is set. If button state remains, timer goes up, if button debounces timer is reset.
If timer reach the value of nnn button state is toggled.
Loop.

1633249991042.png
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I need to understand state transition. I have total five states for button. Button remain in the ideal state until is pressed. When button is pressed system will goes from ideal state to Button pressed state. System stay in button pressed state when when button is pressed initially. What happens in Button pressed state. When does system leave this state. What will be next state after Button pressed state

Button FSM2.png
 

click_here

Joined Sep 22, 2020
548
In my experience the button only bounces on closing.

I usually have these states when implementing a state machine and a push button:
waitForPress, processButtonPress, waitBounceTime, waitForRelease

Note that I process the button press before waiting for bouncing to finish.

Start by making an enum with those states.

Then make a loop by using while(1), or my favourite for( ; ; )

Put a switch statement using the enum as the input and each case as a state.

Fill in each case with logic to move from one state to the other.

Does that make sense?
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Start by making an enum with those states.
I have good understanding of enum and switch case statement in C programming language. first main focus is to make state machine for button when it is ready then I can start for coding. My state machine is not ready as you can see I am having trouble understanding state transactions.
 

Irving

Joined Jan 30, 2016
3,843
My state machine is not ready as you can see I am having trouble understanding state transactions.
Maybe button debouncing wasn't a good first candidate given some of its intermediate transitions are dynamically variable rather than simple Yes/No.
 

trebla

Joined Jun 29, 2019
542
When button is pressed system will goes from ideal state to Button pressed state. System stay in button pressed state when when button is pressed initially. What happens in Button pressed state. When does system leave this state. What will be next state after Button pressed state
Button pressed state is needed for generating signal flag for other software routines in system. After generating the signal flag and starting debounce timer this sate is leaved for avoiding multiple false button press signals generating. Next state in your diagram will be Button pressing state where you wait for debounce counter end signal. After that you can check the physical button state again, if it is released the next state according your diagram will be Button released. You can use this state for sending signal flag and starting debounce counter. New state will be Button releasing. If debounce counter finishes then the physical button state is checked again, if button is still released then next state will be Idle (Ideal in your diagram), if not it goes to Button pressed state again and generates next button press signal.

As other peole noted here this task can be made with less states.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I have studied more than 200 diagrams since last night but till now I don't fully understand the state machine for button. The main reason for this is that all the diagrams are different from each other. Can anyone help me to find a good state machine diagram for a button that fully explains the debounce logic for button.
 
Top