Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Dataflow and Structures

Shift Registers and Feedback Nodes

Carry data between loop iterations with LabVIEW shift registers and feedback nodes: initialization, stacked registers, running totals and moving averages.

A loop body runs again and again, but each pass starts fresh: wires inside do not remember anything from the last iteration. To remember a value you need memory. LabVIEW gives you the shift register and the feedback node. They are the standard tools for running totals, counters, filters, and state machines. This lesson describes both in words; the examples are JavaScript stand-ins, not LabVIEW.

What a shift register is

A shift register is a pair of terminals on the left and right edges of a loop. Right-click the border and choose Add Shift Register. The pair works as follows:

  1. The right terminal receives a value at the end of an iteration.
  2. That value reappears at the left terminal at the start of the next iteration.

So the value "shifts" across the iteration boundary. A shift register works on any data type: numbers, strings, arrays, clusters.

Initialize it

The left terminal needs a starting value for iteration 0. Wire a constant or a control to the left terminal from outside the loop. If you leave it unwired, LabVIEW uses the data type's default (0 for numbers, empty for strings and arrays) and, importantly, remembers the last value from the previous run of the VI. That surprises people: the first run starts at 0, the second run starts where the first ended. Always initialize shift registers explicitly unless you really want that memory.

Reading the final value

When the loop ends, the right terminal's final value also leaves the loop on the right side, available to wire to the rest of the diagram.

Example: running total

To add up numbers as they arrive:

  • Initialize the shift register with 0.
  • Inside the loop, add the new sample to the value from the left terminal.
  • Wire the sum to the right terminal.

After N iterations, the output holds the total. The average is the total divided by N.

Stacked shift registers

Right-click a left terminal and choose Add Element to stack it. A stack of three left terminals holds the values from the previous one, two and three iterations ago. The top terminal holds the last iteration, the next holds two ago, and so on. You must initialize every stacked element. This is the natural way to build a moving average, a difference (x[n] - x[n-1]), or a simple digital filter.

Memory inside loops

Right terminal Save at iteration end
Left terminal Value on next start
Initialize Wire from outside

Unwired registers remember the last run.

1 / 2

The feedback node

A feedback node (Programming > Structures) is a compact way to do the same thing for one value. It looks like an arrow with a small box and has an initializer terminal at the bottom. It works inside any loop. Use it when a wire naturally loops back from an output to an earlier input in the same loop: it saves you from drawing a shift register and the extra wire. On a larger diagram shift registers are clearer because they have visible start and end points.

You can convert between the two: right-click a shift register and pick Replace with Feedback Node, or the reverse.

Common patterns

  • Counter: initialize at 0, add 1 each iteration.
  • Accumulator: add each new sample.
  • Previous value (edge detection): compare the current Boolean to last iteration's; a rise is now AND NOT previous.
  • Build array: shift register holds the array; Build Array appends. For large arrays prefer auto-indexing output tunnels, because they avoid copying on every append.
  • State memory: a shift register holding an enum is the heart of a state machine; see state machines later in this course (see the track page).
  • Error accumulation: carry an error cluster through the loop so the first error is retained.

Initialization pitfalls

  1. Forgot to initialize. First run looks fine; later runs start with stale data. Symptom: "works once".
  2. Wrong data type. A shift register inherits the type from the first wire. Wire it deliberately.
  3. Initializing inside the loop. A constant placed inside the loop does not initialize; it gets re-set each iteration, defeating the memory.
  4. Using i as the memory. i resets only when the loop starts; it is a counter, not general memory.

Stand-in 1: a running average with a shift register

This models a loop with a shift register holding the total and the count. The "left terminal" is the variable at the top of the loop; the "right terminal" is its assignment at the end.

JS Stand-in: running total and average

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

No output.

Stand-in 2: a three-point moving average with stacked registers

Stacked registers keep the last three samples. For the first two iterations the history holds initial values (zeros), so a real VI often waits until the history fills before trusting the result.

JS Stand-in: stacked shift registers

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

No output.

Stand-in 3: stale memory when not initialized

This shows the "works once" bug. A function without initialization keeps its register between calls, just like an unwired shift register keeps its value between runs of the VI.

JS Stand-in: the forgotten initialization

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

No output.

If a VI seems to work on its first run only, check every shift register for a missing initializer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the right shift register terminal do?

    Whatever you wire to the right terminal becomes the left value on the next pass.
  2. 2. What happens to an uninitialized shift register on a second run of the VI?

    Uninitialized registers remember the last value between runs, a frequent source of bugs.
  3. 3. What does a stacked shift register provide?

    Each stacked terminal holds a value from one more iteration ago.
  4. 4. When might a feedback node be preferred?

    A feedback node is a compact alternative to a shift register for a single recirculated value.

Summary

  • A shift register passes a value from one iteration to the next; initialize it from outside the loop.
  • Uninitialized registers keep the last value between runs.
  • Stacked terminals expose several previous iterations, ideal for filters and differences.
  • A feedback node is a compact form of the same idea.
  • Typical uses: counters, totals, edge detection, state memory and error carry-through.

Next: Case and Sequence Structures