New library: arduino-fsm

I've created a new arduino library called arduino-fsm for implementing a finite state machine1. It's similar to this library2; however, the user triggers a transition by sending an event, allowing the client to remove the need for complex switch statements in their code. This is really an implementation of the State design pattern, using callbacks instead of inheritence.

The code is available on github: https://github.com/jonblack/arduino-fsm

See the example code for a guide on using it. I'll follow up with a blog post soon. It's worth noting the following callbacks:

  • on_enter (when a state is entered)
  • on_exit (when a state is exited)
  • on_transition (when transitioning from one state to another)

Each callback is optional.

Questions? Post here. Clear bugs? See the github issue tracker.

[1] Finite-state machine - Wikipedia
[2] Arduino Playground - FiniteStateMachine Library

Thanks for sharing, bookmarked!

May you add some more examples, please (because it's a little bit tricky for beginners like me)? :~

Greetz Chris

Nowhere i can find the documentation of timeInCurrentState().

Please help! :astonished:

Greetz Chris

Nowhere i can still find the documentation of timeInCurrentState().

Please help! :astonished:

Greetz Chris

Check Jon's page here for an example:

http://www.jonblack.me/arduino-multitasking-using-finite-state-machines/

and this very basic example illustrates the library

http://www.jonblack.me/arduino-finite-state-machine-library/

There is an unwanted remnant in the version 2.1 of the library... a serial.print command in the fsm.cpp file.

For my install, this is the directory the fsm library lands into:

C:\Users\ <<me!!>> \Documents\Arduino\libraries\arduino-fsm

Find the fsm.cpp file in your installed library directory and comment the serial.print out, as seen below:

void Fsm::check_timer()
{
  for (int i = 0; i < m_num_timed_transitions; ++i)
  {
    TimedTransition* transition = &m_timed_transitions[i];
    if (transition->transition.state_from == m_current_state)
    {
      if (transition->start == 0)
      {
        transition->start = millis();
      }
      else
      {
        unsigned long now = millis();
        // **** commented this out: Serial.println(now);
        if (now - transition->start >= transition->interval)
        {
          m_current_state = transition->transition.make_transition();
          transition->start = 0;
        }
      }
    }
  }
}