Countdown Timer with minutes seconds and milliseconds!

Thread Solved

Join Date: Jan 2008
Posts: 11
Reputation: theonlyhugeg is an unknown quantity at this point 
Solved Threads: 0
theonlyhugeg theonlyhugeg is offline Offline
Newbie Poster

Countdown Timer with minutes seconds and milliseconds!

 
0
  #1
Jan 4th, 2008
hi everybody!
im in a basketball team n my coach asked me to make a scoreboard program for the huge screen we have (he knows im the only one from the team that does some programming)
im using Visual Basic 2008.
i pretty much know how to do everything except for the countdown timer which has to count down all the way from 8 minutes to 0. it would also be nice if it makes a beep when it reaches 0. so here's the problem. i know how to make a simple timer with seconds. but how to put minutes n milliseconds?? it should look like this
minutes:seconds:milliseconds
someone please help!! im desperate
thanks in advance
Last edited by theonlyhugeg; Jan 4th, 2008 at 1:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #2
Jan 4th, 2008
i know how to make a simple timer with seconds. but how to put minutes n milliseconds?? it should look like this
minutes:seconds:milliseconds
Using the 'Now' function with VB6 (this is a VB6 forum) gives you the current time. However, this will only give you the time in seconds.

Even if you convert the Now to a double precision value (which you'll have to use obtain milleseconds), the best you will get is still a change in the visible value every second.


So what you need to use is the Windows API 'QueryPerformanceCounter' in your program to obtain a higher precision of accuracy. To find how much precision your CPU will provide you use the 'QueryPerformanceFrequency' API

The degree of precision depends on your CPU and how simple you can keep your code.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Type LARGE_INTEGER
  2. LowPart As Long
  3. HighPart As Long
  4. End Type
  5.  
  6. Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
  7.  
  8. Private Declare Function
  9.  
  10. QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long
  11.  
  12. Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

I've given you a start. But here's the pseudo logic you'll need:

Determine your CPU's capabilities with the QueryPerformanceFrequency

Create a loop to provide a value to use for your millisecond display.

And you need to provide some method to get yourself out of the loop.

I'll leave the rest to you. But that should get you started.

Hank
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 62
Reputation: Mbt925 is an unknown quantity at this point 
Solved Threads: 5
Mbt925's Avatar
Mbt925 Mbt925 is offline Offline
Junior Poster in Training

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #3
Jan 4th, 2008
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Declare Function GetTickCount Lib "kernel32.dll" () As Long

This API function Return a time in miliSecond for each time called.
you can use it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #4
Jan 4th, 2008
This API function Return a time in miliSecond for each time called.
you can use it.
That API depends on the resolution of of the operating system's timer.
That could be anywhere from 9 to 100 milliseconds.

If the user requires a resolution of 1 millisecond, then you'll have to use the other API's to determine if the running CPU has that capability: hence the necessity of using the QueryPerformanceFrequency and QueryPerformanceCounter APIs.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #5
Jan 4th, 2008
This code, when the timer property for interval is set at 1 millisecond, should give you resolution of the user's system timer.

This came out to be about 15 milliseconds on a 2.4 GHz pentium4

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2. Private Declare Function GetTickCount Lib "kernel32" () As Long
  3. Dim blnStart As Boolean
  4.  
  5. Private Sub Command1_Click()
  6. Timer1.Enabled = Not Timer1.Enabled
  7. End Sub
  8.  
  9. Private Sub Form_Load()
  10. On Error GoTo Load_ERROR
  11. blnStart = True
  12. Exit Sub
  13. Load_ERROR:
  14. MsgBox Err.Description, vbInformation, "eRROR"
  15. End Sub
  16.  
  17. Private Sub Timer1_Timer()
  18. Static CurrentTickCount As Long
  19. Dim lngTickCount As Long, lngDifference As Long
  20. If blnStart = True Then
  21. lngTickCount = GetTickCount
  22. CurrentTickCount = 0
  23. blnStart = False
  24. Else
  25. lngTickCount = GetTickCount
  26. End If
  27. If CurrentTickCount = lngTickCount Then
  28.  
  29. CurrentTickCount = lngTickCount
  30. lngTickCount = 0
  31. Else
  32. Label1.Caption = lngTickCount
  33. lngDifference = lngTickCount - CurrentTickCount
  34. Debug.Print lngDifference
  35. CurrentTickCount = lngTickCount
  36. End If
  37.  
  38. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: theonlyhugeg is an unknown quantity at this point 
Solved Threads: 0
theonlyhugeg theonlyhugeg is offline Offline
Newbie Poster

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #6
Jan 4th, 2008
hi
i noticed now that this forum is for vb 6... but thats no problem. i just downloaded n installed that and your code works fine! thanks! one question though... now the count is going up...like its adding time and its starting from a number like 1774796... how can i make it actually count down and start from 8 minutes??

thanks a lot
appreciate your help
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #7
Jan 5th, 2008
its starting from a number like 1774796
It always helps to read the manual. In this case: the documentation which is freely available online from MSDN for this API.

how can i make it actually count down and start from 8 minutes??
http://support.microsoft.com/kb/172338
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #8
Jan 5th, 2008
Everything else is just algebra and logic.

You need to have one established reference point: That's the value you derive from using the above API's and finding the current time using the Now function.

Lets' call that value lngValueA. Save that value and use it as a reference point for your minutes:seconds:milleseconds.

Once you have established that reference point. You need to come up with a formula to calculate How many minutes, how many seconds, and how many fractional seconds.

This is just basic algebra.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: theonlyhugeg is an unknown quantity at this point 
Solved Threads: 0
theonlyhugeg theonlyhugeg is offline Offline
Newbie Poster

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #9
Jan 5th, 2008
hi
i figured out how to do it. i used a different method... i dont think its as accurate as yours, but its not really important... in basketball the milliseconds are just there to look cool lol.
anyway thanks for your help
im using this code right now

Option Explicit

Dim EndTime As Single
Private Sub Form_Load()
Timer1.Enabled = False
End Sub
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled = True Then Command1.Caption = "Stop"
If Timer1.Enabled = False Then Command1.Caption = "Start"
EndTime = (Timer + 8 * 60) Mod 86400
Timer1.Interval = 20
End Sub

Private Sub Timer1_Timer()
Dim CtDn As Long
CtDn = (EndTime - Timer) * 100
If CtDn < 0 Then
If CtDn < -1000 Then 'bit of faff to avoid over midnight countdown
CtDn = CtDn + 8640000
Else
Label1.Caption = "0:00:00"
Beep 'or somesuch
Timer1.Interval = 0
Exit Sub
End If
End If
Label1.Caption = Format((CtDn \ 6000) Mod 60, "0:") & Format((CtDn \ 100) Mod 60, "00:") & Format(CtDn Mod 100, "00")
End Sub
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Countdown Timer with minutes seconds and milliseconds!

 
0
  #10
Jan 5th, 2008
If CtDn < -1000 Then 'bit of faff to avoid over midnight countdown
CtDn = CtDn + 8640000
Else
Use the datediff function to avoid midnight problems.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC