Wait time within a While Loop

I was always under the impression that Wait Timers (e.g. Wait and Wait Ms Multiple) always executed last within a while loop. As I'm reading up on timing of real-time loops I'm finding that NI suggested using a stacked sequence structure to force timing to run last. Has my assumption of wait timer execution been wrong all this time or do PC and RT systems differ in how loops handle timing?
Thanks,
Craig

So, ok, coming back on this.
Attached you find a small example to show my remarks in the previous post.
How to use it:
The main focus is in the first iterations once "Wait now!" is pressed. So it is usually not of interest to run the "waiting" for more than 5-10 iterations.
In order to see an "understandable" behavior, i suggest to wait at least 100ms. I configured "Time To Wait" to be at least 10 (coercing).
The second focus is "Time Value".
For "Wait (ms)", the "Time Value" will have any number, but increase each iteration by the amount of "Time To Wait".
For "Wait Until Next Multiple (ms)", "Time Value" will be initialized to a value, which is a integer multiple of the "Time To Wait" and will increase each iteration to the next multiple.
My example does not contain parallel code, so it does not show the parallelism of the waiting functions!
Nevertheless, understanding the shown behavior is the key. Because if the code running in parallel to the waiting requires less execution time than the wait function will wait, the behavior will be exactly as shown.
If the parallel code requires more execution time than the waiting, "Wait (ms)" is already finished and therefore the loop will immediatly continue with the next iteration (high CPU load!).
If the parallel code requires more execution time than the waiting, "Wait Until Nexty Multiple (ms)" will wait that long that the "Time Value" is again in line to an integer multiple of the "Time To Wait" (so this one keeps on running in parallel until waiting time is calculated and over). Hence, "Wait Until Next Multiple" will still introduce a waiting time (reduced CPU load), but you will "miss complete iteration slots".
As you can see, using a single "Wait Until Next Multiple" outside the loop for initialization can make sense if the first iteration should also have a "close to normal" execution time.
Please note that the Timed Loop does something similar once started. As it does it during the iterations, the first 1-3 iteration(s) are called "warmup iterations". Most often, the first single iteration is sufficient for this though....
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Attachments:
TimingVI_Behavior_2011.vi ‏29 KB

