Hi, I'm new to both bpod and coding in general so please bear with me if this is an obvious question. I'm currently running a pretty simple cue conditioning script with my setup. However, one thing I would like to add to it is a global timer that will start at the beginning of the 1st trial (tied to trigger a timed PWM2 LED flash at consistent intervals) and will persist across trial loops.
As it stands currently, the problem I'm running into is that my GlobalTimer gets reset every time it hits 'sma = NewStateMachine();' on a new loop. So for example, if my LED is flashing at 3 second intervals, and a flash happens 1 second before the trial end, then another flash will happen 1 second later when the trial loop restarts. Ideally, I want the flash to be constant, so in the case of a 3 second flash interval, if a flash happens 1 second before trial 1 end, then I would like the next flash to happen 2 seconds into the start of trial 2.
I've tried a couple workarounds. I tried to configure the timer in the non-looping "setup phase" of the code as follows:
% LED FLASH CONFIG
if S.GUI.LEDFlashEnable
LED = PIB_LED_Flash(S.GUI.LEDFlashInterval);
sma = NewStateMachine();
sma = LED.ConfigureTimers(sma); % programs LoopMode, Interval, PulseWidth, etc
sma = AddState(sma, 'Name','StartTimer','Timer',0.01, ...
'StateChangeConditions',{'Tup','exit'}, ...
'OutputActions', LED.TriggerAction);
SendStateMachine(sma); RunStateMachine;
end
%% Main loop (runs once per trial)
for currentTrial = 1:maxTrials
S = BpodParameterGUI('sync', S); % Sync parameters with BpodParameterGUI plugin .......... etc.
But this would cause only a single flash to go off at the start, presumably because once it hits the 'sma = NewStateMachine();' of the main loop it gets overwritten. I also tried to configure and start the timer at the beginning of the first trial loop only:
%--- Assemble state machine
sma = NewStateMachine();
LED = PIB_LED_Flash(S.GUI.LEDFlashInterval);
sma = LED.ConfigureTimers(sma); % programs LoopMode, Interval, PulseWidth, etc
startActions = {'DIO1',28};
if S.GUI.LEDFlashEnable && currentTrial == 1
startActions = [startActions, LED.TriggerAction];
end
sma = AddState(sma, 'Name', 'PreOdor', ...
'Timer', S.GUI.PreOdor,...
'StateChangeConditions', {'Tup', odor_name},...
'OutputActions', startActions);
sma = AddState(sma, 'Name', odor_name, ...
'Timer', S.GUI.Odor,...
'StateChangeConditions', {'Tup', 'Trace'},...
'OutputActions', {'ValveModule1',valve_command,'DIO1',arduino_command}); .......... etc.
This caused my LED flash timer to work as intended for the first trial loop, but once the trial ends and loops back to the start it stops working, presumably because it hits 'sma = NewStateMachine();' again.
So my question is, is it possible to make a Global Timer that won't be reset between loops? Is there a fix I'm not thinking of? Or would it be better to run a persistent Global Timer through a third party instead of the bpod?
As a beginner I appreciate any and all input. Also, if preferred, I can send my entire cue conditioning code, as well as my LED flash code if I left out any additional info that would be helpful. Thanks for the taking the time to read this!
Ryan