Page 1 of 1

stopwatch-reverse counter

Posted: Tue Jan 19, 2016 4:55 pm
by Jellobus
Hi all,

I am trying to make a stop watch that reverse counts while the app is stopped. I used "the seconds" function and save "the seconds" into specialFolderPath("documents") whenever counting is stopped. so the saved seconds can be used for resuming count. but the problems is is has no accuracy..

Goal: Auto counting while the counting is stopped or app is shutdown.

Problem : lack of accuracy. when counting is resumed, the number in field "time" is increased.

reproduce this problem :
#1. click "start/resume" then it's reverse counting.
#2. click "stop" then the number stops. e.g. 22 <--8 sec passed from 30
#3. wait few seconds (5~10 sec) then click "start/resume"
#4.Problem will be found.-->the number in the field should be less than the prev number (e.g. the number should be less than 22) but sometimes it's higher than the previous number.

I attached a stack, please help if you have a better approach.
Thanks a lot in advance.

Cheers,

Louis

Code: Select all

---on start/resume button
global t1, t2, tDuration, tRemained, tStop, tTime


on mouseUp
   put empty into t1
   put empty into t2
   put empty into tRemained
   put empty into tStop
   put 30 into tDuration ## start point
   getTimeRemained
   startCount
end mouseUp

on getTimeRemained
   if url("file:" & specialFolderPath("documents") & "/timeCounter.txt") is not empty then      ##if there is a saved seconds
      put url("file:" & specialFolderPath("documents") & "/timeCounter.txt") into t1     ##the seconds saved from the last session
      put the seconds into t2     ## the current seconds
      if tDuration < (t2-t1) then      ## time passed for more than 30 sec
         answer "Time's up"
      else if tDuration = (t2-t1) then     ## time passed for 30 sec
         answer "Time's up"
      else if tDuration > (t2-t1) then
         put (tDuration - (t2-t1) ) into tRemained
      end if
   else      ##if it starts fresh
      put the seconds into t1
      put 30 into tRemained
   end if
end getTimeRemained

on startCount
   repeat until field "time" = 0
      if tStop = true then
         exit repeat
      end if

      put the seconds into t2
      
      put (tRemained-(t2-t1)) into tTime
      put tTime into field "time"
      
      wait for 100 millisec with messages
   end repeat
end startCount

------on stop button

global tStop

on mouseUp
   put true into tStop
   put the seconds into url("file:" & specialFolderPath("documents") & "/timeCounter.txt")     ## save current seconds
end mouseUp


Re: stopwatch-reverse counter

Posted: Tue Jan 19, 2016 10:50 pm
by bn
Hi Louis,

change your code in script of button "Start/Resume" to

Code: Select all

  else ##if it starts fresh
      put the seconds into t1
      put t1 into url("file:" & specialFolderPath("documents") & "/timeCounter.txt") -- ADD this line
      put 30 into tRemained
   end if
end getTimeRemained
and in button "Stop"

Code: Select all

on mouseUp
   put true into tStop
   --put the seconds into url("file:" & specialFolderPath("documents") & "/timeCounter.txt") -- BLOCK this line 
end mouseUp
changes marked in trailing comments
Now you refer to the time from start to current seconds, not from Stopp seconds to current seconds

on a sidenote: it would probably be cleaner to change your "StartCount" handler to the send in time form, something like this

Code: Select all

on startCount
   if tStop then
      exit startCount
   end if
   
   put the seconds into t2
   put (tRemained-(t2-t1)) into tTime
   put tTime into field "time"
   
   send "startCount" to me in 100 milliseconds
end startCount
Kind regards
Bernd

Re: stopwatch-reverse counter

Posted: Wed Jan 20, 2016 3:18 am
by Jellobus
Hi Bernd,

It works a lot better with your suggestion. but it sometimes counts faster with new script if I resumed counting. (about 10 sec faster sometimes.) but it seems a lot better than before. thanks anyway.

Cheers,

Louis

Re: stopwatch-reverse counter

Posted: Wed Jan 20, 2016 4:09 am
by Jellobus
I think it counts faster not because of any script error but because of some system delay when it get "the seconds". by the way, I still implement your suggestion. thanks a lot!

Cheers,

Louis

Re: stopwatch-reverse counter

Posted: Wed Jan 20, 2016 9:06 am
by bn
Hi Louis,

you might want to rethink your code.

The system is not lagging that much. File access is "slow" but you could easily measure that if you wanted. But since you are counting down seconds it would probably not matter much.
And my code has the same lags as your code since it writes just as often to the file as yours. To increase precision you could use milliseconds and round to the second.

Kind regards
Bernd