Example Code

Why Does the Code Inside a While Loop Executes Twice within First Desired Loop Time?

Code and Documents

Attachment

The first time Loop Timer executes in a loop it records the current time. The next time it executes, it adds Count to the initial time and waits until Count has elapsed from the initial recorded time. If you place the Loop Timer in a loop so that it can execute immediately when the loop starts, all the code parallel with the Loop Timer in the loop executes twice before Count elapses after the initial time.

To prevent the code from executing twice before Count elapses, use a Flat or Stacked Sequence structure with the Loop Timer in its first frame and the rest of the code in subsequent frames to ensure that the code for the first and subsequent iterations is properly timed.

The attached code shows the difference between placing the Loop Timer in the first frame of a Flat Sequence and the rest of the code in subsequent frames and not using a sequence structure at all. By having a look at elapsed_time array when executing Loop Timer Behavior VI it can be checked that it takes two iterations for the FPGA LED to blink every second. Nevertheless, the FPGA LED in Loop Timer in first frame of Flat Sequence VI blinks every second right after the first iteration.

Loop Timer.PNG

Note: This information can all be found in the labview help documentation. You can find this documentation by going to Start»All Programs»National Instruments»labview folder. In the help file, this specific information is located in the Timing VIs section under FPGA Module»FPGA Module Timing VIs and Functions.

David Oña

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Todd S.
NI Employee (retired)
on

Thanks for posting.  Was this code already located in the help documentation?  Also, could you please rename your attachment to include the LabVIEW version and add "LabVIEW" to the tags so it show up on the main page of ni.com/community?  Thanks.

Todd S.
LabVIEW Community Manager
National Instruments
Contributors