I need to make a timer that starts counting at the beginning of a multi-phase delay task, and that ends the delay task after a certain period of time has passed, moving on to the next part of the experiment. For now, I'd like to end the task after 2 seconds have passed.
In the code below, I included an example that can be paste into an editor. I used a part of a Stroop task for this delay task that consists of one phase (in my actual code there are 3 phases, but I simplified the task for this question)-- press the 1 key for red, the 2 key for green, and the 3 key for blue. Each phase currently runs for six trials. (just one set of 6 trials for my one phase for now).
I'd like the task itself (all phases together) to last a period of time, and then terminate at the time I set regardless of the trial number. So if the 2 seconds have passed, the task should end even if we are only on phase 1, trial number 3 of 6.
The code below that is commented out (while loop with NumSecondsStart and NumSecondsEnd) is my current attempt. I'm not sure where such a loop would go (around the phase for loop, around the trial loop?) Thanks!
CODE:
clear all
close all
KbName('UnifyKeyNames');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[window, rect]=Screen('OpenWindow',0);
RED=KbName('1'); GREEN=KbName('2'); BLUE=KbName('3');
keysOfInterest=zeros(1,256);
keysOfInterest([RED, GREEN, BLUE])=1;
KbQueueCreate(-1, keysOfInterest);
KbQueueStart;
WORDCOLORS = {'red', 'green', 'blue'};
rgbColors = [1000 0 0; 0 1000 0; 0 0 1000];
starttime=Screen('Flip',window);
KbQueueFlush;
% NumSecondsStart = GetSecs;
% while (NumSecondsEnd-NumSecondsStart) == 1
for phase = 1
design=repmat([1 2 3], 2, 2)';
if phase == 1
designRand=design(randperm(length(design)),:);
Word=WORDCOLORS(designRand(1:6));
Color=rgbColors(designRand(:,2),:);
end
for trial=1:6
if phase == 1
DrawFormattedText(window, Word{trial}, 'center' ,'center', Color(trial,:));
WaitSecs(rand+.5)
starttime=Screen('Flip',window);
[pressed, firstPress]=KbQueueCheck;
endtime=KbQueueWait();
RTtext=sprintf('Response Time =%1.2f secs',endtime-starttime);
DrawFormattedText(window,RTtext,'center' ,'center',[255 0 255]);
vbl=Screen('Flip',window);
Screen('Flip',window,vbl+1);
NumSecondsEnd =GetSecs;
end
end
end
% break;
% end
ListenChar(0);
ShowCursor();
Screen('CloseAll');