| Contents | Index |
| On this page… |
|---|
When a switch opens and closes, the switch contacts can bounce off each other before the switch completely transitions to an on or off state. The bouncing action can produce transient signals that do not represent a true change of state. Therefore, when modeling switch logic, it is important to filter out transient signals using a process called debouncing.
For example, if you model a controller in a Stateflow chart, you do not want your switch logic to overwork the controller by turning it on and off in response to every transient signal it receives. Instead, you can design a Stateflow debouncer that uses temporal logic to determine whether the switch is really on or off.
The model sf_debouncer illustrates a design pattern that uses temporal logic to isolate transient signals.

The Debouncer chart contains the following logic:

The key behaviors of the Debouncer chart are:
In addition to the states On and Off, the Debouncer chart contains an intermediate state called Debounce. The Debounce state isolates transient inputs by checking whether the signals retain their positive or negative values, or fluctuate between zero crossings over a prescribed period of time. The logic works as follows.
| If the input signal... | Then this state... | Transitions to... | And the... |
|---|---|---|---|
Retains positive value for 0.1 second | Debounce.On | On | Switch turns on |
Retains negative value for 0.1 second | Debounce.Off | Off | Switch turns off |
Fluctuates between zero crossings for 0.3 second | Debounce | Off.Fault | Chart isolates the input as a transient signal and gives it time to recover |
The debouncer design pattern uses temporal logic to:
Determine whether the input signal is normal or transient
Give transient signals time to recover and return to normal state
Using Absolute-Time Temporal Logic. The debouncer design uses the after(n, sec) operator to implement absolute-time temporal logic (see Operators for Absolute-Time Temporal Logic). The keyword sec defines simulation time that has elapsed since activation of a state.
Using Event-Based Temporal Logic. As an alternative to absolute-time temporal logic, you can apply event-based temporal logic to determine true state in the Debouncer chart by using the after(n, tick) operator (see Operators for Event-Based Temporal Logic). The keyword tick specifies and implicitly generates a local event when the chart awakens (see Using Implicit Events).
The Error Generator block in the sf_debouncer model generates a pulse signal every 0.001 second. Therefore, to convert the absolute-time temporal logic specified in the Debouncer chart to event-based logic, multiply the n argument by 1000, as follows.
| Absolute Time-Based Logic | Event-Based Logic |
|---|---|
| after ( 0.1, sec ) | after ( 100, tick ) |
| after ( 0.3, sec ) | after ( 300, tick ) |
| after ( 1, sec ) | after ( 1000, tick ) |
To run the sf_debouncer model, follow these steps:
Open the model by clicking sf_debouncer or typing sf_debouncer at the MATLAB command prompt.
Open the Stateflow chart Debouncer and the Scope block.
Simulate the chart.
The scope shows how the debouncer isolates transient signals from the noisy input signal.

Note To debounce the signals using event-based logic, change the Debouncer chart as described in Using Event-Based Temporal Logic and simulate the chart again. You should get the same results. |
![]() | Stateflow Design Patterns | Scheduling Execution of Simulink Subsystems | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |