LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Shall I use global or local variables for my program?

Hello,

1) I am using 2 while loops in parallele. One 50 ms timed and the other one 1000 ms. The 50 ms one gets data from a sensor. I want to display that data every 1000ms in the 1000 ms loop. I have used global variables to do that. Should I better use local variables?

 

2) This question deals with the VI I have attached

a)  Shall I initialise the "  Quitter programme " global variable in the main VI aswell ? Or is what I have done enough?

b)

I want the sequence to continue ( finishes the last sequence and goes back to the first one without any delay if possible ) until the user presses Quit. I am concerned about the " wait until " icon of the exterior main while loop ( set to be 250 ms at the moment ). This time, 250 ms, is the time it takes between the end of the last sequence and the beginning of the first sequence? Is that right? Once u start the first sequence, the while loop doesn’t do anything until the end of the last sequence ( my 3 sequences can take several minutes ) ?

Hope I am clear… lol

Don’t hesitate to ask any questions if not clear enough

thanks a lot,

David

0 Kudos
Message 1 of 6
(3,006 Views)

Bonjour Marc.  You will definitely have a race condition because you are writing to the global in a parallel loop.  You could click the stop button in the quitter program, then before it is read in the main loop, the lower loop writes a false to it, and your click is lost.  Why do you want a separate quitter program?  You may have a good reason but it isn't clear here.  I would not use globals or locals for this, just put your stop button on the main vi.  You already have one, so why do you want another one in another vi?  If you must do it this way, eliminate both lower loops.  They are not necessary.  The global default is set to false, so when you run the vi, it is false.  The button mechanical action is set to latch when released, so after clicking it will turn true until the main vi reads it, then it will automatically turn false.  So you don't need the lower loops to keep writing false to it.  If you just eliminate both lower loops, it will work fine.  The only purpose I could see for the quitter vi is to have one button cause several vi's to stop.  Instead of doing it this way, put one stop button in your main vi, create a reference (right click - create - reference), and wire this reference into all subvi's that the main calls.  The subvi's must be changed to accept the reference and use it.  This would be a much better method to close all vi's with one button.

You could also get rid of the sequence structure (it hides code and makes it more difficult to read).  Just put all your code side by side.  The error in/out wire will ensure that the execution order will be fixed, just like in your first sequence with the open file and write file functions.  Just wire the error out of the write vi to the second write vi, and then the error out of the second write vi to the close vi.  No sequence structure needed, and all your code is viewable on one screen.

- tbob

Inventor of the WORM Global
Message 2 of 6
(2,986 Views)

In addition to what tbob said (since I already made the modifications), you use globals when you need to pass code between VIs. Locals are for the local VI.

You can look at my modification to see a slightly different example which uses events and doesn't have a subVI. Note the shift register and the -1 wired into the timeout terminal.

To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. Here and here are a couple you can start with. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide and the LabVIEW user manual (Help>>Search the LabVIEW Bookshelf).


___________________
Try to take over the world!
Message 3 of 6
(2,982 Views)
Dear marc france
 
Here is one way to measure, process, save data... and display if neccessary 🙂

Message Edited by EVS on 08-24-2005 09:07 PM


Jack
Win XP
LabVIEW 6.1, 7.0, 7.1, LabWindows/ CVI 7.1
Let us speek Russian 🙂
Message 4 of 6
(2,975 Views)

Hello EVS,

Thanks a lot for ur answer !

First I tried to go your way with the program I have attached. However I had 2 problems :

  1. I want the user to be able to start writing to the file when ever he wants. That is to say , I don’t want the saving to start when the program starts ( like the attached program does ). So I tried to put the "open file" in the while loop and use structure events so that it doesn’t "open the file" it every 50 ms but just writes in it. But I couldn’t make it work correctly. Would u have any idea how to do this?
  2. My second problem is that it seems that the writing timed to occur every 1000ms does one more 50 ms turn. That’s why I sometimes record every 2 secs instead of 1 sec. I have made many changes ( changing the initialisation, ….) without any success.

Any help would be really great

Regards,

marc

0 Kudos
Message 5 of 6
(2,935 Views)
Here is a small modification.
 
Choosing and opening the filename  should be placed outside of the main loop. It is because of time conflict possibility.
 
This code model is not optimal for large project, just for illustration Smiley Tongue.
 
Look for another models. One of them is a state machine.

Jack
Win XP
LabVIEW 6.1, 7.0, 7.1, LabWindows/ CVI 7.1
Let us speek Russian 🙂
0 Kudos
Message 6 of 6
(2,923 Views)