Hi Josh,
I am finally trying to use the rotary encoder module, but I have some questions about how to use the thresholds defined in the rotary encoder to cause state changes when defining the state matrix. Basically, I want to play a sound and then to make sure mice run a certain minimum distance before giving them reward, but I don't know what syntax to use for this "threshold crossing" event. Moreover, I want to start counting this threshold only from the time the sound ends, so I also want one of the output actions of the next state ('WaitforMovement') to be set to zero. How can I define that action as an output for the state?
I am posting the code I want to use. Could you have a look and check if there is something else I am missing?
Thanks,
Sara
R = RotaryEncoderModule('COM7');
R.thresholds = [-40 40];
R.sendThresholdEvents = 'on';
R.streamUI
for currentTrial = 1:MaxTrials
startLogging(R);
sma = NewStateMachine();
sma = AddState(sma,'Name','Sound',...
'Timer',1,...
'StateChangeConditions', {'Tup', 'WaitForMovement'},...
'OutputActions', {'SoftCode', soundnum});
sma = AddState(sma, 'Name', 'WaitForMovement', ...
'Timer', 0,...
'StateChangeConditions', {'R', 'Reward'},... % I want the threshold crossing to cause the state change
'OutputActions', {});
% I want one of the output actions of this state to do zeroPosition(R) so that the threshold crossing is counted from the beggining of this state
sma = AddState(sma, 'Name', 'Reward', ...
'Timer', RewardValveTime,...
'StateChangeConditions', {'Tup', 'ITI'},...
'OutputActions', {'ValveState',1});
sma = AddState(sma, 'Name', 'ITI', ...
'Timer', ITIDuration(currentTrial),...
'StateChangeConditions', {'Tup','exit'},...
'OutputActions', {});
Acknowledged = SendStateMachine(sma); % Send state machine to the Bpod state machine device
RawEvents = RunStateMachine; % Run the trial and return events
BpodSystem.Data.AccumulatedReward(currentTrial) = AccumulatedReward;
stopLogging(R) % Stop logging the rotary encoder
BpodSystem.Data.RotaryEncoderData(currentTrial) = getLoggedData(R);
end