Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[open] Log response time of button press via serial port

edited July 2014 in OpenSesame

Hello,
we are dealing with a staircase paradigm in the fMRI scanner and we record subject's responses through a serial port. We use PySerial to set the serial port and log the responses. Responses are mostly given while the current stimuli are still being presented. After they are presented we can read the serial port. OS correctly records the response button identity, and uses it to set subsequent stimuli presentations (following the logic of our staircase). However, we are not able to log the actual time of button press. We checked PySerial documentation to see whether there is a function that logs the instant in which the serial port receives a bit ( = a subject's button press), but there is none.
Do you have any suggestion on how to approach this issues?
Thanks, Stefania and Ambra

Comments

  • edited 4:25AM

    Hi Stefania and Ambra,

    Assuming that your button box simply sends a response as soon as a press has been detected, you can just use OpenSesame's self.time() function to get the time immediately after you have collected the response, like so:

    # Your response collection code here
    response_timestamp = self.time()
    exp.set('response_timestamp', response_timestamp)
    

    This will log the timestamp of the response, not the response time relative to some other event, such as the presentation of a sketchpad. But you can deduce the response time easily, simply subtracting the timestamp of the starting event from the timestamp of the response.

    Cheers!
    Sebastiaan

  • edited 4:25AM

    Hi Sebastiaan,
    thank you for your immediate response, that we really appreciated.

    However, if the subject presses the button while the stimulus is still being presented (60 frames), the serial port receives a response during the loop. We only read the response after that loop, so this means that self.time() does not necessarily report the precise time of button press (i.e. when it occurred during the loop).

    We probably need a parallel pooling loop that reads the serial port (and uses self.time() to get the actual time of presses) inside the presentation loop. Is that correct? again, any suggestions will be very welcome.

    Thanks, Stefania and Ambra

  • edited 4:25AM

    Hi guys,

    Actually, you could do this in a serial fashion, rather than using the more complicated parallel threading approach. Place the following code in an inline_script placed directly behind the sketchpad that you use to present your stimulus, and set the duration of that sketchpad to 0.

    # get the starting time
    t0 = self.time()
    
    # determine the presentation time in milliseconds
    # (= Nframes * (1/refresh rate in Hz) * 1000 - half a frame time)
    Nframes = 60 # frames
    refrate = 60 # Hz
    frametime = (1.0 / refrate) * 1000
    prestime = int(Nframes * frametime - (frametime/2))
    
    # run until the presentation time passes or a response is made
    response = None
    while self.time() - t0 < prestime and response == None:
        # get the button response
        response = # PLACE YOUR CODE HERE
        # check if there is a response
        if response != None:
            # set the OpenSesame response variables
            exp.set('response_timestamp', t0)
            exp.set('response', response)
            # save a timestamp
            t1 = self.time()
    
    # pause for the remainder of the stimulus duration
    if response != None:
        self.sleep(prestime - (t1-t0))
    

    Does that work for you?

  • edited 4:25AM

    First of all thanks for putting out this great program! Quick question: in PsychoPy it is possible to start recording keypresses at certain frames. My experiment uses a paradigm where I need a 'beep' played at a certain frame (the first after an edit) and the participant will then press a button, the RT time of which also needs to be recorded. How do I anchor the keypress, the recording, and the sound to a certain frame?

  • edited 4:25AM

    Hi Xandra,

    My experiment uses a paradigm where I need a 'beep' played at a certain frame (the first after an edit) and the participant will then press a button, the RT time of which also needs to be recorded. How do I anchor the keypress, the recording, and the sound to a certain frame?

    That really depends on how your experiment is structured. What does your trial look like exactly and how have you implemented it at the moment?

    Cheers,
    Sebastiaan

Sign In or Register to comment.