A robust, flexible state machine implementation for Arduino and embedded systems that supports event-driven transitions, timed states, and hierarchical state management.
- Template-based configuration - Set maximum states and transitions at compile time
- Multiple transition types - Event-driven, timed, and conditional transitions
- State lifecycle hooks - Entry and exit callbacks for states
- Event system - Handle external events with optional data payload
- Timeouts - Automatic state transitions after specified durations
- Debug support - Optional verbose logging via Serial or other Print-compatible output
- Memory efficient - No dynamic allocation, fixed memory footprint
- Download the latest release from GitHub
- Install via Arduino Library Manager (search for "AutoStateMachine")
- Or manually install by placing in your Arduino/libraries folder
- Template parameters:
MAX_STATES
: Maximum number of states (default: 10)MAX_TRANSITIONS
: Maximum number of transitions (default: 20)
bool addState(uint8_t id, StateFunction func, TransitionCallback onEnter = nullptr, TransitionCallback onExit = nullptr)
- Adds a new state to the state machine
- Parameters:
id
: Unique state identifierfunc
: Function called repeatedly while in this stateonEnter
: Optional callback when entering stateonExit
: Optional callback when exiting state
- Returns: true if state was added successfully
- Sets the initial state for the state machine
- Must be called before running the state machine
bool addTransition(uint8_t fromState, uint8_t eventId, uint8_t toState, TransitionCondition condition = nullptr)
- Adds an event-triggered transition between states
- Parameters:
fromState
: Source state IDeventId
: Event identifier that triggers this transitiontoState
: Destination state IDcondition
: Optional condition function that must return true for transition to occur
- Returns: true if transition was added successfully
- Configures a timeout transition for a state
- Parameters:
stateId
: State to apply timeout totimeoutMs
: Duration in milliseconds before automatic transitiontimeoutState
: State to transition to after timeout
- Processes an event through the state machine
- Parameters:
eventId
: Event identifiereventData
: Optional pointer to event-specific data
- Returns: true if event triggered a state transition
- Executes the current state's function
- Should be called frequently in main loop
- Handles timeouts and executes current state
- Combines timeout checking and state execution
- Returns the state machine to its initial state
- Calls exit/enter callbacks as appropriate
- Enables debug output
- Parameters:
debugStream
: Print-compatible output stream (e.g., Serial)
- Disables debug output
- Returns: Current state ID
- Checks if a state exists
- Returns: true if state exists
- Create state machine instance with desired template parameters
- Add all states with their functions and optional callbacks
- Configure transitions (event-based and timeout-based)
- Set initial state
- In main loop:
- Call
handleEvent()
for any events - Call
update()
to handle timeouts and run current state
- Call
- Keep state functions short and non-blocking
- Use event data pointers carefully (ensure proper lifetime)
- For complex conditions, use separate condition functions
- Enable debug during development
- Consider state timeouts for fault recovery
- Document your state/event IDs with enums
See the examples/
folder for complete usage examples:
Basic/
- Simple LED blinkerIntermediate/
- Traffic light controllerAdvanced/
- Thermostat with temperature control
- Fixed maximum states/transitions (set at compile time)
- No hierarchical state support
- No built-in serialization for state persistence
MIT License - Free for personal and commercial use