SPLat Logo

Finite State Machines - Make light work of complex functions

NOTICE: We are updating our website. For product enquiries and orders please contact us directly.
NOTICE: SPLat Controls has moved. We are now at 1/85 Brunel Rd, Seaford, 3198. map

Program your own Finite State Machine using a SPLat Controller for only $29.00.

The EC1 "EasyOne", a 32-bit fully featured SPLat Controller with USB and true multi-tasking is a easy way to learn and a cheap way to build your project.

VB.NET - One event handler in detail

I am not going to bore you with a blow by blow description of every one of the 9 subs. Consider than an exercise - you will learn more by working out some of it for yourself. Instead I will detail just a few selected subs.

The listing below is for the event handler that processes temperature change events (simulated by the NumericUpDown1 form control).

Private Sub NumericUpDown1_ValueChanged(...
'Simulated temperature reading
  Select Case NumericUpDown1.Value
    Case Is < 84
      Select Case State
        Case 0
          SetState(1)
      End Select
    Case Is > 85
      Select Case State
        Case 1
          SetState(0)
      End Select
  End Select
End Sub

The first thing the code does is use a Select Case to categorize the temperature reading (simulated by the Value property of NumericUpDown1) as either <84°C or >85°C. Anything else is somewhere in between, and of no interest.

For each of the selectable cases, it contains an inner Select Case that selects on the state number. As it happens, in this simple state machine each event (Temperature<84°C or Temperature>85°C) is of interest in only one state. I could just as easily, therefore, have use a simple If ... Then. However, part of my programming style is to use the same constructs for the same things as much as reasonably possible, even at the cost of a bit more typing. I find that makes the programs easier to read and maintain.

In the case where the Temperature<84°C, if the state is 0, a call is made to SetState with an argument value of 1. This will switch the state machine to state 1, executing the necessary actions along the way.

Conversely, if the Temperature>85°C, if the state is 1, a call is made to SetState with an argument value of 0.