Showing results for 
Search instead for 
Did you mean: 

Odd problem with running Nonlinear Curve Fit VI with two while loops

Go to solution

In the attachment you can find a very simplified version of the program I develop at the moment with LabVIEW Version 13.0f2.

I use the “Nonlinear Curve Fit VI” to fit a function to some data in my main while loop. The values of the fit function are then used in an inner while loop for further calculations. The fit works well, but if I want to stop the program, I have to press the stop button two times.

If I replace the “Nonlinear Curve Fit VI” by another VI (linear fit or similar) or if I put the “Diagram Disable Structure” to remove the “Nonlinear Curve Fit VI”, the program works as expected: It stops after pressing the stop button one times.

I do not understand this behavior. Is this a bug? Or do I oversee some simple mistake?

I would be very happy about every help!

0 Kudos
Message 1 of 4

Classic race condition.


Most likely the local variable in the outer loop is read before the inner loop starts executing. When the stop button is pressed and the inner loop quits, the outer loop uses the value read from the local previously (false) and starts the next iteration. In the cases which appear to work the way you expected, you got lucky that the inner loop started before the local was read.  Because the inner loop is a greedy loop, once it starts running, everything else probably waits.


The fix? Well, the way you have things set up there does not seem to be any reason to have either loop. You said this was a simplified version so I presume other things happen in the larger program. Wire from the stop button through a tunnel in the inner loop to the stop terminal in the outer loop, eliminating the local variable.


better stop.png



Message 2 of 4
Accepted by topic author partial81

Your main problem is also the fact that the mechanical action of your button is "switch until released" (like a car horn!), This make the time it is true a function of the person operating the button. Depending on the execution times of the (currently missing) extra code in the inner and outer loops, there is no guarantee that both button instances (terminal and local variable) are read during the time the boolean is true. The outer local variable will most likely get read first, and you'll always get an extra outer iteration, after which the outer loop stops IF the button is still held down.


As Lynn said, the button should be in the inner loop and wired to both termination conditions if that's what you want. The mechanical action should also be "latch when released". Of course you can eliminate the outer loop entirely, because it serves no purpose here. The purpose of the inner loop depends on the currently missing code, so we cannot really judge that.

0 Kudos
Message 3 of 4

I see!
Thank you so much for your detailed explanation. Now I understand perfectly what my mistake is!

0 Kudos
Message 4 of 4