for block = 1:param.nblocks
for trial = 1:ntrials
if trial == 1
TrialStart(block, trial) = GetSecs;
else
TrialStart(block, trial) = TrialStart(block, trial-1) + blocklog(block).ran(trial-1).soa;
end
% Clear any pending events from previous trials
while KbCheck; end
% Start the polling loop
ttlDetected = false;
startTime = GetSecs;
while (GetSecs - startTime) < 0.5
% Poll the digital pin using digitalRead
ttlValue = digitalRead(arduino, pinNumber);
% Check if TTL is detected within the first 495 ms
if (GetSecs - startTime) < 0.495 && ttlValue == 1
ttlDetected = true;
break;
end
end
% Determine which sound to present based on TTL detection
if ttlDetected
% Play sound A or B according to predetermined sequence
soundIndex = getSoundIndexForTrial(block, trial); % Replace with your own logic
playSound(TheSnd(soundIndex), pahandle);
else
% Play sound C or D according to predetermined sequence
soundIndex = getSoundIndexForTrial(block, trial); % Replace with your own logic
playSound(TheSnd(soundIndex), pahandle);
end
[data] = present(blocklog, block, trial, TrialStart, TheSnd, pahandle, param, ntrials, key);
data.correct = correct(data);
subjdata = [subjdata; struct2array(data)];
end
block_feedback (scr, param, subjdata, block, img);
end
In this modified script, the while loop continuously polls the digital pin using the digitalRead function until either a TTL is detected within the first 495 ms or the 500 ms interval ends. Based on the TTL detection, the appropriate sound is played using the playSound function.
Make sure to replace 'pinNumber' with the actual pin number you are using on the Arduino, and implement your own logic to determine the sound index for each trial based on your predetermined sequence.
Thank you.