/*
AuCP_Debounce.ino
Created: 5-May-2023
Author: MicroBeaut
GitHub: https://github.com/MicroBeaut/Finite-State#debounce
*/
#include "FiniteState.h"
#define buttonPin A0 // Define the Button input pin.
#define ledPin 7 // Define the LED output pin.
/*
__________________________________________________________________________________________________________________________________________________
| State-Transition Table |
|_________________________________________________________________________________________________________________________________________________|
| | | | Next-State | Next-State | | | Delay-Time | |
| State | Id | Predicate | Fase | True | Process | Event | (milliseconds) | Timer-Type |
|_____________|_______|_________________|_____________|_____________|_________________|_______________________|_________________|_________________|
| RELEASED | 0 | ButtonPredicate | 0 | 1 | ReleasedProcess | - | - | - |
| DEBOUNCE_T | 1 | ButtonPredicate | 0 | 2 | - | - | 10 | TRUE_TIMER |
| PRESSED | 2 | ButtonPredicate | 3 | 2 | PressedProcess | - | - | - |
| DEBOUNCE_F | 3 | ButtonPredicate | 0 | 2 | - | - | 10 | FALSE_TIMER |
|_____________|_______|_________________|_____________|_____________|_________________|_______________________|_________________|_________________|
*/
bool ButtonPredicate(id_t id); // Declare Read Button Predicate function
void ReleasedProcess(id_t id); // Declare Released Process function
void PressedProcess(id_t id); // Declare Pressed Process function
enum DebounceState : id_t {
RELEASED,
DEBOUNCE_T,
PRESSED,
DEBOUNCE_F
};
#define debounce 10 // Debounce Delay 10 milliseconds
Transition transitions[] = {
{ButtonPredicate, RELEASED, DEBOUNCE_T, ReleasedProcess}, // State-0 - NextF = 0, NextT = 1
{ButtonPredicate, RELEASED, PRESSED, nullptr, nullptr, debounce, TRUE_TIMER}, // State-1 - NextF = 0, NextT = 2
{ButtonPredicate, DEBOUNCE_F, PRESSED, PressedProcess}, // State-2 - NextF = 3, NextT = 2
{ButtonPredicate, RELEASED, PRESSED, nullptr, nullptr, debounce, FALSE_TIMER} // State-3 - NextF = 0, NextT = 2
};
const uint8_t numberOfTransitions = sizeof(transitions) / sizeof(Transition); // Calculate the number of transitions.
FiniteState debounceFS(transitions, numberOfTransitions); // Finite-State Object
bool buttonState;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the Button input mode
pinMode(ledPin, OUTPUT); // Set the LED output pin mode
debounceFS.begin(RELEASED); // FSM begins with Initial Transition Id 0
}
void loop() {
debounceFS.execute(); // Execute the FSM
digitalWrite(ledPin, buttonState); // Set LED with the button State.
}
bool ButtonPredicate(id_t id) {
return !digitalRead(buttonPin); // Read Button value.
}
void ReleasedProcess(id_t id) {
buttonState = false; // Set the Button state with false value.
}
void PressedProcess(id_t id) {
buttonState = true; // Set the Button state with true value.
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
bounce:Input
bounce:Output
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
led1:A
led1:C
r9:1
r9:2