Sunday, November 20, 2011

PEBL Programming Tutorial III: collecting responses

This is Part III of a series of tutorials on PEBL
First  |   Previous   | Next

The previous tutorial described how to display basic text stimuli on the screen.  Most experiments require both a stimulus and a response.  The simplest way to collect responses is via the computer keyboard.  PEBL offers some simple response collection functions that make this easy to do.

So, picking up where we left off, here is a simple one-function experiment that creates a window, adds a label giving basic instructions, and then waits for the subject to press a key 1 through 7 on the keyboard.

define Start(p)
{
    win <-MakeWindow() 
    stim <- EasyLabel("Rate on a scale of 1 to 7 how good you feel today",
                                    400,300,win,18) 
     Draw()
     resp <- WaitForListKeyPress(["1","2","3","4","5","6","7"]) 
     stim.text <- "response was: " + resp

     Draw()
     WaitForAnyKeyPress()
}


It looks like this:












This is pretty simple, but really forms the basis for a lot of possible data input.  You might not want to give people unlimited time to make a response though.  10 seconds should be enough for this question.  To do this, you can use the function WaitForListKeyPressWithTimeout.  The code would look like this:


define Start(p)
{
    win <-MakeWindow() 
    stim <- EasyLabel("Rate on a scale of 1 to 7 how good you feel today",
                                    400,300,win,18) 
     Draw()
     time1 <- GetTime()
     resp <- WaitForListKeyPressWithTimeout(["1","2","3","4","5","6","7"],10000) 
     time2 <- GetTime()
     if(IsList(resp))
      {
         resp <- "No response made"
      }
     stim.text <- "response was: [" + resp + "]  RT was : ["+ (time2-time1) +"ms."

     Draw()
     WaitForAnyKeyPress()
}

There are a few changes required here.  First, there is a second argument to WaitForListKeyPress, which specifies how long, in milliseconds, you will give the participant.  In PEBL, time is almost always measured in 1/1000 of a second.  A value of 10000 means 10 seconds.

The function will return the character pressed, but if the time limit is reached, it will return a list that contains the text "":  [""].  Thus, the simplest way to see if they missed the time limit is to see if a list was returned, thus the check for IsList().  

Finally,  we might want to know how long the response actually took.  The function GetTime() will return the counter indicating the state of the computer's real-time clock.  If we use it to record the time before and after the response collection, we can get the RT by subtraction.

No comments: