start() -> register(turnstile, spawn(?MODULE, init, )).
% Initial state
init() -> locked().
coin() -> turnstile ! coin.
pass() -> turnstile ! pass.
% States and transitions
alarm() -> io:format("You shall not pass!~n").
unlock() -> io:format("Unlocking...~n").
lock() -> io:format("Locking...~n").
thankyou() -> io:format("Thank you for donation~n").
The idea behind this code is simple. Every state is implemented as a function that does two things: it listens for messages sent by other processes; when message is received the appropriate action is taken and one of the state-functions called recursively. Simple, right? And thread-safe!