I implemented a super simple 2-state FSM based on gen_fsm. You can press this light between on and off.
light_fsm.erl:
module(light_fsm). -hehavior(gen_fsm). -export([start/1, press_light/1]). -export([init/1, on/2, off/2]). -record(state, {name=""}). start(Name) -> gen_fsm:start(?MODULE, [Name], []). press_light(Pid) -> gen_fsm:send_event(Pid, press). init(Name) -> io:format("~p in init state: off.~n", [Name]), {ok, off, #state{name=Name}}. off(press, S=#state{}) -> io:format("~p rec msg press, turned on.~n", [S]), {next_state, on, S#state{}}. on(press, S=#state{}) -> io:format("~p rec msg press, turned off.~n", [S]), {next_state, off, S#state{}}. Use this FSM:
Eshell V5.10.2 (abort with ^G) 1> c(light_fsm). {ok,light_fsm} 2> {ok, Light1} = light_fsm:start("Light1"). ["Light1"] in init state: off. {ok,<0.39.0>} 3> light_fsm:press_light(Light1). {state,["Light1"]} rec msg press, turned on. ok 4> light_fsm:press_light(Light1). {state,["Light1"]} rec msg press, turned off. ok