Hi,
My code takes 3 min 28 s to run after pressing the start button. May I ask for your help to reduce the launch time please?
Thanks in advance!
function [sma, S] = PrepareStateMachine(S, TrialTypes, currentTrial)
% Sync parameters with BpodParameterGUI plugin
S = BpodParameterGUI('sync', S);
% Randomized withhold period (2-4 s/8-12 s/90-120s)
withhold2 = 8+round((12-8)*rand(1)); %ITI
switch TrialTypes(currentTrial) % Determine trial-specific state matrix fields
case 1
cue_time = 30; cue = {'HiFi1',1}; reward_time = 0.5; withhold = withhold1;
case 2
cue_time = 10; cue = {'HiFi1',1}; reward_time = 0.5; withhold = withhold2;
end
% Assemble state machine
sma = NewStateMachine();
sma = SetGlobalTimer(sma, 'TimerID', 1,'Duration', cue_time); % set cue period
sma = SetGlobalTimer(sma, 'TimerID', 2,'Duration', 0.2); % set time between 1.5mm and 3mm to avoid holding/slowly press
sma = SetGlobalTimer(sma, 'TimerID', 3,'Duration', 600); % for LEDoff period
sma = SetGlobalTimer(sma, 'TimerID', 4,'Duration', 110); % preparation for LEDoff
sma = SetGlobalTimer(sma, 'TimerID', 5,'Duration', 600); % LED (same as GlobalTimer3 but for LED period)
sma = AddState(sma, 'Name', 'start_trial', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'LEDoff'},...
'OutputActions', {'AnalogIn1',1});
sma = AddState(sma, 'Name', 'LEDoff', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'timer4'},...
'OutputActions', {'GlobalTimerTrig',3}); % GlobalTimerTrig has a bug..Do not trigger it together with any other actions!!!
sma = AddState(sma, 'Name', 'timer4', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'ITI'},...
'OutputActions', {'GlobalTimerTrig',4});
sma = AddState(sma, 'Name', 'ITI', ...
'Timer', withhold2,...
'StateChangeConditions', {'GlobalTimer5_End','stop','GlobalTimer3_End', 'block_end','GlobalTimer4_End', 'ITI2','Tup', 'cue'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'ITI2', ...
'Timer', 10,...
'StateChangeConditions', {'GlobalTimer3_End', 'block_end','GlobalTimer5_End', 'stop','Tup', 'block_end'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'cue', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'timer'},...
'OutputActions', cue);
sma = AddState(sma, 'Name', 'timer', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'press'},...
'OutputActions', {'GlobalTimerTrig',1});
sma = AddState(sma, 'Name', 'press', ... % ends from here means no cross-threshold mov at all, give white noise
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer1_End', 'miss', 'AnalogIn1_1', 'press_threshold2'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'press_threshold2', ... % no white noise even pass the 1st threshold
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer1_End', 'miss','AnalogIn1_2', 'hit','GlobalTimer2_End','press'},...
'OutputActions', {'GlobalTimerTrig',2});
sma = AddState(sma, 'Name', 'hit', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'reward'},...
'OutputActions', {'HiFi1',4});
sma = AddState(sma, 'Name', 'reward', ... % Open solenoid valve
'Timer', S.GUI.RewardAmount,...
'StateChangeConditions', {'Tup', 'reward_time','GlobalTimer3_End', 'block_end','GlobalTimer4_End', 'ITI2','GlobalTimer5_End', 'stop'},...
'OutputActions', {'Valve1',1});
sma = AddState(sma, 'Name', 'reward_time', ...
'Timer', reward_time,...
'StateChangeConditions', {'GlobalTimer3_End', 'block_end','GlobalTimer4_End', 'ITI2','GlobalTimer5_End', 'stop','Tup', 'ITI'},...
'OutputActions', {});
sma = AddState(sma, 'Name', 'block_end', ... % end of LEDoff/LED block
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer5_End','stop','Tup', 'timer4_2'},...
'OutputActions', {'WavePlayer1',3});
%'OutputActions', {'HiFi1',4,'AnalogIn1',2,'WavePlayer1',2});
sma = AddState(sma, 'Name', 'timer4_2', ...
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer5_End','stop','Tup', 'LED'},...
'OutputActions', {'GlobalTimerTrig',4});
sma = AddState(sma, 'Name', 'LED', ...
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer5_End','stop','Tup', 'timer5'},...
'OutputActions', {'WavePlayer1',2});
sma = AddState(sma, 'Name', 'timer5', ...
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer5_End','stop','Tup', 'ITI'},...
'OutputActions', {'GlobalTimerTrig',5});
sma = AddState(sma, 'Name', 'miss', ... % white noise punishment
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer3_End','block_end','GlobalTimer5_End','stop','GlobalTimer4_End', 'ITI2','Tup', 'ITI'},...
'OutputActions', {'HiFi1',2});
sma = AddState(sma, 'Name', 'stop', ...
'Timer', 0,...
'StateChangeConditions', {'Tup', 'exit'},...
'OutputActions', {'HiFi1',4,'AnalogIn1',2,'WavePlayer1',3});