Example Code

Perform Action at Specified Intervals without Elapsed Time Express VI Using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This VI demonstrates how to perform a simple action every X number of minutes.

 
Description

Often it is necessary to perform a certain action at certain intervals, such as writing to a log file or updating a graph.  Although the Elapsed Time Express VI will help perform this action, if we are trying to maximize the efficiency of our code, we may want to do this manually.  This code demonstrates how to perform a simple action every X number of minutes, although it could easily be changed to occur by a second increment, and the action performed could be changed to be anything.

One common misunderstanding involving timestamps is that they are just a count of seconds that LabVIEW cleanly displays as time and date.  We can perform calculations and comparisons on these timestamps, just like we could other many other data types.  This code adds X number of minutes to the current time, and then saves that as the next time to execute the action.  Within the while loop it is comparing the current time to the time of the next action, then once this the time has elapsed, it increments another X number of minutes onto the time of the next action and performs the action, in this case a simple switching of a boolean light.


Requirements

  • LabVIEW 2012 (or compatible)


Steps to Implement or Execute Code

  1. Choose the number of minutes to wait between actions.
  2. Run the VI.
  3. Observe Current Time being compared to the Time of Next Action, and the boolean indicator alternating every X minutes.
  4. Stop when complete.

Additional Information or References
VI Snippet

006.png

**This document has been updated to meet the current required format for the NI Code Exchange.**

Paul Davidson
National Instruments
Product Owner - ni.com Chat

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

Comments
Oscarin20
Member
Member
on

Sorry I need to save difference between one data each hour

Paul-D
NI Employee (retired)
on

Greetings Oscarin20, you could certainly achieve this, and any other activities that need occur on some kind of timed basis.  In this example, you would changd your 'Minutes to Delay' to be 60.  Inside the 'True' case I chose to use a boolean indicator that alternated states every time the time elapsed.  You could certainly achieve calculating or saving a difference each hour by putting that code inside the case structure, instead of my code to alternate the boolean indicator.

Paul Davidson
National Instruments
Product Owner - ni.com Chat
kunal7505
Member
Member
on

I have a while loop running for 24hrs and I want that loop to pause and switch to another inner loop after every 30 minutes. Can you explain how to do that?

Paul-D
NI Employee (retired)
on

Due to dataflow, if your inner while loop is executing, the outer will not iterate until the inner finishes.  Therefore, you could wrap your inner while loop in Case Structure using True/False.  Basically inner loop would go inside where 'alternating light' is above.  If the time has elapsed (TRUE), your inner while loop will start executing and run until it is stopped.  If the time has not elapsed (FALSE) then the case structure is empty, and your outer loop keeps iterating without starting the inner loop. 

In this scenario of 2 loops it would be important to at least use a local variable to capture your 'Stop' button inside the inner for loop, or else you wouldn't be able to stop your code until the inner loop finished.  Better practice than a local variable would be to use an event structure, but that's well outside this discussion.

Paul Davidson
National Instruments
Product Owner - ni.com Chat