Beginner Stop Watch!

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
nalexander50
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3
Joined: Fri Feb 22, 2013 1:55 am

Beginner Stop Watch!

Post by nalexander50 » Fri Feb 22, 2013 2:00 am

Hello,

I am new to LiveCode but have basic experience writing in languages like C, Java, and VB. Currently, I am trying to implement a timer into an application that I am writing but am having trouble. I want the application to Count up every second just like a stop watch. I also want it to displace hours, minutes, and seconds into respective slots fitting the format 00:00:00. I have been very close at implementing the timer, but am having serious troubles with the hours place. Can anyone help?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Beginner Stop Watch!

Post by Simon » Fri Feb 22, 2013 2:42 am

Hello and welcome to the forum!
This should get you started:

Code: Select all

local tSec,tMin,tHour
on mouseUp
startClock
end mouseUp

on startClock
   set the numberFormat to "00" --this sets it so that it always shows a leading 0 
   add 1 to tSec
   if tSec = 60 then 
      add 1 to tMin
      put 00 into tSec
   end if
   if tMin = 60 then
      add 1 to tHour
      put 00 into tMin
   end if
   if tMin = 0 then put 00 into tMin
   if tHour = 0 then put 00 into tHour
   put tHour & ":" & tMin & ":" & tSec into fld 1
   send startClock to me in 1 sec --go back to the top and do it again in 1 sec
end startClock
I don't claim that this is the best way to do it. I'm sure that somebody here could write it with much less code. But I think this is easy to read.

Good luck and enjoy LiveCode!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

nalexander50
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3
Joined: Fri Feb 22, 2013 1:55 am

Re: Beginner Stop Watch!

Post by nalexander50 » Thu Feb 28, 2013 10:52 pm

Thanks so much. This is exactly what I needed. And I do not mind the length of code. I had this exact script set up, the only problem I was encountering was when I reached 1 hour, the seconds either set to 00 and stopped counting or was set to -36,000. I had a few bizarre bugs, but this is exactly what I needed. Thanks!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Beginner Stop Watch!

Post by Simon » Thu Feb 28, 2013 11:09 pm

Glad I could help :D

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: Beginner Stop Watch!

Post by rumplestiltskin » Fri Mar 01, 2013 5:59 am

Just dropped in to appreciate the code provided in this thread and was wondering something:

I figured out how to start and stop the timer using a flag I set when a mouseUp occurs in the field. How, though, would I reset the timer to zero? I am guessing I'd have to put the handler to do this in the same script in order to reset tSec, tMin and tHour to "00".

...time passes...

Yep; that did it. I added a button named "Reset" which sent a resetTimer message to the field and, in the resetTimer handler in the field, I set the three time vars to "00" and put empty into the field.

If I tried to reset tSec, etc. to "00" outside of the field script, it did nothing (as the variables declared as "local" can only be changed from any handler within that script).

Thanks to the OP for asking the question and to Simon for his answer. :D

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Beginner Stop Watch!

Post by jacque » Fri Mar 01, 2013 6:45 am

EDIT: I just realized we're writing a stopwatch, not a clock, so the following isn't what you wanted. I'll leave it here anyway. This one is just a clock.

The convert command is handy for dates and times. Here's another way to do it:

Code: Select all

local sFlag

on clock
  get the seconds
  add 1 to it
  convert it to long time
  put it into fld 1
  if sFlag is true then send "clock" to me in 1 second
end clock
Add a handler that sets sFlag to true or false to turn the clock on and off.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Beginner Stop Watch!

Post by Dixie » Fri Mar 01, 2013 2:11 pm

Another way to do it... the script is in the card script..

be well

Dixie
Attachments
Time.rev.zip
(5.13 KiB) Downloaded 400 times

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Beginner Stop Watch!

Post by Mag » Fri Feb 07, 2014 3:48 am

jacque wrote:EDIT: I just realized we're writing a stopwatch, not a clock, so the following isn't what you wanted. I'll leave it here anyway. This one is just a clock.

The convert command is handy for dates and times. Here's another way to do it:

Code: Select all

local sFlag

on clock
  get the seconds
  add 1 to it
  convert it to long time
  put it into fld 1
  if sFlag is true then send "clock" to me in 1 second
end clock
Add a handler that sets sFlag to true or false to turn the clock on and off.
Hi Jacque,

Why you add 1 to it?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Beginner Stop Watch!

Post by jacque » Fri Feb 07, 2014 6:56 am

The handler shouldn't add 1, I probably forgot to remove that line when I was copying from something else. Good catch.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Beginner Stop Watch!

Post by Mag » Fri Feb 07, 2014 2:14 pm

Thanks!

Your message contains 7 characters. The minimum number of characters you need to enter is 10.

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

About Clock

Post by Mag » Thu Feb 13, 2014 12:21 pm

One last consideration: if the clock shows also the seconds, they do not coincide with those of the system, but their change is always a little later (it depends on when I start the script). I guess you can't make them change together with those of the system clock...

Code: Select all

on updateClock
      put the long system time into field "ClockField"
   
      send "updateClock" to me in 1 seconds
end updateClock

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Beginner Stop Watch!

Post by bn » Thu Feb 13, 2014 12:36 pm

Hi Mag,

if you want to sync your seconds to the seconds of the system you use a little different way to send in time
you calculate the time remaining to the next full second of the system and adjusting your send in time value accordingly.

Like this

Code: Select all

local sFlag = false

on mouseUp
   put not sFlag into sFlag
   send "doClock" to me in 0 milliseconds
end mouseUp

on doClock
   if not sFlag then exit doClock
   get the seconds
   convert it to long time
   put it into field 1
   -- get remaining milliseconds
   put 1000 - char 3 to - 1 of the milliseconds into tHowMany -- char 3 to - 1 of the milliseconds are the hundreds
   send "doClock" to me in tHowMany milliseconds
end doClock
KInd regards
Bernd

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Beginner Stop Watch!

Post by jacque » Thu Feb 13, 2014 6:33 pm

One of the cleverest things I ever saw was a method from Geoff Canyon about how to calculate the exact moment the system clock changes the seconds:

Code: Select all

send "updateTimer" to me in (1 - (the long seconds mod 1)) seconds 
Pretty cool.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Beginner Stop Watch!

Post by Mag » Thu Feb 13, 2014 6:43 pm

Thanks guys, two ways to do one thing well!

Post Reply