Stopwatch (chrono)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
Marvi
User
User
Posts: 17
Joined: Fri Oct 03, 2008 6:02 pm
Location: Milano

Stopwatch (chrono)

Post by Marvi »

I was looking for a very simple chrono but i didn't find anything here.

So I realized this little program: feel free to change what's wrong.

(Hundredth of a second , with delay = 25 and manual start/stop, are just for fun!)

Code: Select all

;Purebasic 5.0
;:::::::::::::::::::::. Simple Chrono .:::::::::::::::::::::::::



  
 Global start.d, stop.d
  
  Procedure.s timeformat(time)
    
    
    
    seconds.d=time/1000
     If seconds>59.999
      minutes.d=Int(seconds/60)
      seconds=seconds-minutes*60
    EndIf
    
    If minutes>59.999
      
      hours.d=Int(minutes/60)
      minutes=minutes-hours*60
      
    EndIf
    
    hours2$=Right("00"+Str(hours),2)
    minutes2$=Right("00"+Str(minutes),2)
    seconds2$=Right("0"+StrD(seconds,2),5)
    
    result.s=hours2$+":"+minutes2$+":"+seconds2$
    
    ProcedureReturn Result
  EndProcedure 

 
 
 
 
  
  Procedure chrono(parameter)

  Repeat
    stop=ElapsedMilliseconds()
         SetGadgetText(10,timeformat(stop-start)) 

    Delay(25)
  ForEver

