Page 1 of 1

simple state machine in Ruby

Posted: Thu May 03, 2012 2:33 pm
by fixstuff555
Here is something that I had been wanting to do, as well as others.

It's a simple state machine in a Ruby code block, that illustrates a couple important concepts and uses the event method. The first and second inputs are external event triggers, and the 101st and 102nd are "internal" triggers. The 2nd set of numbering is irrelevant, but is set aside at a high number so that more external events can be added later.

Basically, to transition from state 1 to state 2, input 101 event is triggered by the timer reaching 2 seconds. The states work through accordingly. This also is a good illustration of using timer's in a Ruby block.

Re: simple state machine in Ruby

Posted: Sat May 19, 2012 5:43 am
by DSP
Or another way is to add the t (time) to the output statement: eg: output 1,true,t+2

Here's you example modified to make a set of traffic lights: