(Last modified Tue May 27 15:30 2008)
Figure 1 shows a statechart describing one direction of a stoplight that has corresponding walk lights and no turn signals. The length of time it is green is controlled by an external signal, as is the length of time the "walk" light is on while the stoplight is red (and thus the total time it is red). All of the actions are on the transitions.
The external signals are "turnYellow" and "flash". All other transitions are triggered after specific amounts of time: Yellow to Red after long enough for cars approaching the intersection to stop, and Walk to Flashing after long enough to ensure pedestrians can cross completely.
Figure 2 shows a corresponding statechart whose actions are on the transitions.
Figure 1. Stoplight, actions on transitions
Figure 2. Stoplight, (most) actions in states
The next step is to validate the statecharts, by writing up some stoplight scenarios and walking through the statechart to make sure they are possible (if they are positive scenarios) or not possible (if they are negative scenarios describing what should not happen); and analyzing the statechart (manually or with one of the available tools) to gain appropriate confidence that the statechart describes a stoplight doing everything it should and not doing anything it shouldn't.
Statecharts for two directions (North-South, and East-West) could be combined too, with each direction's Green and Yellow running concurrently with the other direction's Walk, Flashing, and Don't Walk; the Yellow and Don't Walk state transitions would need to synchronize so that they end at the same time.