Netduino VB button tutorial and Event Handlers

Programming a button press with VB and your @Netduino, Event handlers and interrupt ports. In this tutorial I show how to program the Netduino to light up the onboard LED when the button is pressed, I then go into event handlers and latching buttons.

image

 

Code for the simple button press.

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino

Module Module1
    Sub Main()
        Dim led As OutputPort = New OutputPort(Pins.ONBOARD_LED, False)
        Dim button As InputPort = New InputPort(Pins.ONBOARD_SW1, False, Port.ResistorMode.PullUp)

        Do While True
            led.Write(button.Read())
        Loop

    End Sub
End Module

Code for the Event handler latching button.
Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino

Module Module1
    Public btnMode As Boolean = False
    Dim led As OutputPort = New OutputPort(Pins.ONBOARD_LED, False)
    Sub Main()

        Dim button As InterruptPort = New InterruptPort(Pins.ONBOARD_SW1, False, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow)
        AddHandler button.OnInterrupt, AddressOf ButtonChange


        Do
            Thread.Sleep(Timeout.Infinite)
        Loop

    End Sub

    Private Sub ButtonChange(data1 As UInteger, data2 As UInteger, time As Date)
        If btnMode = False Then
            btnMode = True
            led.Write(True)
        Else
            btnMode = False
            led.Write(False)
        End If

    End Sub

End Module

Leave a Comment