Stopwatch for Liberty BASIC 4!

Originally published in NL #116

by Bill Beasley - BillBeasley BillBeasley, Janet Terra - JanetTerra JanetTerra, Stefan Pendl - StPendl StPendl and Gordon Sweet - GordonSweet GordonSweet

Stopwatch for Liberty BASIC 4! | Community | Liberty BASIC 4 | The Code

Community


This demo was constructed on the public forum, with input from several members. The spirit of sharing in the community benefits everyone! The demo also shows how the creation of structured code that is self-documented makes it easy for others to read, understand, and modify a program.

Liberty BASIC 4


We'll be seeing a lot more code that is specific to LB 4 now that it has been released officially. This program makes good use of the ability to declare global variables, and the ability to give names to drawing segments when FLUSHing them.

Thanks to all who participated for giving their permission to include this demo in the issue.

The Code

 
 nomainwin
    '----- Set Up GUI -----
    button #w.start, " START ", [start], ul, 100, 50
    button #w.reset, " RESET ", [reset], ul, 100, 100
    button #w.stop, " STOP ", [stop], ul, 620, 50
    button #w.split, " SPLIT", [split], ul, 620, 100
 
 
    WindowWidth = 800 : WindowHeight = 600
    UpperLeftX = int((DisplayWidth-WindowWidth)/2)
    UpperLeftY = int((DisplayHeight-WindowHeight)/2)
 
 
    graphicbox #w.gb 0, 0, WindowWidth-8 ,WindowHeight-27
    open "Stop Watch" for window_nf as #w
    #w "trapclose [quit]"
    #w "resizehandler [Resized]" ' redraw if window was minimized
    #w.gb "font arial 14 bold"
    #w.gb "down; fill 0 0 120"
    #w.start "!setfocus"
 
 
    '----- Display Timer Face -----
    #w.gb "home; size 2; circlefilled 200"
    #w.gb "color red; home; size 3; circle 195"
    #w.gb "home; size 2; north; go 194"
        For Angle = 0 to 359 step 6
           #w.gb "home; turn 6; go 194"
        Next Angle
    #w.gb "color white; home; circlefilled 170"
    #w.gb "backcolor 0 0 120"
    #w.gb "size 3; place 390 75 "
    #w.gb "\0"
    #w.gb "place 605 290 "
    #w.gb "\15"
    #w.gb "place 385 505 "
    #w.gb "\30"
    #w.gb "place 170 290 "
    #w.gb "\45"
    #w.gb "home; size 2; color black; circlefilled 10"
    #w.gb "flush ElapseTimerBody"
    #w.gb "home; north; go 170"
 
 
    Global ElapsedSeconds, ElapsedMinutes, IsRunning
 
 
    ElapsedSeconds = 0
    ElapsedMinutes = 0
    IsRunning = 0
    First = 1
    Angle = 0
 
 
    TIMER 1000, [TimerEvent]
 
 
[TimerEvent]
    GoSub [DisplayCurrentTime]
    if IsRunning = 1 or First = 1 then
        GoSub [DisplayElapsedTime]
        GoSub [DisplaySweep]
        First = 0
    end if
    Scan
    WAIT
 
 
[quit]
    TIMER 0
    CLOSE #w
end
 
 
[Resized]
    #w.gb "redraw ElapseTimerBody"
    goto [TimerEvent]
 
 
[DisplaySweep]
    Angle = (ElapsedSeconds -1) * 6
    #w.gb "backcolor white; home; circlefilled 170"
    #w.gb "color black; home; north; turn "; Angle
    #w.gb "go 170; home; backcolor 0 0 120; circlefilled 10"
    Return
 
 
[DisplayElapsedTime]
    #w.gb "color white; place 240 40"
    #w.gb "\Elapsed Time: "; ElapsedMinutes;"  Minutes ";_
        format$(ElapsedSeconds); " Seconds"
    ElapsedSeconds = ElapsedSeconds + 1
    If ElapsedSeconds = 60 then
       ElapsedMinutes = ElapsedMinutes + 1
       ElapsedSeconds = 0
    End If
    Return
 
 
[DisplayCurrentTime]
    #w.gb "color white; place 215 543"
    #w.gb "\Current Time: "; Time$(); "   Date: "; Date$()
    Return
 
 
[start]
    #w.stop "!setfocus"
    TIMER 1000, [TimerEvent]
    IsRunning = 1
    WAIT
 
 
[reset]
    #w.start "!setfocus"
    ElapsedSeconds = 0
    ElapsedMinutes = 0
    IsRunning = 0
    Angle = 0
    First = 1
    REDIM SplitTime(4,2)
    splitevent = 0
    #w.gb "redraw ElapseTimerBody"
    goto [TimerEvent]
 
 
[split]
    If IsRunning Then
        SplitTime(splitevent, 0) = ElapsedMinutes
        SplitTime(splitevent, 1) = ElapsedSeconds
        splitevent = splitevent + 1
        #w.gb "font arial 12"
        #w.gb "color white bold"
        Select Case splitevent
            Case 1
                #w.gb "place 620 150" : #w.gb "\Split ";_
                splitevent; " -   "; SplitTime(splitevent-1, 0);_
                ":"; format$(SplitTime(splitevent-1, 1))
            Case 2
                #w.gb "place 620 170" : #w.gb "\Split ";_
                splitevent; " -   "; SplitTime(splitevent-1, 0);_
                ":"; format$(SplitTime(splitevent-1, 1))
            Case 3
                #w.gb "place 620 190" : #w.gb "\Split ";_
                splitevent; " -   "; SplitTime(splitevent-1, 0);_
                ":"; format$(SplitTime(splitevent-1, 1))
            Case Else
                #w.gb "place 620 210" : #w.gb "\Split ";_
                splitevent; " -   "; SplitTime(splitevent-1, 0);_
                ":"; format$(SplitTime(splitevent-1, 1))
                splitevent = 0
        End Select
        #w.stop "!setfocus"
        #w.gb "font arial 14 bold"
    End If
    WAIT
 
 
[stop]
    #w.start "!setfocus"
    IsRunning = 0
    WAIT
 
 
Function format$(secs)
    Select Case LEN(STR$(secs))
        Case 1
            format$ = "0" + STR$(secs)
        Case 2
            format$ = STR$(secs)
    End Select
End Function
' --- end of code