You are on page 1of 6

ARDUINO FINITE STATE MACHINE LIBRARY

September 5th, 2015

Introducing arduino-fsm (https://github.com/jonblack/arduino-fsm), a library that


makes it easy to use a finite state machine (https://en.wikipedia.org/wiki/Finite-
state_machine) in an Arduino project.

DESIGN

This library is composed of two classes: Fsm and State :

State Represents a state in the state machine. A state has two callback functions
associated with it: on_enter and on_exit , which are called when the state
is entered into and exited from, respectively.

Fsm Represents the state machine. Transitions are added using the
add_transition function. This function takes pointers to two states, an
event id, and a callback to a function that's called when the transition takes
place. Calling trigger with the event id invokes the transition, but only if
(https://www.facebook.com/sharer/sharer.php?
the Fsm is in the start state, otherwise nothing happens.
u=http://www.humblecoder.com/arduino-

finite-
(https://plus.google.com/share?
state-
url=http://www.humblecoder.com/arduino-
TUTORIAL
machine-
finite-
(https://twitter.com/home?
library/&t=Arduino
state-
status=http://www.humblecoder.com/arduino-

finite
machine-
finite-
First create the Fsm and State instances at the top of your source file.
(https://pinterest.com/pin/create%2Fbutton/?
state
library/)
state-
url=http://www.humblecoder.com/arduino-
machine

machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
library/)
state- #include<Fsm.h>
mini=true&url=http://www.humblecoder.com/arduino-
machine-
finite-
library/&description=arduino-
state-#defineFLIP_LIGHT_SWITCH1
fsm
machine-
in anFsmfsm;
library/&title=Arduino
Arduino
finiteStatestate_light_on(on_light_on_enter,&on_light_on_exit);
library
state
for Statestate_light_off(on_light_off_enter,&on_light_off_exit);
machine
incorporating
library&summary=arduino-
fsma In your Setup() function, add the state transitions.
finite
in an
state
Arduino
machine voidsetup()
library
in {
for
your
incorporating
fsm.add_transition(&state_light_on,&state_light_off,
Arduino
a
finiteFLIP_LIGHT_SWITCH,
project.&media=http://www.humblecoder.com/content/images/2015/09/banner.jpg)
state&on_trans_light_on_light_off);
machine fsm.add_transition(&state_light_off,&state_light_on,
in
yourFLIP_LIGHT_SWITCH,
Arduino &on_trans_light_off_light_on);
project.&source=)
}

In this case, two transitions are added:

1. From the lighton state to the lightoff state when the event
FLIP_LIGHT_SWITCH is triggered;
2. From the lightoff state to the lighton state when the event
FLIP_LIGHT_SWITCH is triggered.

Define your callback functions. If a callback isn't required, just pass NULL to the
function instead.


(https://www.facebook.com/sharer/sharer.php?
u=http://www.humblecoder.com/arduino-

finite-
(https://plus.google.com/share?
state-
url=http://www.humblecoder.com/arduino-

machine-
finite-
(https://twitter.com/home?
library/&t=Arduino
state-
status=http://www.humblecoder.com/arduino-

finite
machine-
finite-
(https://pinterest.com/pin/create%2Fbutton/?
state
library/)
state-
url=http://www.humblecoder.com/arduino-
machine

machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
library/)
state- voidon_light_on_enter()
mini=true&url=http://www.humblecoder.com/arduino-
machine-
finite-{
library/&description=arduino-
state-//EnteringLIGHT_ONstate
fsm
machine-
in an}
library/&title=Arduino
Arduino
finite
library
statevoidon_light_on_exit()
for
machine
incorporating
{
library&summary=arduino-
a
fsm //ExitingLIGHT_ONstate
finite
in an
state}
Arduino
machine
library
in
for voidon_light_off_enter()
your
incorporating
Arduino{
a
project.&media=http://www.humblecoder.com/content/images/2015/09/banner.jpg)
finite//EnteringLIGHT_OFFstate
state}
machine
in
yourvoidon_light_off_exit()
Arduino{
project.&source=)
//ExitingLIGHT_OFFstate
}

voidon_trans_light_on_light_off()
{
//TransitionedfromLIGHT_ONtoLIGHT_OFF
}

voidon_trans_light_off_light_on()
{
//TransitionedfromLIGHT_OFFtoLIGHT_ON
}

And finally, trigger a state transition.

fsm.trigger(FLIP_LIGHT_SWITCH);//TriggertheeventFLIP_LIGHT_SWITCH

(https://www.facebook.com/sharer/sharer.php?
u=http://www.humblecoder.com/arduino-
The full source code for this example (https://github.com/jonblack/arduino-
finite-
(https://plus.google.com/share?
fsm/blob/master/examples/light_switch/light_switch.ino) can be found in the Github
state-
url=http://www.humblecoder.com/arduino-

machine-
repository.
finite-
(https://twitter.com/home?
library/&t=Arduino
state-
status=http://www.humblecoder.com/arduino-

finite
machine-
finite-
(https://pinterest.com/pin/create%2Fbutton/?
state
library/)
state-
url=http://www.humblecoder.com/arduino-
machine

machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
ARDUINO (/TAG/ARDUINO/)
library/)
state-
mini=true&url=http://www.humblecoder.com/arduino-
machine-
finite-
library/&description=arduino-
state-
fsm
machine-
in an
library/&title=Arduino
Arduino
finite
library
state 11Comments humblecoder 1 Login
for
machine
incorporating
library&summary=arduino-
a
Recommend Share SortbyBest
fsm
finite
in an
state
Arduino Jointhediscussion
machine
library
in
for
your MikeWalsh9monthsago
incorporating
Arduino
a VisualStudio2015AtmelVisualMicropluginhasanexampleofaFSMlibrarybutIam
project.&media=http://www.humblecoder.com/content/images/2015/09/banner.jpg)
finite notsureifitisthesameasyourFSMlibrary.Anythoughtsoncomparisonoristhat
state FSM_1.6justanearlierlesscapableversion?Thanks
machine Reply Share
in
your hmblcodr Mod >MikeWalsh9monthsago
Arduino Therehasneverbeena1.6releaseof`arduinofsm`soI'dguessit'snotthe
project.&source=) same.Idon'thaveWindowssoIcan'tcheckforyouI'mafraid.Lookingatthe
pluginitdoesallowinstallinglibrariesinthesamewayaswiththeArduinoIDE,so
youcouldchecktheretoseeifit'sinstalledornot.
Reply Share

Dennis10monthsago
Hi.IamcreatingaclockradiousingFSM.Iwanttoswitchmodesbasedonakeypress.
ThemodesareDisplay>EditHours>EditMinutesDisplay.Switchingworksfine.
InitializingthedisplaywhenIenterEditHoursmodeisalsostraightforward.ButIam
strugglingtofindawaytoupdatethedisplaybasedonthemode.InEdithoursmodeI
wantthehourstochangewhenIturntheRotaryencoder.WheninEditMinutesmodeI
wanttheminutestochange.Thedoesntseemtobeawaytoaskthemachineforthe
currentmode.Isthereanotherwaytodothis?
Cheers
Reply Share

hmblcodr Mod >Dennis 10monthsago

Thanksforgettingintouch.There'sapullrequestthatwillhelpyououthere.It
addsanon_statecallbackforeachstatewhichisrepeatedlycalledwhilethe
machineisinthegivenstate.Youwouldbeabletoaddyourrotarycoderhandling
inthere.There'sstillsomediscussiongoingonsoit'syettobemergedinto
(https://www.facebook.com/sharer/sharer.php?
master,butyou'rewelcometotryoutthecodeinthePR.
u=http://www.humblecoder.com/arduino-
Reply Share
finite-
(https://plus.google.com/share?
state-
url=http://www.humblecoder.com/arduino-

machine-
Dennis>hmblcodr10monthsago
finite-
(https://twitter.com/home? Thankyouforthetip.I'llcheckitout.
library/&t=Arduino
state-
status=http://www.humblecoder.com/arduino-

finite Reply Share
machine-
finite-
(https://pinterest.com/pin/create%2Fbutton/?
state
library/)
state-
url=http://www.humblecoder.com/arduino-
machine

machine-
AlexHoffmannayearago
finite-
library)
(https://www.linkedin.com/shareArticle?
Hi
machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
library/)
Hi
state-
mini=true&url=http://www.humblecoder.com/arduino-
machine-
finite- IamtryingtouseanFSMtotogglethroughmultiplesensorsandsimplydisplaytheir
library/&description=arduino-
state- valuestoanLCD.Mybasicstructurewasgoingtobeusingtwotriggers,onethatmore
fsm
machine- orlessconstantlyrunstorefresheachstate,thusupdatingthelcdscreen,andonethat
in an
library/&title=Arduino
isusedtotogglebetweenwhichsensor'sinformationisbeingdisplayed.
Arduino
finite
library
state MyproblemisIamusingtwodifferenttriggersandbothtriggerthefirsttransitionfoundfor
for
machine thecurrentstatethusmeaningbothtriggerscallthesametransitionirrelevantofwhich
incorporating triggerwasdefinedwithinthespecifictransition.AmImissingsomethingorwasthis
library&summary=arduino-
fsma librarynotmeanttoworkwithmorethanonetrigger?
finite
in an
state
Arduino Otherwisegreatandsimplelittlefsmimplementation.
machine
library
in Cheers
for
your
incorporating Reply Share
Arduino
a
project.&media=http://www.humblecoder.com/content/images/2015/09/banner.jpg)
finite hmblcodr Mod >AlexHoffmannayearago

state I'mnotsureIunderstandyourproblem.CanyoucreateanissueonGitHuband
machine provideacodeexampleofyourproblem?
in Reply Share
your
Arduino AlexHoffmann>hmblcodrayearago
project.&source=) willdo.thanksforthequickresponse!
Reply Share

IainCDochertyayearago
IwasgoingtowriteaFSMforArduinowhenIcameacrossthis.

Mythoughtwasthatifwewantedto'multitask'severalseparatetasks,e.g.controllinga
servo,monitoringinputs,controllingoutputsetc.theneachofthesetaskswouldbe
writtenasaFSM,thenjustbyloopingthrougheachFSMinturnwewouldachieveasort
ofmultitasking.

Justwonderinghowyourcodewouldbeorganizedtodosomethinglikethis.Anychance
ofasimpleexample?
Reply Share

hmblcodr Mod >IainCDocherty ayearago

JustaheadsuptoletyouknowthatI'veupdatedarduinofsmwithsupportfor
timedtransitions.I'vewrittenapostaboutitandthecodeisalreadyonGitHub.
Reply Share

hmblcodr Mod >IainCDocherty ayearago

(https://www.facebook.com/sharer/sharer.php?
TheFSMlibrarycanusedthatway,butitwon'ttakecareofthetimingforyou.For
u=http://www.humblecoder.com/arduino-
example,ifyouwantaservotospinfor3secondsandduringthesameperiod
finite- haveanLEDblink6times,you'llneedsomeothermethodtokeeptrackofhow
(https://plus.google.com/share?
state-
url=http://www.humblecoder.com/arduino-
muchtimehaspassedandwhenthetransitionshouldhappen.Ifyouweretotry

machine-
finite- andtakecareusingsomethinglikedelay(),theservowouldfirstspinfor3
(https://twitter.com/home?
library/&t=Arduino
state- secondsthenstop,thentheLEDwouldblinksixtimes.
status=http://www.humblecoder.com/arduino-

finite
machine-
finite-
(https://pinterest.com/pin/create%2Fbutton/?
state
library/) PerhapstheFSMlibrarywouldbenefitfromatimedtransitionthatmanagesthe
state-
url=http://www.humblecoder.com/arduino-
machine

machine-
timingautomaticallyandwhenthegiventimeisreached,automaticallytransitions
finite-
library)
(https://www.linkedin.com/shareArticle?
machine-
finite-
library)
(https://www.linkedin.com/shareArticle?
library/) tothenextstate.I'veaddedanissueonGithubandwhenIgettime,I'lllookintoit
state-
mini=true&url=http://www.humblecoder.com/arduino-
abitdeeper.
machine-
finite-
library/&description=arduino-
state- WhenIcreatedthelibraryIhadaneventbasedarchitectureinmind:auser
fsm
machine- providesinput,forexamplebypressingabutton,andthatisusedtotriggera
in an
library/&title=Arduino transitionintheFSM.
Arduino
finite
library
state Doesthatansweryourquestion?
for
machine Reply Share
incorporating
library&summary=arduino-
fsma
finite
in an ALSOONHUMBLECODER
state
Arduino TracktableselectionsinEmber WhichtexteditorshouldIuse?
machine
library 2commentsayearago 1comment2yearsago
in
for hmblcodrThisisstillDDAUbecause EECOLORAndhereIam,thinkingwe
your
incorporating thetemplateforthebooktable havemodelesseditorsforabout35years,
Arduino
a componentpassesbookdowntothe Imustbestupid...
project.&media=http://www.humblecoder.com/content/images/2015/09/banner.jpg)
finite
state Arduinomultitaskingusingfinitestate Pickalanguage,anylanguage
machine machines 9commentsayearago
in 17commentsayearago hmblcodrThanksforyourcomment.I
your hmblcodrThanksforgettingintouch. wasn'tawarePascalwascreated
Arduino Atfirstglanceitlookslikeanicelibrary, specificallyforlearning.Iwonderifit's
project.&source=)
indeedeasyforbeginners.SadlyI

Subscribe d AddDisqustoyoursiteAddDisqusAdd Privacy

Created by Jon Black (https://twitter.com/hmblcodr) using Ghost (http://ghost.org)

humblecoder.com (http://humblecoder.com) | 2015-2016 | Code hard, stay humble


(https://www.facebook.com/sharer/sharer.php?
u=http://www.humblecoder.com/arduino-

finite-
(https://plus.google.com/share?
state-
url=http://www.humblecoder.com/arduino-

machine-
finite-
(https://twitter.com/home?
library/&t=Arduino
state-
status=http://www.humblecoder.com/arduino-

finite
machine-
finite-
(https://pinterest.com/pin/create%2Fbutton/?
state
library/)
state-
url=http://www.humblecoder.com/arduino-
machine

machine-
finite-
library)
(https://www.linkedin.com/shareArticle?

You might also like