EndProcedure

        
      If OpenWindow(0, 0, 0, 180, 180, "Chrono", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered )  
  ButtonGadget(1, 10, 10, 70, 40, "Start")
  ButtonGadget(2, 10, 10, 70, 40, "Stop")
  ButtonGadget(3, 100, 10, 70, 40, "Lap Time")
  ButtonGadget(4, 10, 10, 70, 40, "Clear")
  HideGadget(2,1)
  HideGadget(3,1)
  HideGadget(4,1)
  TextGadget(10, 10,  60, 160, 30, "00:00:00")  ; max 99h 59' 59"
  
  EditorGadget(11, 10, 110, 160, 60)

  
     Font1 = LoadFont(#PB_Any, "Quartz MS"  ,  18, #PB_Font_Bold)
    SetGadgetFont(10, FontID(Font1))
    SetGadgetColor(10, #PB_Gadget_BackColor, $000000)
    SetGadgetColor(10, #PB_Gadget_FrontColor, $04DF1D)
 
   Repeat
    EventID = WaitWindowEvent()

     If EventID = #PB_Event_Gadget
    
       Select EventGadget()
           
           
         Case 1 ; start
          
           start=ElapsedMilliseconds()
           ;start=start-359990000
           thread=CreateThread(@chrono(),0)
          
           HideGadget(1,1)
           HideGadget(2,0)
           HideGadget(3,0)
      
         Case 2 ; stop
           stop=ElapsedMilliseconds() 
          KillThread(thread)
         
          SetGadgetText(10,timeformat(stop-start)) 
          AddGadgetItem(11, a, timeformat(stop-start))
          
  HideGadget(2,1)
   HideGadget(4,0)
   HideGadget(3,1)
   
    Case 3  ; laptime
           stop=ElapsedMilliseconds() 
         
        
AddGadgetItem(11, a, timeformat(stop-start))
   
   Case 4 ; clear
   ClearGadgetItems(11)
   SetGadgetText(10,"00:00:00")
   HideGadget(4,1)
   HideGadget(1,0)
   
     EndSelect
    EndIf
     

   Until EventID = #PB_Event_CloseWindow
EndIf
End
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Stopwatch (chrono)

Post by infratec »

Hi,

I like it simple, so I 'optimized' a bit of your code:

Code: Select all

Procedure.s timeformat(time.i)
  
  ProcedureReturn FormatDate("%hh:%ii:%ss.", time / 100) + RSet(Str(time % 100), 2, "0")
  
EndProcedure
Bernd
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Stopwatch (chrono)

Post by wilbert »

infratec wrote:Hi,

I like it simple, so I 'optimized' a bit of your code:

Code: Select all

Procedure.s timeformat(time.i)
  
  ProcedureReturn FormatDate("%hh:%ii:%ss.", time / 100) + RSet(Str(time % 100), 2, "0")
  
EndProcedure
Bernd
It should be 1000 instead of 100 :wink:
Windows (x64)
Raspberry Pi OS (Arm64)
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Stopwatch (chrono)

Post by infratec »

Hi, Hi,

it was too simple.
Wilbert is right since we have milliseconds an not centiseconds.

So here is the corrected version:

Code: Select all

Procedure.s timeformat(time.i)
  
  ProcedureReturn FormatDate("%hh:%ii:%ss.", time / 1000) + RSet(Str(time % 1000), 2, "0")
  
EndProcedure
Bernd
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Stopwatch (chrono)

Post by davido »

Thanks Marvi, nice display. :D

@Bernd: Nice simplification.
DE AA EB
Marvi
User
User
Posts: 17
Joined: Fri Oct 03, 2008 6:02 pm
Location: Milano

Re: Stopwatch (chrono)

Post by Marvi »

Thank you guys, it's always simple but more elegant!

Code: Select all

;Purebasic 5.0
;:::::::::::::::::::::. Simple Chrono .::::::::::::::::::::::::

 Global start.d, stop.d
  
 Procedure.s timeformat(time.i)
  
  ProcedureReturn FormatDate("%hh:%ii:%ss.", time / 1000) + RSet(Str(time % 1000), 2, "0")
  
EndProcedure



  
  Procedure chrono(parameter)

  Repeat
    stop=ElapsedMilliseconds()
         SetGadgetText(10,timeformat(stop-start)) 

    Delay(25)
  ForEver

EndProcedure

        
      If OpenWindow(0, 0, 0, 180, 180, "Chrono", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered )  
  ButtonGadget(1, 10, 10, 70, 40, "Start")
  ButtonGadget(2, 10, 10, 70, 40, "Stop")
  ButtonGadget(3, 100, 10, 70, 40, "Lap Time")
  ButtonGadget(4, 10, 10, 70, 40, "Clear")
  HideGadget(2,1)
  HideGadget(3,1)
  HideGadget(4,1)
  TextGadget(10, 10,  60, 160, 30, "00:00:00")  ; max 99h 59' 59"
  
  EditorGadget(11, 10, 110, 160, 60)

  
     Font1 = LoadFont(#PB_Any, "Quartz MS"  ,  18, #PB_Font_Bold)
    SetGadgetFont(10, FontID(Font1))
    SetGadgetColor(10, #PB_Gadget_BackColor, $000000)
    SetGadgetColor(10, #PB_Gadget_FrontColor, $04DF1D)
 
   Repeat
    EventID = WaitWindowEvent()

     If EventID = #PB_Event_Gadget
    
       Select EventGadget()
           
           
         Case 1 ; start
          
           start=ElapsedMilliseconds()
           ;start=start-359990000
           thread=CreateThread(@chrono(),0)
          
           HideGadget(1,1)
           HideGadget(2,0)
           HideGadget(3,0)
      
         Case 2 ; stop
           stop=ElapsedMilliseconds() 
          KillThread(thread)
         
          SetGadgetText(10,timeformat(stop-start)) 
          AddGadgetItem(11, a, timeformat(stop-start))
          
  HideGadget(2,1)
   HideGadget(4,0)
   HideGadget(3,1)
   
    Case 3  ; laptime
           stop=ElapsedMilliseconds() 
         
        
AddGadgetItem(11, a, timeformat(stop-start))
   
   Case 4 ; clear
   ClearGadgetItems(11)
   SetGadgetText(10,"00:00:00")
   HideGadget(4,1)
   HideGadget(1,0)
   
     EndSelect
    EndIf
     

   Until EventID = #PB_Event_CloseWindow
EndIf
End 

 
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Stopwatch (chrono)

Post by applePi »

thanks for this useful utility, i want to use it as alarm clock instead of online clocks, such as playing sound after 5 seconds and exit, i have added to the proc:

Code: Select all

Procedure chrono(parameter)
  Repeat
    stop=ElapsedMilliseconds()
    SetGadgetText(10,timeformat(stop-start)) 
    If (stop-start)>=5000 ; more than 5 seconds
      PlaySound(alarm):Delay(5000)
      KillThread(thread)
      FreeSound(alarm)
      End
    EndIf
    Delay(25)
  ForEver
EndProcedure
but i'm not happy with the delay(5000) since should adapt it to different sounds lenghts
also do we need killthread here or End will kill the thread?
will play roar.wav in "C:\PureBasic\Examples\3D\Data" after 5 seconds and ends

Code: Select all

Global start.d, stop.d
Global alarm
InitSound()
 
 Procedure.s timeformat(time.i)
  
  ProcedureReturn FormatDate("%hh:%ii:%ss.", time / 1000) + RSet(Str(time % 1000), 2, "0")
  
EndProcedure
 
 
  
  Procedure chrono(parameter)
  Repeat
    stop=ElapsedMilliseconds()
    SetGadgetText(10,timeformat(stop-start)) 
    If (stop-start)>5000
      PlaySound(alarm):Delay(5000)
      KillThread(thread)
      FreeSound(alarm)
      End
    EndIf
    Delay(25)
  ForEver

EndProcedure

        
      If OpenWindow(0, 0, 0, 180, 180, "Chrono", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered )  
  ButtonGadget(1, 10, 10, 70, 40, "Start")
  ButtonGadget(2, 10, 10, 70, 40, "Stop")
  ButtonGadget(3, 100, 10, 70, 40, "Lap Time")
  ButtonGadget(4, 10, 10, 70, 40, "Clear")
  HideGadget(2,1)
  HideGadget(3,1)
  HideGadget(4,1)
  TextGadget(10, 10,  60, 160, 30, "00:00:00")  ; max 99h 59' 59"
  
  EditorGadget(11, 10, 110, 160, 60)

  
     Font1 = LoadFont(#PB_Any, "Quartz MS"  ,  18, #PB_Font_Bold)
    SetGadgetFont(10, FontID(Font1))
    SetGadgetColor(10, #PB_Gadget_BackColor, $000000)
    SetGadgetColor(10, #PB_Gadget_FrontColor, $04DF1D)
 
    LoadSound(alarm,"C:\PureBasic\Examples\3D\Data\roar.wav")
    Repeat
    EventID = WaitWindowEvent()

     If EventID = #PB_Event_Gadget
    
       Select EventGadget()
           
           
         Case 1 ; start
          
           start=ElapsedMilliseconds()
           ;start=start-359990000
           thread=CreateThread(@chrono(),0)
          
           HideGadget(1,1)
           HideGadget(2,0)
           HideGadget(3,0)
      
         Case 2 ; stop
           stop=ElapsedMilliseconds() 
          KillThread(thread)
         
          SetGadgetText(10,timeformat(stop-start)) 
          AddGadgetItem(11, a, timeformat(stop-start))
          
  HideGadget(2,1)
   HideGadget(4,0)
   HideGadget(3,1)
   
    Case 3  ; laptime
           stop=ElapsedMilliseconds() 
         
        
AddGadgetItem(11, a, timeformat(stop-start))
   
   Case 4 ; clear
   ClearGadgetItems(11)
   SetGadgetText(10,"00:00:00")
   HideGadget(4,1)
   HideGadget(1,0)
   
     EndSelect
    EndIf
     

   Until EventID = #PB_Event_CloseWindow
EndIf
End
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Stopwatch (chrono)

Post by infratec »

Hi,

to avoid with trouble different directories:

Code: Select all

LoadSound(alarm, #PB_Compiler_Home + "Examples\3D\Data\roar.wav")
And for playing the sound:

Code: Select all

PlaySound(alarm)
While SoundStatus(alarm) = #PB_Sound_Playing
  Delay(1)
Wend
FreeSound(alarm)
End
KillThread() is not neccessary.
If the thread is finished, it is finished.

Bernd
User avatar
Demivec
Addict
Addict
Posts: 4268
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Stopwatch (chrono)

Post by Demivec »

infratec wrote:KillThread() is not neccessary.
If the thread is finished, it is finished.
Here are some simple modifcations to accomplish what infratec suggested.

Code: Select all

Global start.d, stop.d, endChrono ;endChrono can also be a local Define at main level

;---

Procedure chrono(parameter)
  ;Shared endChrono ;needed if endChrono is not Global
  endChrono = 0
  Repeat
    stop=ElapsedMilliseconds()
    SetGadgetText(10,timeformat(stop-start)) 

    Delay(25)
  Until endChrono

EndProcedure

;--- to end thread
        stop=ElapsedMilliseconds() 
        endChrono = 1 ;any non-zero value will end thread
;---
Post Reply