Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > How to program two different timers as to keep tracking the responses in miliseconds?
 

How to program two different timers as to keep tracking the responses in miliseconds?

I have two Buttons, button A and button B. After 100 clickson button A a pictures is displayed and after 10 clickson button Banother picture is displayed. I wanna have two different timers as to timing each response (button a or button B respectively) withouth these two timers to be timing both these responses. I try to do this for over one month!! Please help me!!!
agg.ioa  Saturday, November 28, 2009 7:56 PM


Not sure I understand

This code times the time in between the first click and the 100th one on the button 1 and puts the total time in a label

and times the time in between the first and the tenth click on the button 2 and puts the the total time in label 2

(Time in millisecondes)

Public Class Form1

Dim NbClickButton1 As Integer = 0

Dim NbClickButton2 As Integer = 0

Dim SW1 As New Stopwatch

Dim SW2 As New Stopwatch

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Handles Button1.Click

NbClickButton1 += 1

If NbClickButton1 = 1 Then

SW1.Start()

ElseIf NbClickButton1 = 100 Then

SW1.Stop()

Label1.Text = CInt(SW1.ElapsedMilliseconds)

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Handles Button2.Click

NbClickButton2 += 1

If NbClickButton2 = 1 Then

SW2.Start()

ElseIf NbClickButton2 = 10 Then

SW2.Stop()

Label2.Text = CInt(SW2.ElapsedMilliseconds)

End If

End Sub

End Class

Crazypennie  Saturday, November 28, 2009 8:29 PM
I don't understand either but why use a timer why can't you check the current time when a button is clicked to the last time the button was clicked beginning on a certain date . You can store the number of clicks in a file with the date and time of each click if you wanted like a log . If you try doing it memory any interruption in the computer would wipe out that has been done . Anything can happen in a months time .
coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read .
bdbodger  Saturday, November 28, 2009 10:54 PM
That was more than helpful!!! Thank you very much from the bottom of my heart!!! I am doing my Ph.D. in Psychology and I have to program some experiments in Visual Basic. One more question, I wanna have numbers like 0.87777 for instance for the first click, or 1.34444 for the socnd etc. Now everything is in milliseconds. Can i get seconds and milliseconds together?? Thank you once more!!!
agg.ioa  Sunday, November 29, 2009 12:24 AM


Just divide the value in milliseconde by 1000. this will show it in secondes.

Notice that you need to change CInt to CStr In
"CStr(SW1.ElapsedMilliseconds / 1000)"
(It was my error )

Public Class Form1

Dim NbClickButton1 As Integer = 0

Dim NbClickButton2 As Integer = 0

Dim SW1 As New Stopwatch

Dim SW2 As New Stopwatch

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click

NbClickButton1 += 1

If NbClickButton1 = 1 Then

SW1.Start()

ElseIf NbClickButton1 = 100 Then

SW1.Stop()

Label1.Text = CStr(SW1.ElapsedMilliseconds / 1000)

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button2.Click

NbClickButton2 += 1

If NbClickButton2 = 1 Then

SW2.Start()

ElseIf NbClickButton2 = 10 Then

SW2.Stop()

Label2.Text = CStr(SW2.ElapsedMilliseconds / 1000)

End If

End Sub

End Class

Crazypennie  Sunday, November 29, 2009 12:34 AM
There are other time fuctions you can use as well . Try add a button to your form and this code .

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static ButtonPress As Date = Now
        Static StartTime As Date = ButtonPress
        Dim NewTime As Date = Now
        If NewTime > ButtonPress Then
            Dim TimeDiff As TimeSpan = NewTime - ButtonPress
            Dim StartDiff As TimeSpan = NewTime - StartTime
            MessageBox.Show("You last pressed the button " _
                            & vbCrLf & TimeDiff.Days & " Days " _
                            & vbCrLf & TimeDiff.Hours & " Hours " _
                            & vbCrLf & TimeDiff.Minutes & " Minutes " _
                            & vbCrLf & TimeDiff.Seconds & " Seconds ago", "Time Difference" _
                            , MessageBoxButtons.OK)

            MessageBox.Show("The Time in Decimal Days is " & TimeDiff.TotalDays _
                            & vbCrLf & " From first button click the difference is " _
                            & StartDiff.TotalDays)
        End If
        ButtonPress = NewTime
    End Sub

coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read .
bdbodger  Sunday, November 29, 2009 2:54 AM
Thanks a million!!! Now I wanna the SW (stopwatch) to reset after every 60 seconds. I write the following code under the button1.Click: if Label1.Text >= 60 then sw.Reset () End if, and the program crashes...Any idea for this too?
agg.ioa  Sunday, November 29, 2009 1:04 PM


I dont understand when you want to reset, when pressed 10 times or after 60 secondes ?


Please post your code It will be much easier for me to make it right

This exemple resets the timerafter 60 secondes

Public Class Form1

Dim NbClickButton1 As Integer = 0

Dim NbClickButton2 As Integer = 0

Dim SW1 As New Stopwatch

Dim SW2 As New Stopwatch

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

NbClickButton1 += 1

If NbClickButton1 = 1 Then

SW1.Start()

ElseIf NbClickButton1 = 100 Then

SW1.Stop()

'Label1.Text = CStr(SW1.ElapsedMilliseconds / 1000)

End If

If SW1.ElapsedMilliseconds > 60000 Then

SW1.Reset()

NbClickButton1 = 0

End If

Label1.Text = CStr(SW1.ElapsedMilliseconds / 1000)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

NbClickButton2 += 1

If NbClickButton2 = 1 Then

SW2.Start()

ElseIf NbClickButton2 = 10 Then

SW2.Stop()

End If

If SW2.ElapsedMilliseconds > 60000 Then

SW2.Reset()

NbClickButton2 = 0

End If

Label2.Text = CStr(SW2.ElapsedMilliseconds / 1000)

End Sub

End Class

Crazypennie  Sunday, November 29, 2009 5:45 PM
I would like to thank once more!!!! What I am doing is much more complicated. I have a button A in which a picture of 1 Euro is displayed after of an average 70 clicks. Simultaneously there is a timer which functions randomly from 1 sec to 35 sec andmay steal this euro and then another picture is displayed showing a euro with an X. I wanted the code with the timers so I could keep track of the clicks and the time, so as to can calculate the frequency per minute. The button B has a timer, which postpones the loss for 45 sec, after having pressed for 10 times. The person does not click this button with the mouse. Instead, presses the letter K from the keyboard. (Here, the problem that I have faced is that hecan press the letter more than 10 times, so the timer that you suggested me, keeps counting - so I would like the presses of the letter K to be deactivated after the 10 presses). The code is the following (Just because you asked me - Hope not to disturbing you with all my silly questions)

Private

Sub butA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butA.Click

SW.Start()

vClicksB = 0

lblTime.Text = VB.Timer - vTime

vTime1 = VB.Timer

butB.Visible =

False

vCount = vCount + 1

If (Me.BackColor = Color.Salmon) AndAlso (tmrCheck.Enabled = False) AndAlso VB.Timer - vTime > vRand Then

imgB.Visible =

True

butA.Visible =

False

butB.Visible =

False

vCount = 0

vClicks = -1

rand_number()

randA_number()

lblNoPoints.Text = vPoints + 1

lblPoints.Text = lblPoints.Text - 1

vPoints = vPoints + 1

SW.Stop()

End If


vClicks = vClicks + 1

vSumA = vSumA + 1

If vClicks >= vRandA Then

imgA.Visible =

True

butA.Visible =

False

butB.Visible =

False

randA_number()

vClicks = 0

vCount = 0

rand_number()

lblPoints.Text = lblPoints.Text + 1

SW.Stop()

End If

If (tmrCheck.Enabled = True) AndAlso (vClicks >= vRandA) Then

imgA.Visible =

True

butA.Visible =

False

butB.Visible =

False

vClicks = 0

