%Stroop.m

strs = {'red','green','blue','yellow'};
cols = {[255,0,0],[0,255,0],[0,0,255],[255,255,0]};


p.responseInterval =1.5; %seconds
p.ITI = 2; %seconds

% Set display parameters
display.dist = 50;  %cm
display.width = 30; %cm
display.skipChecks = 1; %avoid Screen's timing checks and verbosity

n = length(strs);

nReps  =1;

conds = repmat(1:n^2,nReps,1);
conds = shuffle(conds(:));

[r,c] = ind2sub([n,n],conds);

%%

try
    display = OpenWindow(display);
    
    drawText(display,[0,6],'Push key corresponding to the text color',[255,255,255]);
    
    display = drawFixation(display);
    %while KbCheck; end
    KbWait;
    pause(1);
    
    startTime= getSecs;
    for i=1:length(conds)
        drawText(display,[0,0],strs{c(conds(i))},cols{r(conds(i))});
        Screen('Flip',display.windowPtr);
        
        [key,RT] = waitTill(p.responseInterval);
        Screen('Flip',display.windowPtr);
        
    
        
        if length(key) == 1
            response.key(i) = key;
            response.RT(i) = RT;
        else
            response.key(i)  ={' '};
            response.RT(i) = NaN;
        end
        waitTill(i*p.ITI,startTime);
        
    end
    
    
    
catch ME
    Screen('CloseAll');
    rethrow(ME)
end
Screen('CloseAll');


save stroopData p response display

%% data analysis

congruent = r(conds) == c(conds);

correct = zeros(length(conds),1);
for i=1:length(conds)
    correct(i) =  response.key{i} == strs{r(conds(i))}(1);
end

RTc = response.RT(congruent & correct);
RTi = response.RT(~congruent & correct);

figure(1)
clf
subplot(2,1,1) 
hist(RTc,[0:.05:p.responseInterval]);

subplot(2,1,2) 
hist(RTi,[0:.05:p.responseInterval]);
xlabel('RT (sec)');





    
    

