Hello Ke,
Here's my suggested approach:
-Capture rotary encoder data via USB instead of the microSD card. Relevant commands are:
startUSBStream()
data = readUSBStream()
stopUSBStream()
as documented here.
This will allow you to use the '#' command passed from the state machine, to add an event timestamp to the rotary encoder data.
-Run a single trial on the state machine, for the duration of your experiment. Make sure the trial ends when your experiment ends, so your trial data will be saved.
-In the 'Output Actions' section of the first state, trigger two looping global timers. The first timer is attached to BNC1 --> Photometry System. The second timer (in sync with the first) sends the '#' command to the rotary encoder. Each timer loop is also timestamped in your Bpod behavior data, so this repeating event will be timestamped on all 3 systems. Both timers loop in sync at 10Hz, more than sufficient to account for any clock drift.
LoadSerialMessages('RotaryEncoder1', {['#' 1]}); % Set Message 1 as the REM command to sync
sma = NewStateMachine;
sma = SetGlobalTimer(sma, 'TimerID', 1, 'Duration', 0.05, 'OnsetDelay', 0,...
'Channel', 'BNC1', 'OnLevel', 1, 'OffLevel', 0,...
'Loop', 1, 'SendGlobalTimerEvents', 1, 'LoopInterval', 0.05);
sma = SetGlobalTimer(sma, 'TimerID', 2, 'Duration', 0.05, 'OnsetDelay', 0,...
'Channel', 'RotaryEncoder1', 'OnMessage', 1, 'OffMessage', 0,...
'Loop', 1, 'SendGlobalTimerEvents', 1, 'LoopInterval', 0.05);
sma = AddState(sma, 'Name', 'TimerTrig', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'MyNextState'},...
'OutputActions', {'GlobalTimerTrig', '11'}); % Binary strings can trigger multiple timers
Please let me know if this works for you!
-Josh