randA_number()

vCount = 0

rand_number()

lblPoints.Text = lblPoints.Text + 1

SW.Stop()

End If

lblTotPoints.Text = (lblPoints.Text) - (lblNoPoints.Text)

If SW.ElapsedMilliseconds > 60000 Then

SW.Reset()

vSumA = 0 ' this code is the code I asked for so I could calculate the frequecy afrewards

End If

Label1.Text =

CStr(SW.ElapsedMilliseconds / 1000)

WriteLine(6, vRand, vRandA, vClicks, vCount, lblTime.Text, lblPoints.Text, lblNoPoints.Text, lblTotPoints.Text,

Me.BackColor = Color.SkyBlue, Me.BackColor = Color.Salmon, Me.BackColor = Color.LightSalmon, Label1.Text, vSumA)

End Sub


Private

Sub Form9_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

Select Case e.KeyCode

Case Keys.K

butB_Click(sender,

New System.EventArgs)

End Select

End Sub


Private
Sub butB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butB.Click

SW2.Start()

lblTime.Text = VB.Timer - vTime

vTime1 = VB.Timer

vClicksB = vClicksB + 1

vSumB = vSumB + 1

butA.Visible =

False

tmrCheck.Enabled =

False

If (vClicks = 0) AndAlso (vClicksB > 9) Then

imgC.Visible =

True

butA.Visible =

False

butB.Visible =

False

Me.BackColor = Color.SkyBlue

vClicksB = 0

vCount = 0

rand_number()

SW2.Stop()

End If

if SW2.ElapsedMIlliseconds > 30000 then
SW2.Reset ()
End if

Label2.Text = CInt(SW2.ElapsedMilliseconds)

WriteLine(6,

"The button B was pushed" & " ", vClicksB, lblTime.Text, "The safety period was" & " ", tmrCheck.Interval, Me.BackColor = Color.SkyBlue, Me.BackColor = Color.Salmon, Me.BackColor = Color.LightSalmon, Label2.Text / 1000, vSumB)

End Sub


agg.ioa  Sunday, November 29, 2009 6:56 PM


Ok I understand now,

If you have more question just ask, you are not disturbing me at all,

It is just so easier to answer the questions when I undestand what the code should do
Crazypennie  Sunday, November 29, 2009 7:13 PM
The only thing left is to deactivate the presses of the letter K after having pressed for 10 times.
agg.ioa  Sunday, November 29, 2009 7:25 PM


I believe that you dont want to desactivate the K forever

Here a code that check how many times the K was pressed in the 40 secondes preceding the actual key stroke.

If it was pressed more than 10 times it does not call butB_Click. ( so maximum 10 K in 40secondes )

If 40 secondes is not good, adjust "New TimeSpan(Hour, Minute, Seconds)"



Dim Time As New Queue(Of DateTime)

Private Sub Form9_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

If e.KeyCode = Keys.K Then

While Time.Count <> 0 AndAlso (Now - Time.Peek > New TimeSpan(0, 0, 40))

Time.Dequeue()

End While

If Time.Count < 10 Then

Time.Enqueue(Now)

butB_Click(sender, New System.EventArgs)

End If

End If

End Sub

Crazypennie  Sunday, November 29, 2009 9:00 PM

That was more than helpful!!! I havereally appreciated your precious help. I wish I could help you in any way! I'll program the computers at Univ tomorrow first thing..and I think that in the future I will also need your help...Have a nice night!!!

agg.ioa  Sunday, November 29, 2009 9:32 PM

You can use google to search for other answers

Custom Search

More Threads

• Is that a bug or not?
• Permanently listen and read from a COM port
• Keyboard input after mouse selection of menu or toolbar
• Customized MessageBox that goes to next and previous record
• VS2005 COM DLL
• Algorith to reduce material waste
• Multiple Text Box Lines
• Help on a certain aspect of a game.
• Getting 'Could Not Find File' error when trying to update an Access database (ErrorCode=-2147467259)
• un-highlight a clicked treeview node