Similar Messages

  • Execute case structure a certain number of times within a while loop

    Hi guys,
    I was wondering if it was possible to execute a case structure a specified number of times within a while loop. Basically I'm running a camera where each iteration of the outer while loop displays a frame of a live video feed and I'd like to capture the next x amount of frames (specified by the user) each time the 'Start Capture' button is clicked. The block diagram within the case structure captures and saves an image frame. As the program is written right now, frames will continue to be captured until the 'Start Capture' button is clicked again. Instead of having to click the button again to stop the case structure from executing for each iteration of the while loop, I'd like it to only execute for the next x iterations of the while loop. Any help would be appreciated. Thanks.

    Hello Uxorious,
    A while loop has a iteration counter that gives an integer value of how many times the loop has iterated. You can utilise this to count the number of frames captured. I assume that the program could be running for any amount of time before clicking start capture, if that is the case then you will need to be able to detect when you have clicked the button and add the desired number of frames you wish to capture to the current loop iteration.
    If you use a shift register on the outer loop with the start capture button connected to it (inside the loop) you will be able to compare its current value to its previous value. If it changes from false to true then you can add the desired number of frames to it to give you the loop iteration value where you need to stop.
    Then you can compare the current loop iteration value to that number and once reached, you can use a property node to change the value of your start capture button back to false.
    One final thing, don't forget to initialise the shift register I suggested to false so that you don't run the risk of missing the first time you press the button.
    I hope that makes sense.
    Alistair
    Best regards,
    Ali Bailey
    National Instruments
    Don't forget to Kudos useful posts!

  • Is there a way to run a For Loop independen​tly within a While Loop in LabVIEW 2013?

    In my program I would like to run a For loop inside of a While loop, and have them run independently, at their own execution rates.  As a test, I wrote a simple VI with a While loop with 1 second timing, and into this I inserted a For loop with 3 second timing.  I created indicators for both iteration terminals.  Upon running the VI, I found the While loop waits for the For loop to run N times before the While loop executes again.  I also found that sometimes the first iteration of the For loop will end at 1 second rather than 3 seconds, and the STOP button to terminate execution of the While loop does not always work.
    Solved!
    Go to Solution.

    ksinks wrote:
    Thanks, how would you synchronize the loops?  I have gone through the Getting Started exercises and manual.
    Why would synchronization matter? Did you want them to run independantly at their own speed or not?
    Regardless, there's a synchronization pallette with functions for this, as Occurance.
    Other solutions include a common loop counter and a case structure executing every X'th iteration, or having the faster loop send a queue or event every X'th loop that controls the slower loop.
    /Y
    LabVIEW 8.2 - 2014
    "Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
    G# - Free award winning reference based OOP for LV

  • How can I build a table with the time values of a timer from a while loop

    Hi:
    I have a question concerning building a table:
    Every 100ms I read a value from a sensor (while loop with a timer). I would like to build a table with the actual time and the concerning value. For example:
    0msec         1V
    100msec     2V
    200msec     3V
    300msec     4V
    etc.
    If I use the Express VI for building a table, I always get the date and time, but I don't need the date and the time is in the following format: HH:MMS, which is nonsensical for me as I can't differentiate within msec. Can I change the format anywhere?
    Can I also save the table to a file or even to an Excelsheet? How can I do that?
    Thanks for your help!

    Hi Craig:
    thank you very much. To solve the mystery : ) :
    I want to drive a stepper motor with a specific frequency. To get the current degree value of the motor I would like to measure the current time (from the beginning of the move on). (With a formula I get the degree value out of the time)
    Concurrently I would like to get data from a torque sensor and from a pressure sensor. That's why I asked you about the time and the table. The measurement should start with the movement of the motor. How can I do that? Right now I have different block diagrams (different while loops) (see attachment) and I would like to put them in one.
    I haven't done the block diagram for the pressure sensor yet, so there is only the one for the torque sensor and the one for the motor.
    I also would like to set a mark in the table when the voltage value of an analog input gets under a specific threshold value. Is that possible?
    I'm sorry, I'm a novice in LabVIEW. But maybe you can help me.
    Thank you very much!
    Steffi
    Attachments:
    motor.vi ‏238 KB
    sensor.vi ‏59 KB

  • Looking to implement a timer inside a while loop

    Hi,
    I am trying to program a LabView application where I have a mail 'While' loop for continuous data acquisition. Data is continuously acquired and written to file in each iteration of this loop.
    Upon meeting a certain condition in the data, I plan to implement the start of a timer. However, this timer should not interfere with the normal execution of the while loop. After this timer is complete, I check for a certain second condition and stop my data acquisition.
    I tried using the 'Wait' function, but could not succeed programmatically in avoiding the delay with interfering with the normal functioning of the while loop.
    Could there be a simple workaround?
    Thanks,
    -Dude

    Look at the Time Elapsed express VI (Time & Dialog palette). It allows you to reset a timer and check if the time has passed. If you open it (right click>>Open FP>>Convert), you will see it uses an uninitialized shift register to remember the time it started at. Search the site for that term or LV2 globals to learn more about this technique.
    Try to take over the world!

  • Writing a file for prescribed amount of time in a while loop after the triggering condition is met

    Hello Guys,
    I am trying to program an application in which if the trigger condition is met it should start to write the file.
    I was able to do that, but if I want it to be written for certain amount of time, and once the trigger condition is met, it should continue to write the files for prescribed amount of time. I was not able to do this. The file name should also be updated accordingly.
    I tried to with some options by keeping the creation of the file outside of the loop, if I do that then if the trigger condition is not met it will stop writing and I dont want that, as once the trigger condition is met it should be true thereafter.
    So I cant keep any thing outside the while loop, bcaz then it checks the condition of the shift register for the trigger and I have it as a false constant.
    I am attaching my application which is kind of mess and I have written in the application where I am having problems. For every iteration of while loop it makes a new file, and i dont want that, I want to write specific amount of time data to the each file.
    Please take a look at my code and help me in solving the problem. Any insights and examples on how to do this thing will be a relief to me.
    Thanks in advance.
    Regards,
    Nitin
    Attachments:
    PXI_4462_Sync_and_Stream_trigger.zip ‏192 KB

    what i am saying is to keep track of how much data you have written and whenever you need to make a new file you make 1.
    here is a vi that i just made that should show you. let me know if you need any help understanding it.
    Attachments:
    new file exemple.vi.zip ‏21 KB

  • How to create a time control for while loop?

    Hello all,
    I would like to control my while loop's execution time. In other words I want to stop while loop's execution after time (in ms) which is set by user. For example user sets 5 seconds to front panel control and a while loop runs and stops after 5 seconds.
    I am not sure is it possible with the while loop. I succeed with the timed loop but it suits badly to my application. Is there any other loops for timed control if it is not possible with while loop? There is also a simple while loop with Random Number (0-1).vi as attachment.
    Best Regards,
    Jick
    Solved!
    Go to Solution.
    Attachments:
    execution time loop.vi ‏6 KB

    hi,
    see this, may be it will help.
    Best luck
    Gaurav k
    Gaurav k
    CLD Certified !!!!!
    Do not forget to Mark solution and to give Kudo if problem is solved.
    Attachments:
    execution time loop.vi ‏9 KB

  • While loop with stop button within event structure locks up front panel.

    I am not sure if this is a bug with my program or a bug within LabVIEW.  If you believe that this is a bug with my program then I will post my program to be looked at.
    The problem I am having is there is a while loop within an event structure that fires when a particular value changes.  Once the "Activate" button is pressed the while loop within the event structure starts going with a polling frequency of 1hz (1000ms wired to the "wait till next ms multiple" vi).  There is a "Deactivate" button that is wired to the stop control of the while loop and an outter while loop that resets the event structure so that the activate button is being listened to again.
    Once inside the while loop, however, none of the button are responsive within the front panel.  The VI continues to run, and only 60% of my CPU is being consumed, but none of the button or scroll bars work.  The only way for me to terminate the program is with the "Abort" button next to the "Run" button.  If I remove the event structure so that the while loop in question runs as soon as the program starts, the front panel remains responsive.  I've inserted probes within the while loop and verified that it is not running prior to the "Activate" button being asserted, and it is running after the assertion of the "Activate" button with the expected polling frequency set by the "wait till next ms multiple" vi.
    Can anyone help?  Do I need to post my code?
    -Nic

    It is typically not a good idea to stall an event structure by placing loops inside event cases. What good is an event structure if it is not free to repond to events?
    Have a look at some alternative solutions, such as in the following link:
    http://forums.ni.com/ni/board/message?board.id=170&message.id=224817#M224817
    LabVIEW Champion . Do more with less code and in less time .

  • Real time waveform graph in while loop

    Hi
    I have a VI which includes a subVI that reads data from an oscilloscope and presents the output in the form of a cluster made up of the initial time t(0), a time interval delta t and an array of voltage readings.
    This cluster can then be wired to a waveform graph in the usual way. All of this is situated within a while loop in my VI and on each iteration of the loop I obtain a waveform graph of the voltage readings starting at the same time t(0) and ending at the same time t. However I want my graph to begin at a time t(0) when I set my VI running and then continuously acquire and build up the data on each iteration of the while loop so after my VI has finished executing I have a complete graph of all the voltage readings acquired after the set number of iterations of the loop. I know on the Express X-Y Graph VI you can double-click the node and deselect the box that says 'clear data on each call' - if I could do this in my case that would be great but I can't use an x-y graph because I have an array of y-values to plot on each iteration.
    If anyone can help me out it would be much appreciated - I'm sure there must be an easy solution to this but I'm going out of my mind trying to find out what it is!
    Many thanks,
    Bex.

    As usual, you have several options.
    You can use an XY Graph by generating the X values yourself. This will ensure that you place the data exactly where you want it on the X axis. This is fast, but does involve a memory cost.
    You can use a Waveform Graph with a waveform input instead of the cluster you are using. Use the Build Waveform primitive instead of the bundler to make your waveform. Set the t0 values to the appropriate values (negatives do work, as do small numbers - time is in seconds, use the To Time Stamp primitive to convert from DBL to timestamp). Finally, you need to set the Waveform Graph to use the t0 values. Pop up on the Waveform Graph and make sure Ignore Time Stamp is not checked. Depending on your version of LabVIEW, you may have to pop up on the graph or the axis. When you unselect Ignore Time Stamp, you will probably want to reformat the X-axis again. The Waveform Graph clears data at each iteration, so you will need to replot all your data each time. Just create an array of waveforms and keep adding to it. The t0s will put the waveforms in the right places. Yes, this may cause memory problems.
    Finally, you can use the Waveform Chart, which does have memory. Create the waveforms like you did for the Waveform Graph and send them to the chart. It will remember what was sent to it from run to run. Make sure you set the Chart History Length (pop up on chart and select Chart History Length...) to long enough to hold your entire data. You can use a property node to get the data back at the end, if you need to.
    Good luck!
    This account is no longer active. Contact ShadesOfGray for current posts and information.

  • Why the execution time increases with a while loop, but not with "Run continuously" ?

    Hi all,
    I have a serious time problem that I don't know how to solve because I don't know exactly where it comes from.
    I command two RF switches via a DAQ card (NI USB-6008). Only one position at the same time can be selected on each switch. Basically, the VI created for this functionnality (by a co-worker) resets all the DAQ outputs, and then activates the desired ones. It has three inputs, two simp0le string controls, and an array of cluster, which contains the list of all the outputs and some informations to know what is connected (specific to my application).
    I use this VI in a complex application, and I get some problems with the execution time, which increased each time I callled the VI, so I made a test VI (TimeTesting.vi) to figure out where the problem came from. In this special VI I record the execution time in a csv file to analyse then with excel.
    After several tests, I found that if I run this test VI with the while loop, the execution time increases at each cycle, but if I remove the while loop and use the "Run continuously" funtionnality, the execution time remains the same. In my top level application I have while loops and events, and so the execution time increases too.
    Could someone explain me why the execution time increases, and how can I avoid that? I attached my test VI and the necessary subVIs, as well as a picture of a graph which shows the execution time with a while loop and with the "run continuously".
    Thanks a lot for your help!
    Solved!
    Go to Solution.
    Attachments:
    TimeTesting.zip ‏70 KB
    Graph.PNG ‏20 KB

    jul7290 wrote:
    Thank you very much for your help! I added the "Clear task" vi and now it works properly.
    If you are still using the RUn Continuously you should stop. That is meant strictly for debugging. In fact, I can't even tell you the last time I ever used it. If you want your code to repeat you should use loops and control the behavior of the code.
    Mark Yedinak
    "Does anyone know where the love of God goes when the waves turn the minutes to hours?"
    Wreck of the Edmund Fitzgerald - Gordon Lightfoot

  • Time in while loop measure

    hello,
    I want to measure the time in a While loop.
    I'm trying to know,  how much does need  my while-loop  to run for i = 0 and for i = 1. Which Function can I used?
    BR..

    You can use the Tick Count function.  It gives you the clock time in milliseconds.  Compare it with the value from the previous iteration that you store in a shift register.

  • Does the inner while loop have to be stopped before the outer loop will run?

    too many while loops cause my controls to be very sluggish.

    Hello,
    The outermost while loop executes everything inside it over and over. Therefore, if you have a while loop inside another while loop, the first iteration of the outer while loop will execute the inner loop, and then the inner while loop will execute over and over again until it is stopped. At this time the outer while loop will perform another iteration, and the process will repeat until the outer loop is stopped. Generally, embedding while loops within while loops is not recommended.
    Please visit the below sites for more information on program efficiency.
    http://zone.ni.com/devzone/conceptd.nsf/webmain/b6605678ad7a09a8862568eb007a6d7a?OpenDocument
    http://zone.ni.com/devzone/learningcenter.nsf/03f7c60f17aad210862567a90054a26c/1b36040af87887078625
    6cbc006fe314?OpenDocument
    I hope this helps!
    --Liz F

  • While loop doesn't stop until 2 more clicks

    I have to say I am really not familiar with LV at all, but Im learning.
    So, why is that after I clicked stop bottom, I had to click my OK button twice, then my program stopped. Why it can't stop immediately?
    Besides, if I want to some the dice result by array, which wire am I missing?
    Thank you!
    Solved!
    Go to Solution.
    Attachments:
    dice.vi ‏15 KB

    Hi RawtoLV,
    You have a while loop within a while loop. When you run the other while loop stops when you pressed the stop button, the inner while loop still runs. Since your OK button is using Latch when released, what i noticed is that when you pressed that OK button, it quickly switched to from true to false which didn't stop that while loop operation (could be due to the wait). May I ask what are you actually doing with the sequence structure?
    I would suggest using event structure instead (http://zone.ni.com/reference/en-XX/help/371361K-01/glang/event_structure/):
    here some more info on event structure:
    http://www.ni.com/white-paper/3331/en/
    http://www.youtube.com/watch?v=8eO64fo3Pho
    Warmest regards,
    Lennard.C
    Learning new things everyday...

  • Can't break out of a while loop

    Hi, I am doing a networking course, for which an assignment was to write an echo client and server. I did the assignment in Java and it works just fine. However, there is something I don't understand that is bugging me. I wanted a way to stop the server by either detecting when the client entered "quit" or when the user of the server entered "quit". This was not part of the assignment, but after trying many different things I really want to know (1) what I am doing wrong and (2) what the correct way to do things would be.
    I tried to put an "if" clause within the while loop so that if the client entered "quit" the server would detect that, close the socket connections, and break the while loop. But... it seems that I have done this wrong and I am not sure why.
    (Note - I am not a Java programer - just a beginner, and the course in question isn't a programming course. I did the program below by following some brief guidelines/tutorial. I don't even have any books on Java to consult right now - just ordered few from Amazon for any future projects.)
    The code:
    // EchoServer.java     - a simple server program. It listens on a predetermined port for an incoming connection.
    //                After a client connects they can input a line of text and
    //                the server will echo the exact same line back.
    import java.io.*;
    import java.net.*;
    public class EchoServer {
         protected static int DEFAULT_PORT = 3004;
         public static void main (String args[]) throws Exception {
              int portNumber;          
              try {
                   portNumber = Integer.parseInt(args[0]);
              } catch (Exception e) {
                   portNumber = DEFAULT_PORT;
              ServerSocket myServer = null;
                try{
                   myServer = new ServerSocket(portNumber);      
                   System.out.println("Echo Server started. Listening on port " + portNumber);
              } catch (IOException e) {
                   System.out.println("Could not listen on port: " + portNumber);
                   System.out.println(e);
              Socket clientSocket = null;                         
              try{          
                     clientSocket = myServer.accept();
              } catch (IOException e) {
                     System.out.println("Accept failed:  " + portNumber);
                   System.out.println(e);
              BufferedReader input = null;     
              PrintWriter output = null;
              try {               
                   input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                   output = new PrintWriter(clientSocket.getOutputStream(), true);
              } catch (IOException e) {
                   System.out.println(e);
              String theLine;     
              while(true){
                   theLine = input.readLine();
                   output.println(theLine);     
                   output.flush();
                        if (theLine.equals("quit"))  {
                             input.close();
                             output.close();
                             clientSocket.close();
                             myServer.close();     
                             break;
                        }     // end if
              }     // end while     
         }     // end main
    }          // end EchoClient

    A few observations:
    - You might as well exit after you have caught your exceptions, since none of the conditions I see would allow your program to run properly afterwards. You can either do that by a System.exit(int) or a return;
    - Although this s an example program, you should take advantage of doing things right. Addining a try-finally would be good for tidying things up. First declare your variables before the main try, otherwise the finally won't be able to do its job. For example (this won't compile as is):
    Socket clientSocket = null;
    ServerSocket myServer = null;
    InputStream in = null;
    OutputStream out =null;
    try {
      String theLine = null;     
      while( (theLine = input.readLine()) != null ){
        output.println(theLine);     
        output.flush();
        if (theLine.equals("quit"))  {
           break;
      }     // end while
    } catch ( IOException ex ) {
      ex.printStackTrace();
    } finally {
      if ( in != null ) {
         in.close();
      if ( out != null ) {
         out.close();
      // add other conditions to close other closeable objects
    }The reasoning is that if an exception were ever to happen you would be sure that the clean up would be done. In your current example you may risk having an inputstream left open. Also note that BufferedReader.readLine() returns null when its arrived at the end of its content.

  • How to stop a while loop with long delay

    Hi everyone,
    I am building a simple program for turning on and off a single Bit.  The task is to turn ON the Bit for a period length T1, and OFF the Bit for a period length T2. T1 is normally different from T2, and can be controlled  by users. 
    Since T1 and T2 can be variable, they sometimes can be very long.
    If I use a Wait function inside a While loop, it needs long delay to be able to Stop the Main program.
    The same situation happens when I use Timed loop.
    Can anyone suggest me how to do this task?
    Thanks a lot. 
    Message Edited by tatuan on 04-12-2010 12:56 AM

    Here's another possibility using OpenG Wait ms:
    Ton
    Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
    Nederlandse LabVIEW user groep www.lvug.nl
    My LabVIEW Ideas
    LabVIEW, programming like it should be!

Maybe you are looking for

  • How do I use a mini display to hdmi adapter to view my laptop on my tv?

    I bought a mini to hdmi adapter and I have an hdmi cable plugged into the TV but I get no picture and there is no other diplay detected by the computer.

  • Clean install, migration, time machine

    I am not a MAC OSX specialist in any way, but I would like to share what I have found out the hard way about the clean install. 1) Never do this unless you are absolutely sure that you have complete back up. Time machine cannot restore to the latest

  • 3 big problems with my gmail and my iphone 4 inbox...

    I know these are not new issues to most people, but I just haven't found any solutions yet. I've had 3 main issues with my gmail and iphone 4 inbox: 1) I've had the same issues of these weird messages from 1969, and also from 2000. I've done everythi

  • Schedule agreement texts (Header)

    Hi,     I need to display the header texts for the payment terms in Schedule agreement. In T.code Me32L I am changing the texts of the payment terms of schduling agreement,But how to retrieve that texts. I used READ_TEST function module,But i dont kn

  • How to open mdr files

    How can you open mdr files on mac?