Control an LED Display

About the Example

The following example illustrates how MATLAB Function blocks exchange enumerated data with other Simulink® blocks. This simple model uses enumerated data to represent the modes of a device that controls the colors of an LED display. The MATLAB Function block receives an enumerated data input representing the mode and, in turn, outputs enumerated data representing the color to be displayed by the LED.

This example uses two enumerated types: switchmode to represent the set of allowable modes and led to represent the set of allowable colors. Both type definitions inherit from the built-in type Simulink.IntEnumType and must reside on the MATLAB® path.

Class Definition: switchmode

Here is the class definition of the switchmode enumerated data type:

classdef switchmode < Simulink.IntEnumType

This definition must reside on the MATLAB path in a MATLAB file with the same name as the class, switchmode.m.

Class Definition: led

Here is the class definition of the led enumerated data type:

classdef led < Simulink.IntEnumType

This definition must reside on the MATLAB path in a file called led.m. The set of allowable values do not need to be consecutive integers.

Simulink Model

Open the example model by typing emldemo_led_switch at the MATLAB command prompt. The model contains the following blocks:

Simulink BlockDescription
StepProvides source of the on/off signal. Outputs an initial value of 0 (off) and at 10 seconds steps up to a value of 1 (on).
Data Type Conversion from double to int32Converts the Step signal of type double to type int32.
Data Type Conversion from int32 to enumerated type switchmodeConverts the value of type int32 to the enumerated type switchmode.

In the Data Type Conversion block, you specify the enumerated data type using the prefix Enum: followed by the type name. You cannot set a minimum or maximum value for a signal of an enumerated type; leave these fields at the default value []. For this example, the Data Type Conversion block parameters have these settings:

  • Output minimum: []

  • Output maximum: []

  • Output data type: Enum:switchmode

For more information about specifying enumerated types in Simulink models, see Define Enumerated Data Types for MATLAB Function Blocks.

MATLAB Function checkState

Evaluates enumerated data input state to determine the color to output as enumerated data ledval. See MATLAB Function Block: checkState.
DisplayDisplays the enumerated value of output led.

MATLAB Function Block: checkState

The function checkState in the MATLAB Function block uses enumerated data to activate an LED display, based on the state of a device. It lights a green LED display to indicate the ON state and lights a red LED display to indicate the OFF state.

function ledval = checkState(state)

if state == switchmode.ON
    ledval = led.GREEN;
    ledval = led.RED;

The input state inherits its enumerated type switchmode from the Simulink step signal; the enumerated type of output ledval is explicitly declared as Enum:led:

Explicit enumerated type declarations must include the prefix Enum:. For more information, see Define Enumerated Data Types for MATLAB Function Blocks.

How the Model Displays Enumerated Data

Wherever possible, Simulink displays the name of an enumerated value, not its underlying integer. For instance, Display blocks display the name of enumerated values. In this example, when the model simulates for less than 10 seconds, the step signal is 0, resulting in a red LED display to signify the off state.

Similarly, if the model simulates for 10 seconds or more, the step signal is 1, resulting in a green LED display to signify the on state.

Simulink scope blocks work differently. For more information, see Enumerations and Scopes.

Related Examples

More About

Was this topic helpful?