Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท State Machines and Design Patterns

Designing State Machines

Build the classic LabVIEW state machine: a While Loop, a Case structure driven by an enum, and a shift register carrying the next state. Includes error states and design tips.

Sequence structures run steps in a fixed order. Real programs need to branch, repeat and recover: initialise, wait for the user, acquire, save, handle an error, shut down. The state machine is the LabVIEW pattern for this. It is the single most used architecture in LabVIEW applications and the starting point for every pattern in this chapter.

LabVIEW cannot run here, so the diagrams are described in words and a mermaid diagram, and a Python stand-in shows the same control flow so you can experiment with it.

The ingredients

A state machine on the block diagram has four parts:

  1. A While Loop that keeps running until a stop condition.
  2. A Case structure inside it, with one case per state, wired to an enum (or a string).
  3. A shift register on the loop that carries the next state from one iteration to the next.
  4. An initial value wired to the shift register's left terminal on the loop boundary, normally Initialize.

Each case does its work, then outputs which state should run next. The enum selects the case on the next iteration. To make the enum reusable, define it as a Type Definition: right-click the control, choose Make Type Def, and save it as State.ctl. When you add a state in the type def editor (open it and use Edit Items), every Case structure and constant using it updates automatically.

Use an enum, not a string, for the case selector. An enum cannot hold a misspelled value and the case selector lists the names; with strings a typo makes the VI fall into the default case silently.

A state diagram first

Before drawing wires, draw the states and arrows.

stateDiagram-v2
    [*] --> Initialize
    Initialize --> Idle
    Idle --> Acquire: Start pressed
    Acquire --> Process
    Process --> Save
    Save --> Idle
    Idle --> Shutdown: Stop pressed
    Acquire --> ErrorHandler: error
    ErrorHandler --> Shutdown
    Shutdown --> [*]

Every box becomes a case; every arrow is a value written to the shift register.

The same logic in a stand-in

The Python below uses an Enum, a dictionary of state functions (the Case structure), and a variable state (the shift register). Each function receives and returns the data the loop carries (the "data cluster").

Python A state machine in Python

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Passing data between states

States rarely work in isolation: Acquire produces samples that Process needs. In LabVIEW you bundle everything the machine shares into one cluster (typedef it too), carry it on a second shift register, and unbundle/bundle by name inside each case. Alternatives, from simplest to most flexible:

  • Local variables of front-panel controls (simple, but they hide dataflow and can race).
  • Shift register cluster (the recommended default).
  • Functional global variable (FGV) or a Data Value Reference when several loops must share the data.

Error handling inside the machine

Wire the error cluster into the loop on its own shift register. At the end of each case, test the cluster; if status is true, set the next state to Error Handler instead of the planned one. The handler shows the message, cleans up hardware and then moves to Shutdown. A single Default case that goes to the error state also catches impossible values.

A neat trick: put the error check in a small subVI Next State Or Error that takes the intended state and the error cluster, and returns the intended state or Error Handler.

Multiple next states

Often a state can go to more than one successor. The Case structure of that state chooses with a Select function or a nested Case structure: for example, Idle reads the last user command and returns Acquire, Settings or Shutdown. For states that always run the same successor, you can wire a constant.

Another variant is the queue-driven state machine: instead of a single shift register value, an array of states is carried and the machine pops the first element each iteration, so one state can enqueue a whole sequence. This leads directly to the queued message handler.

Common mistakes

  • Forgetting the timing. A tight loop in Idle with no wait uses 100% of a CPU core. Add Wait (ms) of 10 to 50 ms, or block on an event (event-driven UIs).
  • Doing too much in one case. If a state takes seconds, the UI freezes. Break it up, or move the slow work into its own loop (producer/consumer).
  • Uninitialised shift registers in subVIs that retain values between calls.
  • Unreachable states and states with no exit. A state diagram makes these obvious.

Documenting

Add a free label to each case saying what it does, give each state a short verb-noun name, and keep the Case structures small enough to view at once. A good state machine reads like its diagram.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which part of a state machine carries the next state to the following iteration?

    The shift register passes the value written in one iteration to the next.
  2. 2. Why use a type-defined enum for the state selector?

    Type definitions propagate changes; enums also prevent misspelled state names.
  3. 3. An Idle state with no wait and no event makes the CPU run at full speed. How do you fix it?

    A tight polling loop spins; a short wait or an event structure lets the processor rest.
  4. 4. How should errors be routed in a state machine?

    An explicit error state lets you report the problem and clean up hardware before stopping.

Summary

  • A state machine = While Loop + Case structure + enum + shift register.
  • Draw the state diagram first; each box is a case, each arrow a next-state value.
  • Share data in a typedef cluster on a shift register.
  • Route errors to a dedicated state and never leave a loop spinning without a wait.

Next: Producer/Consumer Loops