Changing a Parameter using a Pushbutton
' Button_1.Bas
'
' Illustrates an arrangement which might be used to modify a parameter
' by mometarily depressing a push button.
'
' When Mode_Switch on terminal 21 is at a logic one, LED on term 23 flashes
' the number of times specified in NumFlashes.
'
' When the Mode_Switch is at logic zero, the routine continually loops,
' looking for a depression of Button0. If there was a depression,
' NumFlashes is incremented. However, after 10, it rolls over to one.
'
' The implementation of GetButton is to monitor the specified pin for
' the number of times specified. If, at any time, the pin is read as a
' logic one, a logic one is returned. However, if the pin is at a logic
' zero on all of the samples, a zero is returned when the pin returns to
' a logic one.
'
' Illustrates Exit For.
'
' Compile with SerialPort.Bas
'
' Copyright, Peter H. Anderson, Baltimore, MD, Oct, '99
Const Mode_Switch as byte = 21 ' 1 for flash
' 0 for adjust
Const Button0 as Byte = 22
Const LED as Byte = 23
Sub Main()
Dim NumFlashes as Integer
Dim ButtonState as Byte
Dim Y as Byte
Dim str as String
NumFlashes = 1
Call OpenSerialPort(2, 9600)
Call PutPin(Mode_Switch, 3) ' Make inputs
Call PutPin(Button0, 3)
Call PutPin (LED, 1) ' turn off LED
Do
Call PutPin(Mode_Switch, 3) ' configure as inputs
Call PutPin(Button0, 3)
Y = GetPin(Mode_Switch) ' if at one, then flash LED
If (Y = 1) then
Call Flash(LED, NumFlashes)
str = "..." ' used for debugging
Call PutStr(str)
Call PutI(NumFlashes)
Call NewLine()
Else ' otherwise, get button
ButtonState = GetButton(Button0, 100)
If (ButtonState = 0) then
NumFlashes = NumFlashes + 1
If (NumFlashes > 10) then
NumFlashes = 1
End if
Call PutI(NumFlashes)
End if
End if
Loop
End Sub
Sub Flash(ByVal Pin as Byte, ByVal NumFlashes as Integer)
Dim N as Integer
For N = 1 To NumFlashes
Call PutPin(Pin, 0)
Call Sleep(0.1)
Call PutPin(Pin, 1)
Call Sleep(0.1)
Next
Call Sleep(2.0)
End Sub
Function GetButton(ByVal Pin as Byte, ByVal Samples as Integer) as Byte
Dim N as Integer
Dim ReturnVal as Byte
Dim Y as Byte
Dim str as String
ReturnVal = 0
For N = 1 TO Samples
Call PutPin(Pin, 3)
Y = GetPin(Pin)
If (Y = 1) then
ReturnVal = 1 ' Exit the For Next with return val = 1
Exit For
End if
Next
Do ' wait for button to return to logic one
Call PutPin(Pin, 3)
Y = GetPin(Pin)
If (Y=1) Then
Goto GetButtonDone
End if
Loop
GetButtonDone:
str ="'''" ' used for debugging
Call PutStr(str)
GetButton = ReturnVal
End Function