|
-
Visual Basic timer help
Hey guys. I asked this once before and never really figured anything out. I thought I had gotten out of doing this, but my friend called me today looking for his program. So here it goes.
My friend is a chiropractor and he needs a program to time reflexes. What he wants is for the screen to change color, then the user hit spacebar as fast as he/she can and then record the time it took to do so. I need to record the time down to the thousandths (.999). The timer in VB only does the hundredths (.99) and I hear isn't the most accurate thing in the world. Is there a way to get it down to the thousandths? Someone gave me a link to another VB timer but it was very confusing and I did not get it to work at all. Any help would be great!!!!!! Thanks!
Here is the program:
User sits at screen. 
Screen changes from blue screen to green screen...counter starts
User hits spacebar as soon as he/she sees the green screen
When spacebar hit, counter stops.
Output info.
Neal
-
Re: Visual Basic timer help
Originally posted by ZF_NeAlvey
Hey guys. I asked this once before and never really figured anything out. I thought I had gotten out of doing this, but my friend called me today looking for his program. So here it goes.
My friend is a chiropractor and he needs a program to time reflexes. What he wants is for the screen to change color, then the user hit spacebar as fast as he/she can and then record the time it took to do so. I need to record the time down to the thousandths (.999). The timer in VB only does the hundredths (.99) and I hear isn't the most accurate thing in the world. Is there a way to get it down to the thousandths? Someone gave me a link to another VB timer but it was very confusing and I did not get it to work at all. Any help would be great!!!!!! Thanks!
Here is the program:
User sits at screen. 
Screen changes from blue screen to green screen...counter starts
User hits spacebar as soon as he/she sees the green screen
When spacebar hit, counter stops.
Output info.
Neal
Something like this would be easy. Look up the 'GetTickCount' windows API procedure in the API viewer. It returns the number of milliseconds elapsed since Windows last started, and its as accurate as your computer can be!
I could have source code for such a program posted in 15 minutes or less easily!
-
All you need is a timer control on your form!
Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim lngOldTime As Long
Dim lngNewTime As Long
Dim lngResult As Long
Private Sub Form_Load()
'Disable the timer when loading
Timer1.Enabled = False
Me.Show
Randomize 'This procedure resets the random number generator
'This equasion returns a radom value between 5000 and 10000, and can be found in the msdn help file
'for the 'Rnd' function. This makes the form's backcolor change at a random time between 5 and 10 seconds.
Timer1.Interval = Int((10000 - 5000 + 1) * Rnd + 5000)
'Make the backcolor blue and enable the timer.
Me.BackColor = vbBlue
Timer1.Enabled = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'32 = the space bar
If KeyCode = 32 Then
'If the user presses the space bar before the screen turns green, the first variable wont be set.
If lngOldTime = 0 Then
Timer1.Enabled = False
MsgBox "You pressed the space bar before the screen turned green"
Randomize
Timer1.Interval = Int((10000 - 5000 + 1) * Rnd + 5000)
Timer1.Enabled = True
'If the screen has turned green
Else
'Get the new time.
lngNewTime = GetTickCount
'Calculate the time elapsed.
lngResult = lngNewTime - lngOldTime
'Output the result.
MsgBox Str(lngResult) & " milliseconds elapsed"
'Reset the variables
lngOldTime = 0
lngNewTime = 0
'Make the backcolor blue again, randomize the interval, and start the whole thing again!
Form1.BackColor = vbBlue
Timer1.Enabled = False
Randomize
Timer1.Interval = Int((10000 - 5000 + 1) * Rnd + 5000)
Timer1.Enabled = True
End If
End If
End Sub
Private Sub Timer1_Timer()
'When the timer goes off, make the form green and set the first variable to the current time.
Form1.BackColor = vbGreen
lngOldTime = GetTickCount
End Sub
Hmm, was that less than 15 minutes?
-
Mako Shark
the problem with the standard timer/time functions is the poor resolution and/or accuracy. so for reflex gauge type applications you want to use "multimedia timers".
you can access the mm timer by api directly but i dont remember all the details about using APIsin VB so i recommend some activex controls that encapsulates multimedia timers. here is one:
http://download.com.com/3000-2401-88...ml?tag=lst-0-1
Last edited by dighn; 07-01-2002 at 09:09 PM.
.
-
Thanks guys for the replies. Your code looked impressive. I'm getting ready to cut/paste it and take a look at it. Thanks for the help!
-
hu flung dung,
That is really impressive. Seems to work like a charm. I really appreciate it. I just need to add a few more things to it and he should be good, or atleast I hope he is. Thanks again!
-
Catfish
the problem with the timer control is that there is no garuntee it will fire at the correct interval, but that it wont fire BEFORE the interval. so if your machine is bogged down, it may fire well after the interval. You can get free 'dll' files that implement far more acurate timers, or you can use the multimedia timers, etc.
-
Originally posted by dighn
the problem with the standard timer/time functions is the poor resolution and/or accuracy. so for reflex gauge type applications you want to use "multimedia timers".
you can access the mm timer by api directly but i dont remember all the details about using APIsin VB so i recommend some activex controls that encapsulates multimedia timers. here is one:
http://download.com.com/3000-2401-88...ml?tag=lst-0-1
DWORD timeGetTime();
but that is for C++
-
Originally posted by df
the problem with the timer control is that there is no garuntee it will fire at the correct interval, but that it wont fire BEFORE the interval. so if your machine is bogged down, it may fire well after the interval. You can get free 'dll' files that implement far more acurate timers, or you can use the multimedia timers, etc.
If you looked at my code, then you would know that I'm using the GetTickCount procedure to time reflexes, and the timer control to randomize the time it takes for the screen to turn from blue to green.
It is impossible to have a perfectly accurate timer DLL or procedure because your system timer is not perfectly accurate! GetTickCount is no more or less accurate than any other timing-related Windows API call! You system timer, however, would be accurate to within less than 10 milliseconds!. I don't think perfect accuracy is important when just timing somone's reflexes (it probably takes more than 10 milliseconds to hit the space bar anyhow)!
Last edited by hu flung dung; 07-02-2002 at 01:51 PM.
-
Mako Shark
Originally posted by hu flung dung
If you looked at my code, then you would know that I'm using the GetTickCount procedure to time reflexes, and the timer control to randomize the time it takes for the screen to turn from blue to green.
It is impossible to have a perfectly accurate timer DLL or procedure because your system timer is not perfectly accurate! GetTickCount is no more or less accurate than any other timing-related Windows API call! You system timer, however, would be accurate to within less than 10 milliseconds!. I don't think perfect accuracy is important when just timing somone's reflexes (it probably takes more than 10 milliseconds to hit the space bar anyhow)!
but multimedia timer APIs are supposed to be very accurate, certainly more so than reguarly API timers
-
Originally posted by dighn
but multimedia timer APIs are supposed to be very accurate, certainly more so than reguarly API timers
But how can you make a software timer more accurate than your own system timer?
-
Mako Shark
Originally posted by hu flung dung
But how can you make a software timer more accurate than your own system timer?
i dont know. from what i read, multimedia timer APIs uses the more precise timing devices.
-
Originally posted by dighn
i dont know. from what i read, multimedia timer APIs uses the more precise timing devices.
The only timing device in your computer is your system timer! All timing software/code is based on it! These API's you speak of cannot be more accurate than your own system timer!
Can you please show me the source code for a multimedia timer that claims to be more accurate than the Windows API?
-
Mako Shark
actually multimedia timer is part of the windows API
here's some information on it: http://www.microsoft.com/hwdev/platf...c/mm-timer.asp
as u can see there are a lot of timing devices in your computer
Last edited by dighn; 07-02-2002 at 08:45 PM.
.
-
Ah, cool!
Thanx for the heads up! I did a search on msdn.microsoft.com and found the 'timeGetTime' procedure, which has more accuracy (its using the multimedia timer) and it works exactly the same way!
Here's the declaration:
Code:
Private Declare Function timeGetTime Lib "winmm.dll" Alias "timeGetTime" () As Long
However, they've also mentioned that the QueryPerformanceCounter and QueryPerformanceFrequency functions can give you even more accuracy than the multimedia timer because their based on the clock cycles of your processor!
Last edited by hu flung dung; 07-02-2002 at 10:23 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|