Hi there!

I'm writing a simple psychologial experiment program that has to measure the subjects' reaction time to various stimuli.

In details, it has to present a stimulus, wait for a key press, save the results - then show the next stimulis, and so on.

My problem is that I can't figure how to make the program stop and wait for a keypress.

Here is a skeleton program, for what I've done so far.

Public Class ExperimentProgramSkeleton
' The array with the stimuli:
Dim Stimuli() As Stimulus = ThisExperiment.Stimuli

' The array in wich the results will be stored:
Dim Results() As Result

Sub Experiment_Run() ' Present all the stimuli
For s As Integer = 0 To Stimuli.GetUpperBound(0)
Results(s) = PresentStimulus(Stimuli(s))
End Sub

Function PresentStimulus(ByVal Stim As Stimulus) As Result
Dim ThisKey As New Char
Dim ThisReactionTime As Integer = 0
Dim ThisResult As Result

ShowPicture(Stim.Picture) 'show the stimulus

' I need some code here to _wait_ for a keypress
' response from the subject.
' I also need to know how much
' time has ellapsed between the stimulus and the response
' The code stores these data
' in the 'key' and ReactionTime variables
' ...
' then puts them in the Results array:

ThisResult.KeyPress = ThisKey
ThisResult.ReactionTime = ThisReactionTime
End Function

Sub ShowPicture(ByVal Pict As StimulusPicture)
' Here's a code that puts the stimulus to the screen
End Sub

End Class

So is there a way to get this thing working

Thank you!

Re: Visual Basic General Measuring Reaction Time


Generally a Windows program does not have to stop and wait for a keypress as it is event driven anyway and does not do anything until an event occurs.

Your problem arises because you are thinking of doing it within a For....Next loop and so you need a method for waiting within the loop until something happens. It would be better to try a different approach.

One way is illustrated in the following. After pressing the Start button it displays letters on screen (after a random delay) and then times how long it takes to press the key corresponding to the letter displayed. Most of the code is just for setting up the form.

Public Class Form1
Dim T As New Stopwatch
Dim WithEvents Tmr As New Timer
Dim WithEvents B As New Button
Dim L1 As New Label
Dim L2 As New Label
Dim R As New Random
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If L1.Text = "" Then Exit Sub
If e.KeyValue = Asc(L1.Text) Then
L2.Text = T.ElapsedMilliseconds.ToString & " msecs"
L1.Text = ""
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetBounds(50, 50, 300, 300)
Me.KeyPreview = True
L1.AutoSize = False
L1.SetBounds(125, 100, 50, 50)
L1.Font = New Font("Arial", 24, FontStyle.Bold)
L1.TextAlign = ContentAlignment.MiddleCenter
L2.AutoSize = False
L2.SetBounds(50, 200, 200, 30)
L2.Font = New Font("Arial", 18, FontStyle.Bold)
L2.TextAlign = ContentAlignment.MiddleCenter
L2.BackColor = Color.Yellow
B.SetBounds(100, 50, 100, 20)
B.Text = "Start"
End Sub
Private Sub B_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles B.Click
Tmr.Interval = R.Next(2000, 5000)
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
L1.Text = Chr(R.Next(65, 90))
End Sub
End Class

Just paste the entire code into a new project to try it out.

This could easily be adapted to suit what you are trying to achieve.

Re: Visual Basic General Measuring Reaction Time


Thanks for your post, I've thought abuot a similar solution myself,  but decided not to use it in the long run  becouse of several reasons. The main thing is that the program has to make decisions about the next presented item acording to the responses of the subject at previous stimuli.

As I read my previous post, a can see that I was not so clear at what I really wanted. What I really need is a Function that simulates the effects of the stop_and_wait functions of not event-driven languages (like the INKEY$ function in QuickBasic :D ).

My program is something like a simple IDE for psychological experiments, most of it is ready, and with a similar code like you've posted, I'm already doing experiments for my thesis.

But this way the sequence of the stimuli has to be linear and pre-determined even before the beginning of their presentation, and it is very hard to break out of it.

There has to be some work-around to make this sequence stop and wait for an event to occure (subjects' response) to make decision about the next item. Maybe some multi-threading would do the trick, but I wasn't able to figure out how. :-(


Re: Visual Basic General Measuring Reaction Time


You really don't want that. You want what dave suggested. This is not Quick Basic.

Secondly, after a subject makes a response you have plenty of time to decide what stimulus to select next and present it to the subject. The amount of time required to do that would be truly, truly small.