Use of FirstTime and Persistent Variables

' FirstTime.Bas
' If program is run the first time after download, two counters (long)
' are set to zero.  On each boot thereafter the BootCounter is 
' incremented.
' Each time a pushbutton on Pin21 is set to zero, ButtonCounter is 
' incremented.
' Note that both counters are implemented as persistent variables
' and are thus, not affected by loss of power.
' This type of program might be used to determine the number of times
' a unit is reset and the number of times an action is performed. 
' Note that persistent variables may only be used in one module and must
' first be used in the same order as they are declared.
' Illustrates the use of FirstTime, Persistent variables and Do - 
' Loop Until.
' copyright, Peter H. Anderson, Baltimore, MD, Oct, '99

Dim BootCounter as New PersistentLong
Dim ButtonCounter as New PersistentLong

Sub Main()

   Dim str as String
   If (FirstTime()) then
      BootCounter = 0
      ButtonCounter = 0
   End If
      BootCounter = BootCounter + 1
      ButtonCounter = ButtonCounter + 0

   Call PutPin(21, 3)		' make button as input

   Call OpenSerialPort(2, 9600)
   str = "BootCounter="
   Call PutStr(str)
   Call PutL(BootCounter)
   Call NewLine()  

      If (Button_Depressed(21)= 1) Then
         ButtonCounter = ButtonCounter + 1
         str = "ButtonCounter="
         Call PutStr(str)
         Call PutL(ButtonCounter)
         Call NewLine()
      End If
End Sub

Function Button_Depressed(ByVal Pin as Byte) as Byte

   Dim RetVal as Byte
   Dim Y as Byte
   Call PutPin(Pin, 3)
   If (GetPin(Pin) = 1) then
      RetVal = 0
      RetVal = 1	' true
   End if

      Call PutPin(Pin, 3)	' now, wait for it to go back to a one
      Y = GetPin(Pin)
   Loop Until (Y=1)

   Call Sleep(0.1)
   Button_Depressed = RetVal

End Function