Controlling visual stimuli during audio playback and reading from digital DAQ

I apologise in advance for my only basic understanding of psychtoolbox - I have a lot to learn! I have a script that reads digital inputs from a digital I/O device (NI USB 6501) using a while loop. When it receives an input, it should break out of the while loop to play a sound using PsychPortAudio. This process should be repeated 10 times before it moves to the next part of the code.

My problem is controlling the visual stimuli (the screen textures) displaying during this period. I present a fixation cross before the audio section and I want this to remain on the screen whilst the sounds are playing. Only after it has played its 10 sounds and left that loop should it display the next screen texture. However, on the first trial, it shows the fixation for a few audio presentations before the screen goes black and the windows loading cursor appears whilst the rest of the audio section plays out, and the next texture appears prematurely. For subsequent trials, the next texture appears sometimes almost straight away after the audio section starts.

I am sure I have done something silly - but my understanding was once I had flipped the screen to a texture, it should stay on the screen until I flip again? The audio presentation section is of a variable duration depending on how quickly the digital inputs arrive so I can’t pre-set the duration of the initial fixation stimulus to be shown. I attempted a clunky solution with FinishedBeeps which doesn’t work. I am running PTB 3.0.19 on Windows 10.

Here is the relevant section, simplified:
% display fixation cross
DrawFormattedText(window, ‘+’, ‘center’, ‘center’, black);
Screen(‘Flip’, window);
WaitSecs(4)

%% I/O audio section
FinishedBeeps = 0;
r = read(dq);
for beat=1:10
r.(‘Dev1_port0/line1’)=0;
while r.(‘Dev1_port0/line1’)==0
r = read(dq); %goes to 1 if input detected
end
%input detected
WaitSecs(0.100);
beeptime = PsychPortAudio(‘Start’, pahandle,1);
write(dq,0); % output a logic 1 on port 0, line 0.
WaitSecs(0.02);
write(dq,1); % output a logic 0 on port 0, line 0.
end
FinishedBeeps = 1;

% I/O audio section end

% display new visual stimulus, only once I/O audio section finished
if FinishedBeeps == 1
WaitSecs(2)
DrawFormattedText(window, 'synchronous: up arrow ',‘center’,300,black); %participants give response here
DrawFormattedText(window, ‘NOT synchronous: down arrow’,‘center’,500,black);
Screen(‘Flip’, window);
KbCheck

Hmm. I’m unfamiliar with the syntax used for controlling your DAQ, but the PsychPortAudio Start command won’t wait for sound to actually start - and doesn’t return a valid ‘beeptime’, the way you coded it, without the ‘waitForStart’ flag set to 1. Also you’d use the ‘Stop’ function to wait for end of playback, so maybe your 10 beep tone loop is running too fast and not waiting properly for sound to actually play out properly etc.? Do you actually hear 10 discrete sounds with timing as expected?

The stimulus image indeed stays static on the screen until the next Flip, unless there is some malfunction or really weird thing going on. You don’t use any textures in your sample code btw., you just show text.

For up to 30 minutes of my time for further questions or diagnosis, please post your paid support authentication token from help PsychPaidSupportAndServices. And a more detailed description of your hardware + software setup, and maybe a complete, minimal, self-contained script that demonstrates the problem.