Showing results for 
Search instead for 
Did you mean: 

Timer lag and code efficiency and etiquette

Hi all,


so after writing this code and testing it, i've come across a couple issues, and I was wondering if you guys can help me out a little.  


First of all, I have a couple timers in my code - one that allows my code to write to the spreadsheet every 'n' seconds, and another that decrements my voltage every 'n' seconds.  The issue i have is that every time the loop runs, theres an ever increasing lag or delay.  for example, if i want to decrement my voltage every 2 seconds, it'll start decrement at 2, then 4.02, 6.06, etc.  I think it has to do with the loop iteration time, but how do i compensate for this?


another thing is, I have two big while loops in here, and a single 'STOP' button that aborts the program.  Problem here is that if i abort it during the first while loop, it will just jump into the second while loop.  Any ideas?


lastly, i feel like because i'm fairly new to labview, i've taken many unnecessary steps and overcomplicated this program.  Does anyone have any input as to how i can possibly make this code more efficient.  Also is there a general code etiquette to follow, such as the rules found in text-based programming?


Thanks all!

0 Kudos
Message 1 of 6



I have a few thoughts about your code:


1) Have you run this VI for a long period of time to see if the the timer always increases? If it does, you might try a different architecture. You could place a Tick Count VI outside of your while loop and another one inside your while loop. The one outside will keep a constant count and the one inside will continually increase. You can subtract them to get a more accurate count. It still won't be exact, but it should be closer.


2) The Indicator property node probably doesn't do what you think. It returns true if the stop button is an indicator and false if the stop button is a control. It does not change when you press the button. You probably want the Value property. Remember, though, that this property does not work with certain mechanical actions.


3) The way you have this code set up, the second while loop will not execute until the first one stops. In LabVIEW, a loop/function will not execute until it has all of its inputs available. Since you are passing the instrument handle and error wire from your first loop to your second, your second loop will not execute until your first has stopped. Based on your post, this does not sound like the behavior you want. You can run them in parallel, but since they are both calling the same VIs (which are probably accessing the same resources), you might get errors.


Have you been able to successfully run this code? If not, what errors are you seeing?



Message 2 of 6

1) I'll try something similar and see if that works


2) I dont know why I didn't catch that.  Thanks - i'll go ahead and try and fix that


3)  I want the second while loop to start after the first one finishes - should i put them in a sequencing loop too? becuase the way it is now seems to work fine.  the idea for the two while loops is that the first one ramps up the voltage to the desired level (lets say 300V).  Once we reach the 300V, then the second loop can start, and start the timers and etc.  the first loop is merely an initialization loop.  Does that make sense?

0 Kudos
Message 3 of 6

In regards to #2.  Would there be another way of doing this that you can think of aside from having two different buttons?  Having 'value' gives me a string, or a purple wire. instead of the green boolean one.

0 Kudos
Message 4 of 6

A few additional comments regarding your code.


1) Avoid the excessive use of property nodes everywhere. This will slow your application down. Each property node access will result in a context switch to the UI thread. Use shift registers and wires (wire are similar to a variable in a text based language) to pass your data through your program. Avoiding all of the property nodes may help to get more consist timing.


2) Your voltage initialization loop will set the voltage to the same value every iteration of the loop. You also have nothing in that loop to prevent it from running as fast as it possibly can. this may be OK, but if you are polling the instrument for it's current voltage you may want a small delay between your reads.


3) You will generally execute your second loop an extra time after the Stop button is read. This is because the Stop button will be read very early in the loop execution. You probably want to read it at the end of the iteration so you will need an artificial data dependency to force reading it at the end of the iteration.


4) If you are writing the data to the disk in the same loop you are processing and reading your data you can dramatically impact your timing. Disk access is generally slow and will not be deterministic. You may want to move that to a separate parallel loop and use a queue to pass the data to it to write to the file.


5) I don't believe that the Run Time will get what you wanted. Since your start time is always the same once you pass that time it will always return that the time has elapsed. I think you will want to change the start time everytime your elapsed time triggers.


6) Your second loop will also execute as quickly as possible. This can cause you to starve out other tasks or processes. It is always a good idea to put a Wait(0) to allow in loops that don't provide some other ability for LabVIEW to perform a task switch. Any LabVIEW VI that has some time of timeout associated with it (Queue, Notifier, Reads, Writes as well as others) allow LabVIEW to perform a task switch. You do have a read but the only thing gating how quickly this loop runs is the response time from the instrument. You will be querying it as quickly as the program can execute.

Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 5 of 6

Hi guys,


I'm calling it a night for today, but I took some of your comments, and *tried* to make some fixes and additions that follows your suggestions.  I did, however, run into problems/confusion as to how to replace the property nodes, and get the run times working right.


I've included the VI i have so far - if you have some time would you mind looking at it and tell me what direction to head towards.  I have not been able to run the VI through and see if it functions because the power supply is currently in use.



so first let me sum up what the goal of this VI is.  I need to control this power supply, so first i need to initialize it and get it up to the desired voltage level.  Then, it splits into three different functions.  I need the power supply to be on for a set amount of time "run time (generally 1-2 hours long).  I need it to either supply a constant voltage/current, supply a downwards incrementing voltage, or supply an incrementing current.


while on, i also need the VI to be recording time, voltage, current at a set time interval (could be from a couple seconds to a minute), and also display real-time voltage, current, and time.



so the first problem is the one you mentioned - the property nodes.  i'm new to labview so I don't know the best method, but I tinkered and found that this method worked.  I needed a way to increment or adjust the voltage/current levels while running.  Shift registers seem like a good idea, but i'm lost as to how to impliment that.  As you can see, the voltage levels are passed from the first loop to the second loop also.


as for the timing,  i tried to fix a couple of the ones you mentioned by creating multiple loops that (hopefully) will run in parallel.  Is that the correct way to do so?  In my though process, the real-time display will run at its own speed, the write-to-spreadsheet will run at another, and the voltage incrementing loop will run at yet another.  Am i thinking of this the right way?



*Thanks again for all your help.  extremely appreciated

0 Kudos
Message 6 of 6