AddState()

Description

Adds a state to an existing state machine.

Syntax

NewStateMachine = AddState(StateMachineStruct, 'Name', StateName, 'Timer', TimerDuration, 'StateChangeConditions', Conditions, 'OutputActions', Actions)

Parameters

  • StateMachineStruct: The state machine you are adding to. If this is the first state, StateMachineStruct is the output of NewStateMachine().

  • StateName: A character string containing the unique name of the state.

    • The state will automatically be assigned a number for internal use and state synchronization via the sync port.

  • Timer: The state timer value, given in seconds

    • This value must be zero or positive, and can range between 0-3600s.

    • If set to 0s and linked to a state transition (see next bullet), the state will still take ~100us to execute the state's output actions before the transition completes.

  • StateChangeConditions: A cell array of strings listing pairs of input events and the state changes they trigger.

    • Each odd cell should contain the name of a valid input event.

    • Each even cell should contain the name of the new state to enter if the previously listed event occurs, or 'exit' to exit the matrix and return all captured data..

  • OutputActions: A cell array listing the output actions and corresponding values for the current state.

    • Each odd cell should contain the name of a valid output action.

    • Each even cell should contain the value of the previously listed output action (see output actions for valid values).

Returns

  • A state machine struct, updated with the new state.

Examples

% 1. This code generates a simple state matrix that drives BNC output channel 1 to 5V (high) for 1 second before exiting.

sma = NewStateMachine();

sma = AddState(sma, 'Name', 'MyState', ...

'Timer', 1,...

'StateChangeConditions', {'Tup', 'exit'},...

'OutputActions', {'BNCState', 1});

% Tup occurs when the state's internal timer elapses


% 2. This code generates a simple state matrix that flashes the port LEDs of ports 1-3 for 0.1 second each (assuming an LED is connected to the port's PWM line).

sma = NewStateMachine();

sma = AddState(sma, 'Name', 'LightPort1', ...

'Timer', 0.1,...

'StateChangeConditions', {'Tup', 'LightPort2'},...

'OutputActions', {'PWM1', 255});

sma = AddState(sma, 'Name', 'LightPort2', ...

'Timer', 0.1,...

'StateChangeConditions', {'Tup', 'LightPort3'},...

'OutputActions', {'PWM2', 255});

sma = AddState(sma, 'Name', 'LightPort3', ...

'Timer', 0.1,...

'StateChangeConditions', {'Tup', 'exit'},...

'OutputActions', {'PWM3', 255});