LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

elapsed time on while loop on Event structure on for loop

Hello to all.
I have created a simple program, in order to have random numbers for 10 seconds every time I want.
Every time I want it means that it is important not to be forced to have a chain of for loops. Otherwise my event box loses all its meaning.


My main problem is that I must absolutely have 10 seconds of acquisition, and each time from the moment I press OK.I am attaching the program to make it more meaningful.

If you try the program you will see that if I run the program once I will have my 10 seconds of acquisition, but that the following times, depending on the time it takes to press OK, the acquisition time is no longer 10 seconds.

I think I understand that, when my while loop stops, the time continues to advance. And that my set point is not reset when I press ok to resume the loop.

This is very annoying and I would like to be able to reset my "elapsed time" to be able to restart it. Does anyone know this?

I have looked at many similar questions on the forum but nothing matches, and the various NI lessons do not address the issue.

0 Kudos
Message 1 of 10
(1,742 Views)

A few thoughts:

  • You're using a While loop, but always want 10 seconds with a 1 second wait each iteration. You could use a For loop if the number of iterations is known
  • The Express VI you're using has a "Reset" input below the "Time Target" input. You can probably try making that true in the first iteration each time (probably i=0 is the check you want)
  • Your "bouton OK" is outside of the Event Structure. This means if it is "Latching" (it is - see Changing the Mechanical Action of a Boolean Object) then it won't reset by itself - you can put it inside the Event Structure to improve the user experience. 

GCentral
0 Kudos
Message 2 of 10
(1,710 Views)

Yes, there are serious architectural problems, probably cause mostly by inexperience.

 

All you need is a simple state machine containing a toplevel while loop and a case structure (or event structure if done correctly, e.g. actually using them timeout event as needed). You know how many numbers you want with each shot, so the inner loop should be a FOR loop. Loops with delays however don't belong inside event cases, ever! You can use the outer wile loop for everything, just keep track of counts and time.

 

It also seems that you want 10 random numbers in 10 seconds and built it into a 2D array with 10 columns and N rows. Good to know. One new row with each button press.

 


@aerosimon wrote:

I have created a simple program, in order to have random numbers for 10 seconds every time I want.
Every time I want it means that it is important not to be forced to have a chain of for loops. Otherwise my event box loses all its meaning.


OK, we need to know your definition of "any time I want". What should happen if the button is pressed while it is already generating numbers? (start over, add 10 more seconds, etc.)

 

You currently have an outer FOR loop, so after 3 generations you can no longer generate numbers, no matter how much you want at that point time, because the program has terminated.

 

Please sit down and define all requirements in detail.

0 Kudos
Message 3 of 10
(1,646 Views)

See if this can give you some ideas. Modify as needed. 😄

 

altenbach_0-1624125236394.png

 

Message 4 of 10
(1,640 Views)

Thank you all it's very interesting, I don't necessarily want 10 minutes and not necessarily every second. It was for the example otherwise acquiring random values saturates the memory.

 

Your program seems to me really complicated, I have difficulty in understanding the need of the shift registers do they allow me to create only an array that I will be able to change as I wish?

 

I finally realized that, it seems to me a little complicated, maybe with a shift register I would not be obliged to put two events. What do you think about it!

 

 

0 Kudos
Message 5 of 10
(1,632 Views)

NO!!!!!

 

Your code is completely misguided and much more complicated than mine. Never (ever!) hide multiple event structures at various points of the dataflow. Most of the times your event structures cannot be reached, but they will lock the front panel until the event completes. Catch 22! Never place long while loops or any slow code inside any event structure!

 

Stacks of loops and 3D arrays is NOT the way to go! Your inner loops do a lot of work counting down time, but only the very last random values are retained. Nothing of this makes any sense!

 

You need to study the basics!

 

0 Kudos
Message 6 of 10
(1,628 Views)

As for the while loop, I have no choice but to put it in an event structure to decide when to start the acquisition.
Also I want to perform n random value acquisition x times. This is an example but in my personal case, I will have to do 3 point acquisitions for 5 different commands. So it seems to me that I have to keep 2 event structures in a row at least, each in its for loop.
And I don't understand why my 3D array wouldn't be a solution, I don't see any other.

 

0 Kudos
Message 7 of 10
(1,587 Views)

Hi Simon,

 


@aerosimon wrote:

As for the while loop, I have no choice but to put it in an event structure to decide when to start the acquisition.


As saw your code in the Rube-Goldberg thread: you really should learn the basics, and you really should learn about using statemachines!

You always have a choice: you can make the statemachine driven by events of your (single!) event structure…

 


@aerosimon wrote:

Also I want to perform n random value acquisition x times. This is an example but in my personal case, I will have to do 3 point acquisitions for 5 different commands. So it seems to me that I have to keep 2 event structures in a row at least, each in its for loop.


Again the answer is: "Use a statemachine!"

Keep a counter of the number of acquisitions in a shift register: that's the typical way of handling "statemachine private/internal data"!

 


@aerosimon wrote:
And I don't understand why my 3D array wouldn't be a solution, I don't see any other.

When you really need a 3d array then it is ok…

How are the 3 dimensions defined? What is controlling each dimension of the 3D array? (In a brick there is width, length and height to define the position in the brick: what is defining the element position in your 3D brick?)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 10
(1,582 Views)

In almost every situation (including yours) you would want the Event Structure to be inside the While loop.

Often (but not always...) you only want one Event Structure. You can definitely achieve your goals with just a single Event Structure.

 

To handle multiple events, you need multiple Event cases, not multiple structures.

 

If you want to have something respond to multiple different commands, but only one can be "active" at a time, take a look at perhaps the "Queued Message Handler" template project (Create Project -> Templates -> Queued Message Handler, the Create Project can be found on the launch screen or in the Files menu).

 

Although the template is a little overwhelming at first, it does work (as in, you can click run) immediately, and it demonstrates the handling of two separate "Do something" actions, along with the Exit button in the Event Handler (top loop).

 

The Initialize case in the "Message Handling Loop" is nice, but not critical for understanding to start with - if you create something like this code from scratch, you could manage without this case when starting.

 

Information about this template and the ideas behind it can be found here: Using a Queued Message Handler in LabVIEW.

Alternative information can be found on the wiki: Queued Message Handler (LabVIEW Wiki)

 

Even if you don't want to do "all of this", you should avoid violating the guideline given in the Wiki page ("Do not put long executing code in the EHL. If code in the event takes a long time to run, this locks up the UI and makes it seem to the user that the program has become unresponsive.").

 

Altenbach's code works by handling changes to the "timeout" value to ensure that the Event Handler remains available at all-ish times. The code actually in the Event Structure in his VI takes ~0 time to execute, and then it just waits for either an event, or the timeout ("time spacing" apart). Therefore, the Event Handler is never blocked.

If you instead use a While loop (inside), your Event Handling will be suspended until the While loop ends, ~10s later in your earlier descriptions.


GCentral
0 Kudos
Message 9 of 10
(1,576 Views)

@aerosimon wrote:

 

Your program seems to me really complicated, I have difficulty in understanding the need of the shift registers do they allow me to create only an array that I will be able to change as I wish?

 

 


Do you really think that altenbach's simple solution is more complicated than your monstrosity? Your code is WAY more complicated.

 

As others have said, you need to learn about state machines, and a queued message handler, which is essentially just a state machine with the states being controlled by messages via queues, would seem to be the a solution to your problem if you need something a little more complex than the simple solution offered by altenbach.. 

0 Kudos
Message 10 of 10
(1,557 Views)