DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Visual Basic 4 / 5 / 6 (http://www.daniweb.com/forums/forum4.html)
-   -   Countdown Timer with minutes seconds and milliseconds! (http://www.daniweb.com/forums/thread103508.html)

theonlyhugeg Jan 4th, 2008 12:51 pm
Countdown Timer with minutes seconds and milliseconds!
 
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
:) :)

hkdani Jan 4th, 2008 2:12 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
Quote:

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.

Private Type LARGE_INTEGER
    LowPart As Long
    HighPart As Long
End Type

Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long

Private Declare Function

QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

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

Mbt925 Jan 4th, 2008 3:36 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
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.

hkdani Jan 4th, 2008 7:16 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
Quote:

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.

hkdani Jan 4th, 2008 9:18 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
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

Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim blnStart As Boolean

Private Sub Command1_Click()
  Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Form_Load()
  On Error GoTo Load_ERROR
  blnStart = True
  Exit Sub
Load_ERROR:
  MsgBox Err.Description, vbInformation, "eRROR"
End Sub

Private Sub Timer1_Timer()
Static CurrentTickCount As Long
Dim lngTickCount As Long, lngDifference As Long
If blnStart = True Then
  lngTickCount = GetTickCount
  CurrentTickCount = 0
  blnStart = False
Else
  lngTickCount = GetTickCount
End If
If CurrentTickCount = lngTickCount Then
 
  CurrentTickCount = lngTickCount
  lngTickCount = 0
Else
  Label1.Caption = lngTickCount
  lngDifference = lngTickCount - CurrentTickCount
  Debug.Print lngDifference
  CurrentTickCount = lngTickCount
End If

End Sub

theonlyhugeg Jan 4th, 2008 10:48 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
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??
:confused:
thanks a lot
appreciate your help :)

hkdani Jan 5th, 2008 5:02 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
Quote:

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.

Quote:

how can i make it actually count down and start from 8 minutes??
http://support.microsoft.com/kb/172338

hkdani Jan 5th, 2008 10:45 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
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.

theonlyhugeg Jan 5th, 2008 11:09 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
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

hkdani Jan 5th, 2008 11:21 pm
Re: Countdown Timer with minutes seconds and milliseconds!
 
Quote:

If CtDn < -1000 Then 'bit of faff to avoid over midnight countdown
CtDn = CtDn + 8640000
Else
Use the datediff function to avoid midnight problems.


All times are GMT -4. The time now is 6:08 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC