SPLat Logo

Finite State Machines - Make light work of complex functions

SPLat will be shutdown for the Christmas Holiday season from Thursday 20 December 2018 thru Sunday 6 January 2019. We will check emails periodically and will continue to process orders but at a slower pace. Santa hat

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 - Full program listing

Here is the full program source code in a form that you can copy and paste straight into VB.NET

Public Class Form1
    Dim State As Integer        'Global state number
    Dim TimerCount As Integer   'Counts down timer ticks

    '****************************************************
    '********** Event handlers **************************
    '****************************************************
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Various bits of initialization code go in the form_load event
        SetState(0)
        Timer1.Interval = 100   'mS
        Timer1.Enabled = True
        Label1.Text = "Heater"
        Label3.Text = "°C"
        OvalShape1.BackStyle = PowerPacks.BackStyle.Transparent
        OvalShape1.BackColor = Color.Red
        Button1.Text = "Alarm reset"
        Button1.AutoSize = True
    End Sub

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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

    Private Sub TimeOut()
        Select Case State
            Case 1
                SetState(2)
        End Select
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Alarm reset button
        Select Case State
            Case 2
                SetState(0)
        End Select
    End Sub

    '****************************************************
    '********** Outputs  ********************************
    '****************************************************
    Private Sub HeaterOut(ByVal Output As Boolean)
        'Simulate the heater with a colored label
        Select Case Output
            Case True
                Label1.BackColor = Color.Red
            Case False
                Label1.BackColor = Color.Blue
        End Select
    End Sub

    Private Sub AlarmOut(ByVal Output As Boolean)
        'Simulate the alarm output with a colored label
        Select Case Output
            Case True
                OvalShape1.BackStyle = PowerPacks.BackStyle.Opaque
            Case False
                OvalShape1.BackStyle = PowerPacks.BackStyle.Transparent
        End Select
    End Sub

    '****************************************************
    '********** State transitions ***********************
    '****************************************************
    Private Sub SetState(ByVal NewState As Integer)
        'Switch to a nominated state, doing all the entry action stuff
        State = NewState     'Update the global state number
        Label2.Text = "State = " & State 'Diagnostic display
        Select Case State   'Actions depend on the state we are entering
            Case 0
                HeaterOut(False)
                AlarmOut(False)
            Case 1
                HeaterOut(True)
                StartTiming(60)
            Case 2
                HeaterOut(False)
                AlarmOut(True)
        End Select
    End Sub

    '****************************************************
    '********** Timer management ************************
    '****************************************************
    Private Sub StartTiming(ByVal Duration As Integer)
        TimerCount = Duration   'Just set the global counter value
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Time out timer. Uses a 100mS tick, counts down an integer interval
        If TimerCount > 0 Then
            TimerCount = TimerCount - 1
            Label4.Text = "Timer = " & TimerCount
            If TimerCount <= 0 Then ' <= is defensive ... just = would do
                TimeOut()
            End If
        End If
    End Sub
End Class