Trouble counting responses in instrumental learning script

I'm running into trouble collecting keyboard inputs for two psychological tasks, a pavlovian and instrumental learning script. I'll show the instrumental script in hopes that solving the problem in the instrumental script will transfer to the Pavlovian script. I'm running on a 2018 MacBook Pro with Touch Bar, High Sierra.

What I want is for the window to close if I press escape, and during the trial loop, count the number of left, up, and right arrow keypresses so the outcome image is shown when the key is pressed nine times. But the blue window just stays on the screen and is unresponsive to keyboard input.


Here's the code. Thank you for your help!


function Instrumental()

 

InputDataStruct = PITGUI();

 

subn = InputDataStruct.subn;

subname = InputDataStruct.subname;

subsex = InputDataStruct.subsex;

subage = InputDataStruct.subage;

nblocks = InputDataStruct.nblocks;

subnote = InputDataStruct.subnote;

isfixed = InputDataStruct.isfixed;

filename = InputDataStruct.filename;

 

 

% Setup PTB with some default values

PsychDefaultSetup(2);

 

% Experimental parameters

Screen('Preference', 'SkipSyncTests', 1);

 

 

KbName('UnifyKeyNames');

Key1=KbName('LeftArrow'); Key2=KbName('RightArrow'); Key3 = KbName('UpArrow');

spaceKey = KbName('space'); escKey = KbName('ESCAPE'); w = KbName('w');

gray = [127 127 127 ]; white = [ 255 255 255]; black = [ 0 0 0]; blue = [0 0 200];

bgcolor = white; trialcolor = blue; textcolor = black;

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Login prompt and open file for writing data out

 

fname1 = sprintf('%s', filename);

fname2 = '.txt';

fname = strcat(fname1,fname2);

outfile = fopen(fname, 'w')

fprintf(outfile, '%i %s %s %i %i %s \n', InputDataStruct.subn, InputDataStruct.subname,...

    InputDataStruct.subsex, InputDataStruct.subage, InputDataStruct.nblocks, InputDataStruct.subnote);

 

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%   Screen parameters

[mainwin, screenrect] = Screen(0, 'OpenWindow');

Screen('FillRect', mainwin, bgcolor);

center = [screenrect(3)/2 screenrect(4)/2];

Screen(mainwin, 'Flip');

slack = Screen('GetFlipInterval', mainwin)/2;

ifi = 1/Screen('FrameRate', mainwin);

stimDuration = 20*ifi;

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%   load images

im = imread('Outcome10kronor.jpg'); outcome1 = Screen('MakeTexture', mainwin, im);

im = imread('Outcome10rand.jpg'); outcome2 = Screen('MakeTexture', mainwin, im);

im = imread('Outcome10francs.jpg'); outcome3 = Screen('MakeTexture', mainwin, im);

im = imread('NoOutcome.jpg'); outcome4 = Screen('MakeTexture', mainwin, im);

 

alloutcomes = [outcome1, outcome2, outcome3, outcome4];

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%   Experimental instructions, wait for a spacebar response to start

Screen('FillRect', mainwin, bgcolor);

Screen('TextSize', mainwin, 24);

DrawFormattedText(mainwin, 'You must press different buttons to get different outcomes. You can only press \n one of three keys (up, left and right). You can switch between keys as often as you like. \n The computer may eventually tell you to stop pressing a certain key. \n Each ten francs, rand, or kronor can be cashed in at the end of the experiment for $0.50. \n When you see the currency, press the spacebar to "bank" it. \n Press spacebar to start the experiment.', 'center', 'center');

Screen('Flip', mainwin);

 

while KbCheck; end;

 

while 1

    keyIsDown = 0;

    while ~keyIsDown

        [keyIsDown, secs, keyCode] = KbCheck(-1);

        WaitSecs(0.001);

    end

    if keyCode(w)

            ShowCursor;

            fclose(outfile);

            Screen('CloseAll');

    end     

    if keyCode(spaceKey)

        break;

    end 

end

WaitSecs(0.3);

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%   Trial loop

 

 

Screen('FillRect', mainwin, trialcolor);

starttime = Screen('Flip', mainwin);

Screen('Flip', mainwin, starttime +stimDuration -ifi/2);

 

while VBLTimestamp-starttime < 1000

 

KbReleaseWait();

 

O1count = 0; %Counting number of left key responses

O2count = 0; %right key

O3count = 0; %up key

totalO1 = 0; %Counter to represent how many times outcome 1 has been delivered

totalO2 = 0; %Outcome 2

totalO3 = 0; %Outcome 3

 

 

while KbCheck; end;

 

while 1

    keyIsDown = 0;

    while ~keyIsDown

        [keyIsDown, secs, keyCode] = KbCheck(-1);

        WaitSecs(0.001);

    end

    if keyCode(w)

            ShowCursor;

            fclose(outfile);

            Screen('CloseAll');

    end     

    if keyCode(spaceKey)

        break;

    end 

    if keyCode(Key1)

        O1count = O1count + 1;

    end 

    if keyCode(Key2)

        O2count = O2count + 1;

    end 

    if keyCode(Key3)

        O3count = O3count + 1;  

    end

    KbReleaseWait();

end

 

keyCode(1:128) = 0;

 

disp('Counts'); 

disp(O1count); 

disp(O2count); 

disp(O3count);

 

if O1count == 9 %Outcome presented every nine key presses

    Screen('DrawTexture', mainwin, alloutcomes(1), [], []);

    outcomeonset = Screen('Flip', mainwin);

    outcomeoffset = Screen('Flip', mainwin, outcomeonset + stimDuration - ifi/2);

    totalO1 = totalO1 + 1;

    O1count = 0;

end

 

if O2count == 9

    Screen('DrawTexture', mainwin, alloutcomes(2), [], []);

    outcomeonset = Screen('Flip', mainwin);

    outcomeoffset = Screen('Flip', mainwin, outcomeonset + stimDuration - ifi/2);

    totalO2 = totalO2 + 1;

    O2count = 0;

end 

 

if O3count == 9

    Screen('DrawTexture', mainwin, alloutcomes(3), [], []);

    outcomeonset = Screen('Flip', mainwin);

    outcomeoffset = Screen('Flip', mainwin, outcomeonset + stimDuration - ifi/2);

    totalO3 = totalO3 + 1;

    O3count = 0;

end 

 

%Experiment ends when each outcome has been delivered 20 times

if totalO1 == 20 && totalO2 == 20 && totalO3 == 20

    fclose(outfile);

    Screen('CloseAll');    

    fprintf('\n\n\n\n\nFINISHED this part! PLEASE GET THE EXPERIMENTER...\n\n');

elseif totalO1 == 20

    DrawFormattwedText(mainwin, 'The left arrow key will no longer be rewarded, \n so don''t bother pressing it!', [], []);

    finishO1onset = Screen('Flip', mainwin);

    finishO1offset = Screen('Flip', mainwin, finishO1onset + 2 - slack);

elseif totalO2 == 20

    DrawFormattedText(mainwin, 'The right arrow key will no longer be rewarded, \n so don''t bother pressing it!', [], []);

    finishO2onset = Screen('Flip', mainwin);

    finishO2offset = Screen('Flip', mainwin, finishO2onset + 2 - slack);

elseif totalO1 == 20

    DrawFormattedText(mainwin, 'The up arrow key will no longer be rewarded, \n so don''t bother pressing it!', [], []);

    finishO3onset = Screen('Flip', mainwin);

    finishO3offset = Screen('Flip', mainwin, finishO3onset + 2 - slack);

end 

KbReleaseWait();

end

end