Creating a Count Up Timer

Objective: Create a count up timer from when a start button is pressed and pause when the button is pressed an additional time. It is to reset to 00:00:00 when a reset button is pressed.

Questions: Should I base this off of the internal system clock or a stand alone system? How would I achieve this?

Work so Far:

GraphicalRefreshTimer (action)

[code] PSIMeterCanvas.Refresh



Note: TotalTimeCanvas is the particular canvas relating to this inquiry.

TotalTimeCanvas (Paint)

[code] Dim s As Integer
Dim m As Integer
Dim h As Integer
Dim today As String
Dim d As Date

d = New Date
s = d.Second
m = d.Minute
h = d.Hour

today = d.LongTime

g.DrawString(today, 0,19)[/code]

Note: Obviously, this constantly displays the time of day based on the system clock. I am just stuck as to what I should base the clock off of and how the timer would be done.

As always, thank you for your help.

I hate being the “New Guy”, but I seem to get so far, then hit blockades.

You need to capture the start time and subtract it from the end time. Maybe have a look at the Xojo.Core.DateInterval class and examples in the docs:
That should be exactly what you need, down to nanoseconds.

That looks like exactly what I need in order to compare two times in order to get a total time.

So how would I utilize this code for counting up? Can a compare the time the button is pressed to the current refreshing system clock, thus giving me a timer that counts up? That would not provide a way to make a pause option if I am not mistaken.

How do I record the time the button was pressed?

Almost exactly like in the docs example:

Dim startdate as Xojo.Core.Date =

And if you do this any time the record button is pressed again, you can get the next dateinterval between two start/stop button pushes and add it to a totaldateinterval of Class Xojo.Core.Dateinterval.

Optionally there’s the$Microseconds or the ticks function that you could use to record the passed microseconds (or 1/60 seconds) between two button pushes and then calculate the time passed. But I think handling the dateinterval is easier for the start.

Example Time CountDown and CountUp

set the time (seconds) in btn.Action

[quote=170501:@Axel Schneider]Example Time CountDown and CountUp

set the time (seconds) in btn.Action[/quote]

Perfect! Now I can see how to format the coding and figure out exactly how to do it myself! Thank you

[quote=170501:@Axel Schneider]Example Time CountDown and CountUp

set the time (seconds) in btn.Action[/quote]

Now pausing the timer. How would I accomplish that? Thank you so much.

I would do exactly the same as you did before, with the addition of a intermediate time that you add to the time interval. The intermediate time is updated every time you hit the pause button.

If you need many split times, you simply manage multiple intermediate times.

make a new Button with Caption ‘Pause’

   if me.Caption = "Pause" then
    CountTimer1.mode = 0
    me.Caption = "Resume"
    CountTimer1.mode = 2
    me.caption = "Pause"
  end if

[code] //Timer Functions

if me.Caption = “Start” then //If caption is “Start” make the timer reset when hit and make it begin counting
myseconds = 0
myduration = 120 // seconds
CountTimer1.mode = 2
me.Caption = “Stop”
elseIf me.Caption = “Stop” then //If caption is “Stop” then make the timer pause
CountTimer1.mode = 0
me.Caption = “Resume”
CountTimer1.mode = 2 //If caption is anything else (Resume) make caption be “Stop” and keep running timer
me.caption = “Stop”
end if[/code]

So like this for a button that counts time, switches roles (start, stop, resume), and that does not reset the timer.

Thank you so much Axel! I truly appreciate your guidance!

Xojo has a very quirky and tedious way of handling/creating a simple up/down timer. It’s kind of pathetic that there is no simpler way to create it.

At first glance, that quirkiness may not be all that necessary. But Meade is learning, and that is what counts (pun intended).