'-------------------------------------------------------------------------------
Public Sub Main()			

' This program reads the state of a switch and toggles the state of an
' output pin whenever the switch is pressed.

    Const InputPin As Byte = 16
    Const OutputPin As Byte = 17

    Const ButtonPressed As Byte = 1
    Const ButtonReleased As Byte = 0

    ' Configure pins.
    Call PutPin(InputPin, bxInputTristate)
    Call PutPin(OutputPin, bxOutputHigh)

    Dim State as Byte		

    State = 0

    Debug.Print
    Debug.Print "Button Demonstration"
    Debug.Print

    Debug.Print "State = "; CStr(State)

    Do
        ' Toggle State if button is pressed.
        If (GetPin(InputPin) = ButtonPressed) Then

            State = State Xor 1
	   
            Debug.Print "State = "; CStr(State)

            ' Pause 0.1 s for button de-bounce.
            Call Delay(0.1)

            ' Wait for release.
            Do Until (GetPin(InputPin) = ButtonReleased)
            Loop
        End If
  
        ' Write State to output pin.
        Call PutPin(OutputPin, State)
    Loop	

End Sub
'-------------------------------------------------------------------------------
