Jump to content
Sign in to follow this  

Measuring reaction-time: How to know if results are accurate?

Recommended Posts


I'm working on an AutoIt program that will be used in a psychological study to measure a person's reaction time.  I have a draft of the program completed, and it seems to work.  In other words, the reaction-time data is plausible.

The program displays a random digit between 2 and 8 inclusive (this is called the "target digit").  The user has to hit the corresponding key as quickly as possible.  The program measures the amount of time it takes the user to hit the key.  There are multiple trials per session, and an average reaction-time is calculated at the end of the session.

Here are the two critical AutoIt functions that perform the actual timing. (In the first function, I edited-out some lines of code that display the target digit in a GUI):

Func _PresentProblemAndGetResponse($sProblem, $sAnswer, $hGui)Local $iReactionTime
Local $iCorrect
Local $sLocalTime
Local $avResults[4]
[Edited-out the code that displays the target key in a GUI]
$hBegin = TimerInit() ; Start the stopwatch running
$iCorrect = _GetKeyPress($sAnswer) ; The parameter here is the correct answer (i.e., the solution to the problem). A returned value of 0 means user pressed wrong key; 1 means correct key.
$iDiff = Round(TimerDiff($hBegin)) ; Stop the stopwatch and get elapsed time in milliseconds
$iReactionTime = $iDiff ; This is the user's reaction time in milliseconds
$sLocalTime = _GetTime() ; This is the user's current local time
GUICtrlDelete($hLabelMessage) ; Erase the problem from the GUI
$avResults[0] = $sProblem ; Assign the relevant data to a vector
$avResults[1] = $iReactionTime
$avResults[2] = $iCorrect
$avResults[3] = $sLocalTime
Return ($avResults)
EndFunc   ;==>_PresentProblemAndGetResponse

Func _GetKeyPress($iRightAnswer)
Local $iRightAnswerCode = 30 + $iRightAnswer ; Convert the answer into the ASCII code that's required by the _IsPressed function
Local $iWrongAnswers[10] = [30, 31, 32, 33, 34, 35, 36, 37, 38, 39] ; Initialize array of wrong answer digits
$iWrongAnswers[$iRightAnswer] = '20' ; Replace the right answer with the code for the space character
While 1
For $x = 0 To 9
If _IsPressed($iRightAnswerCode) Then Return (1)
If _IsPressed($iWrongAnswers[$x]) Then Return (0)
EndFunc   ;==>_GetKeyPress


So, I have two questions.  The second question is much harder, I think.

First of fall, does my approach seem like a reasonable way to measure the time between displaying a target number and registering a user's keypress?  If not, what's a better approach?

Second, how can I verify that the results are accurate?  I've run the program over a hundred times (using myself as the subject), and the results are about what I would expect.  Actually, my main interest lies in measuring small changes that take place over the course of many sessions performed over weeks or months.  The absolute precision isn't terribly important, but consistency is very important.  In other words, if the results are high by 5%, that's not too much of a problem, as long as they are consistently high by 5%.

Thanks for considering my questions.


Share this post

Link to post
Share on other sites

Hi Alex, this could be a way to get the response time of a digit from the screen.


Global $Correct = False, $Counter = 1, $Max = 10, $Random, $Last, $Total

$hMain = GUICreate("Reaction Time Measure")
$hDigit = GUICtrlCreateLabel('',100,0,200,50,0x01)
$hResponseTime = GUICtrlCreateEdit('The test will display ' & $Max & ' digits, one digit each time. You have to press the button of the digit you see it.',0,200,400,150,0x0800)
GUICtrlSetFont($hDigit,30,900,1,'Arial Black')
$hButton = GUICtrlCreateButton("Start test",125,360,150,35)

While True
    Switch GUIGetMsg()
        Case -3 ;GUI_EVENT_CLOSE
        Case $hButton   ; Test will start

Func Test()
            $Random = Random(1,9,1)
        Until $Random <> $Last
        $Timer = TimerInit()
        Until $Correct
        $Diff = TimerDiff($Timer)
        $Correct = False
        $Counter += 1
        $Total += $Diff
        $Last = $Random
        GUICtrlSetData($hResponseTime,Round($Diff,3) & @CRLF,1)
    Until $Counter = $Max
    GUICtrlSetData($hResponseTime,"Average response time: " & Round($Total/$Max,3),1)

Func Correct()
    If @HotKeyPressed = $Random Then $Correct = True

Usually, native functions are faster then UDFs, so I like more this way. But if you still want to use _IsPressed be sure you open user32.dll and use the handle as second parameter of the function otherwise the dll will be opened every time you call the function. This may lead to different response time and for a test like yours this may be relevant.

My example is raw but can be improved to work as you like. Hope this was helpful in any way.

When the words fail... music speaks

Share this post

