Arduino digital read into loop
Show older comments
I want to run an experiment in which a TTL signal is sent to Matlab via an Arduino to trigger an audio playback. I want to use the digitalRead function of PsychToolbox as the process is latency critical and this offers the best latency. This is based on the Arduino Legacy from Matlab. See discussion here.
My script presents sound at a 1250 ms interval with 250 ms jitter and records inputs as response time in a for loop. Here is an excerpt:
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
[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
where TrialStart stores the trial start time and the structure blocklog is the predetermined sequence and its associated parameters (ie. ran (trial) and soa (interval between two trials)).
The goal would be to implement continuous polling of a digital pin during the 500 ms interval in which a sound can be presented. If there is a TTL (pin going from 0 to 1, rise) within, say, the first 495 ms, then immidiately trigger sound A or B according to a predetermined sequence. If no TTL, then revert to presenting either sound C or D according to a predetermined sequence.
I am relatively new to Matlab and can't figure out how to do it. I assume this would require a while loop and research of this forum suggested the use of Callbacks but I am not sure how to do it.
Thank you.
Accepted Answer
More Answers (0)
Categories
Find more on Timing and presenting 2D and 3D stimuli in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!