# Trouble counting responses in instrumental learning script

**URL:** https://psychtoolbox.discourse.group/t/trouble-counting-responses-in-instrumental-learning-script/1945
**Category:** Uncategorized
**Created:** [November 5, 2018, 11:19pm UTC](https://psychtoolbox.discourse.group/t/trouble-counting-responses-in-instrumental-learning-script/1945 "2018-11-05T23:19:56Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![lmnels22](https://avatars.discourse-cdn.com/v4/letter/l/aca169/32.png) [@lmnels22](https://psychtoolbox.discourse.group/u/lmnels22)
#### Post date: [November 5, 2018, 11:19pm UTC](https://psychtoolbox.discourse.group/t/trouble-counting-responses-in-instrumental-learning-script/1945/1 "2018-11-05T23:19:56Z")

</div>

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()

&nbsp;

InputDataStruct = PITGUI();

&nbsp;

subn = InputDataStruct.subn;

subname = InputDataStruct.subname;

subsex = InputDataStruct.subsex;

subage = InputDataStruct.subage;

nblocks = InputDataStruct.nblocks;

subnote = InputDataStruct.subnote;

isfixed = InputDataStruct.isfixed;

filename = InputDataStruct.filename;

&nbsp;

&nbsp;

% Setup PTB with some default values

PsychDefaultSetup(2);

&nbsp;

% Experimental parameters

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

&nbsp;

&nbsp;

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;

&nbsp;

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

% Login prompt and open file for writing data out

&nbsp;

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,...

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

&nbsp;

&nbsp;

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

% &nbsp; 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;

&nbsp;

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

% &nbsp; 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);

&nbsp;

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

&nbsp;

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

% &nbsp; 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);

&nbsp;

while KbCheck; end;

&nbsp;

while 1

&nbsp; &nbsp; keyIsDown = 0;

&nbsp; &nbsp; while ~keyIsDown

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

&nbsp; &nbsp; &nbsp; &nbsp; WaitSecs(0.001);

&nbsp; &nbsp; end

&nbsp; &nbsp; if keyCode(w)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCursor;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose(outfile);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('CloseAll');

&nbsp; &nbsp; end&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; if keyCode(spaceKey)

&nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; end&nbsp;

end

WaitSecs(0.3);

&nbsp;

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

% &nbsp; Trial loop

&nbsp;

&nbsp;

Screen('FillRect', mainwin, trialcolor);

starttime = Screen('Flip', mainwin);

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

&nbsp;

while VBLTimestamp-starttime \< 1000

&nbsp;

KbReleaseWait();

&nbsp;

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

&nbsp;

&nbsp;

while KbCheck; end;

&nbsp;

while 1

&nbsp; &nbsp; keyIsDown = 0;

&nbsp; &nbsp; while ~keyIsDown

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

&nbsp; &nbsp; &nbsp; &nbsp; WaitSecs(0.001);

&nbsp; &nbsp; end

&nbsp; &nbsp; if keyCode(w)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowCursor;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose(outfile);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('CloseAll');

&nbsp; &nbsp; end&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; if keyCode(spaceKey)

&nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; end&nbsp;

&nbsp; &nbsp; if keyCode(Key1)

&nbsp; &nbsp; &nbsp; &nbsp; O1count = O1count + 1;

&nbsp; &nbsp; end&nbsp;

&nbsp; &nbsp; if keyCode(Key2)

&nbsp; &nbsp; &nbsp; &nbsp; O2count = O2count + 1;

&nbsp; &nbsp; end&nbsp;

&nbsp; &nbsp; if keyCode(Key3)

&nbsp; &nbsp; &nbsp; &nbsp; O3count = O3count + 1; &nbsp;

&nbsp; &nbsp; end

&nbsp; &nbsp; KbReleaseWait();

end

&nbsp;

keyCode(1:128) = 0;

&nbsp;

disp('Counts');&nbsp;

disp(O1count);&nbsp;

disp(O2count);&nbsp;

disp(O3count);

&nbsp;

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

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

&nbsp; &nbsp; outcomeonset = Screen('Flip', mainwin);

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

&nbsp; &nbsp; totalO1 = totalO1 + 1;

&nbsp; &nbsp; O1count = 0;

end

&nbsp;

if O2count == 9

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

&nbsp; &nbsp; outcomeonset = Screen('Flip', mainwin);

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

&nbsp; &nbsp; totalO2 = totalO2 + 1;

&nbsp; &nbsp; O2count = 0;

end&nbsp;

&nbsp;

if O3count == 9

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

&nbsp; &nbsp; outcomeonset = Screen('Flip', mainwin);

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

&nbsp; &nbsp; totalO3 = totalO3 + 1;

&nbsp; &nbsp; O3count = 0;

end&nbsp;

&nbsp;

%Experiment ends when each outcome has been delivered 20 times

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

&nbsp; &nbsp; fclose(outfile);

&nbsp; &nbsp; Screen('CloseAll'); &nbsp; &nbsp;

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

elseif totalO1 == 20

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

&nbsp; &nbsp; finishO1onset = Screen('Flip', mainwin);

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

elseif totalO2 == 20

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

&nbsp; &nbsp; finishO2onset = Screen('Flip', mainwin);

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

elseif totalO1 == 20

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

&nbsp; &nbsp; finishO3onset = Screen('Flip', mainwin);

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

end&nbsp;

KbReleaseWait();

end

end