Link to post
Share on other sites

Hello AndreiK,

Thank you for the detailed answer.  I will try your suggestions this weekend.  For now, I'll mark the question as answered, although I think I'll eventually post again about my second question (regarding precision / accuracy in the results).

Thanks again.

-- Alex Chernavsky

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account


Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Can we pause and resume the download in the InetGet function?
      If is possible, what is the solution please?
      I used this code To manage the download

    • VollachR
      By VollachR
      Can you suggest the best way to do what I need?
      BTW, the files in Rows 3-11 will usually have blank value for any row above the number in Row2 (e.g. Row2 = 5 so Rows3-6 will have values but 8-11 be empty), The values I need are in Column 1 of the array, the name of the key from the INI file that the array was created from is in Column 0.
      Full Example:
      Row2 of Array:
      Col0 = Games# - Col1 = 5
      Col0 = Exe2 - Col1 = Path To File
      Col0 = Exe3 - Col1 = Path To File
      Col0 = Exe4 - Col1 = Path To File
      Col0 = Exe5 - Col1 = Path To File
      I need that if Row2 is 5 to check these above for rows if the file exists, if it was 6 then the next row as well and so on up until number 10 in Row2 as it can't go above 10.
      So basically for whatever number in Row2 from 2-10 need to check 1-9 rows from 3-11 to see if the files in Col1 exists and if any of them don't exist it should call a function that shows an error message.
      I'm pretty sure I have the first line of the for look correct:
      For $i = 1 To $aAIO[2][1] Just not sure how to continue from there, also not sure if $i should be equal 1 or 2.
      Help will be appreciated.
    • VollachR
      By VollachR
      It works fine, excepts it only gives an error if one or more of the sections in the custom array is missing, if they all exist but there are other additional sections that shouldn't be there it doesn't give an error and continue to open the editor screen.
      Here are the relevant part of my script:
      The Custom Verification Arrays:
      ;Creating Arrays for INI Verification Global $aSettingsVerify[7] $aSettingsVerify[1] = "Conversion" $aSettingsVerify[2] = "AIO" $aSettingsVerify[3] = "Data" $aSettingsVerify[4] = "Redist" $aSettingsVerify[5] = "Split" $aSettingsVerify[6] = "Autorun" Global $aDataVerify[6] $aDataVerify[1] = "Compression" $aDataVerify[2] = "Exclude" $aDataVerify[3] = "LangExclude" $aDataVerify[4] = "PreCommands" $aDataVerify[5] = "PostCommands" The IniReadSectionNames verification part:
      Case $OK If StringInStr($ActiveConfig, "Settings.ini") Then $CheckArray1 = IniReadSectionNames($ActiveConfig) $CheckArray2 = _ArrayCompare($aSettingsVerify, $CheckArray1) $CheckArray3 = UBound($CheckArray2, 1) If $CheckArray3 = 1 Then GUIDelete($ConfigStart) SettingsGUI() EndIf If $CheckArray3 > 1 Then WrongIni() EndIf EndIf If StringInStr($ActiveConfig, "Data") Then $CheckArray1 = IniReadSectionNames($ActiveConfig) $CheckArray2 = _ArrayCompare($aDataVerify, $CheckArray1) $CheckArray3 = UBound($CheckArray2, 1) If $CheckArray3 = 1 Then GUIDelete($ConfigStart) DataGUI() EndIf If $CheckArray3 > 1 Then WrongIni() EndIf EndIf The Function that performs the compare: (Found it in another thread on this forums somewhere by using Google Search)
      ;Compares Imported INI file with the Verification array Func _ArrayCompare(ByRef $a1, ByRef $a2) Local $nOldSize = UBound($a2) Local $a3[$nOldSize], $nNewSize = $nOldSize For $i = 0 To UBound($a1) - 1 For $j = 0 To $nOldSize - 1 If Not $a3[$j] And ($a1[$i] = $a2[$j]) Then $a3[$j] = 1 $nNewSize -= 1 EndIf Next Next Local $a4[$nNewSize], $j = 0 For $i = 0 To $nOldSize - 1 If Not $a3[$i] Then $a4[$j] = $a2[$i] $j += 1 EndIf Next Return $a4 EndFunc ;==>_ArrayCompare  
      Any way to make a check if there are sections other than the ones in the Verification Array and produce an error?
    • Valnurat
      By Valnurat
      Can you explain why my mouseclick don't work in my shockwave fullscreen.
      I know my syntax is ok, because it moves to coordinates, but this will only press 1 time, even that I want to press 5 times.
      How can that be?
    • nacerbaaziz
      By nacerbaaziz

      I have a question please
      How to show Islamic date in Autoit
      I mean
      for example
      Ramadan month
      And moharam month ... etc
      I hope my question is clear for you
      Thanks in advance