Search code examples
octaveresponse-timepsychtoolbox

How to present stimulus for limited amount of time and still record response using Psychtoolbox


For my experiment I want to present a stimulus and some instructions to the participant. Then 2 seconds later I want the stimulus to disappear but the instructions to remain until the participant responds. Participants should be able to respond immediately after the presentation of the stimulus and up to 10 seconds after its presentation. Response time will be recorded.

With my current code participants aren't able to respond until after 2 seconds (after the stimulus disappears). Is there someway for the stimulus to only appear for 2 seconds while the instructions remain on the screen, but participants are able to respond immediately after the presentation of the stimulus?

%Show the instructions and the stimulus   
           Screen('DrawTexture', window, randFaceTexture2);
           DrawFormattedText(window, [instructions1], 'center', 600)

           stimTime = Screen('Flip', window);

           WaitSecs(2);

           %Stimulus disappears but instructions remain 
           DrawFormattedText(window, [instructions1], 'center', 600)
           Screen('Flip', window);

           if GetSecs() <= stimTime + 10
           keyIsDown = 0;
           startTime = GetSecs();
                while 1
                     [keyIsDown, secs, keyCode] = KbCheck;
                     FlushEvents('keyDown');
                     if keyIsDown
                          nKeys = sum(keyCode);
                               if nKeys == 1
                                    if keyCode(yes) || keyCode(no)
                                         reactionTime = 1000*(GetSecs - startTime);
                                         response = KbName(keyCode);
                                         Screen('Flip', window);
                                         break;
                                    elseif keyCode(escKey)
                                         ShowCursor;
                                         fclose(outfile);
                                         Screen('CloseAll');
                                    return 
                                    end 
                                    keyIsDown = 0;
                                    keyCode = 0;
                               end 
                     end 
                end 
           else 
                line3 = 'Sorry you''re out of time';
                DrawFormattedText(window, line3, 'center', 'center');
                Screen('Flip', window);
                keyIsDown = 0;
                rt = 0;
           end

Solution

  • Instead of using WaitSecs() to control the amount of time the stimulus is on screen, turn the stimulus off in the response checking loop, when the appropriate amount of time has elapsed. Here is a snippet that shows the relevant changes to your above code. I omitted the details of how you are polling for response. As an aside I think your use of 'if GetSecs() <= stimTime + 10' might not be accomplishing what you want, in your code this statement would just be evaluated once and will always be true.

    % time in seconds for the stimulus to remain onscreen
    timeForStimulus = 2;
    % time for the participant to respond, in seconds
    timeToRespond = 10;
    
    %Show the instructions and the stimulus
    Screen('DrawTexture', window, randFaceTexture2);
    DrawFormattedText(window, [instructions1], 'center', 600)
    
    stimTime = Screen('Flip', window);
    stimOnScreen = 1;
    
    while GetSecs() <= (stimTime + timeToRespond)
        % turn the stimulus off, if the stim is on and the stim time has elapsed
        if (stimOnScreen == 1) && (GetSecs() > (stimTime + timeForStimulus))
            %Stimulus disappears but instructions remain
            DrawFormattedText(window, [instructions1], 'center', 600)
            Screen('Flip', window);
            stimOnScreen = 0;
        end
    
        % poll for keyboard response, etc.
    
    end