LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

calculation using formula node needs two clicks

Solved!
Go to solution

Hello,

 

I want to use this piece of code for a larger program. My objective is to take in 10 values and calculate two values using a formula. I used the formula node since regular numeric calculation functions would be too cumbersome. If I click on save, it saves the two values to a file.

 

The problem I'm facing is that when I start the program, it runs fine for the first time. The second time however, if I change the values, and click on save, it still shows the earlier values, or some changed values. I have to click on save two times to get the correct values. I don't understand the reason why this happens. Can anyone help?

 

Attached: Screenshot and VI file.

 

P.S. If the solution helps with the problem, I'm ready to change any part, but please, no irrelevant suggestions on how to *improve my code*, because I've worked hard to make it work together with  another really large project.

Download All
0 Kudos
Message 1 of 14
(3,807 Views)

A few comments,

 

Put the save button terminal inside the event structure and set the mechanical action to latch when release.

Create a stop event and put the stop button terminal inside it.

Why do you use a filter event?

Don't make the top loop run as fast as possible, put a delay in it.

What is the purpose of the top loop, why are you openaing and closing the same file in both loop, couldn't it be done in the same loop?

 

Ben64

Message 2 of 14
(3,787 Views)
Solution
Accepted by topic author prafullashahi

I don't completely understand your problem description.  But I see two problems.

 

1.  The Save button really belongs in the event case that handles the save button.  And the event case should really be Value Change rahter than Mouse Down?

2.  Your stop button has the wrong mechanical action.  Since it is Switched until released, it is only true while you are pressing it.  It will stop the top loop, but can't stop the bottom loop because your bottom loop will also be waiting for the save button to be pressed.  Stop will always be false because you can't press two buttons at once.

 

Try making your mechanical action for the stop button to be Switched When Released.  You'll have to reset the button back to false programmatically after both loops end.  It would actually be better to have an event case for the stop button to stop the lower loop.

 

I don't understand what you are trying to do in the upper loop.  You are rereading a file as fast as you possibly can, and it looks to be the same file you are creating in the lower loop.  Why?

Message 3 of 14
(3,785 Views)
Would you consider it irrelevant to place your control where it's supposed to be or to use the value change event?
Message 4 of 14
(3,782 Views)

@prafullashahi wrote:

Hello,

 

[...]

P.S. If the solution helps with the problem, I'm ready to change any part, but please, no irrelevant suggestions on how to *improve my code*, because I've worked hard to make it work together with  another really large project.


Your code doesn't work as you'd like; any suggestion is, necessarily, on how to improve it. 😛

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 5 of 14
(3,764 Views)

When I try to run it, I get an error in the lower loop: Error code 11 - Too many files open.  You do not appear to be checking errors in that loop.  Opening and closing the same file repeatedly in two loops seem like a recipe for trouble. Why not open it once outside the loops, pass the reference in for reading and writing, and then close the file after both loops exit?

 

Also, how do you stop the VI? The stop button terminal in the lower loop will be read almost immediately after the loop starts running. And then it will be read again immediately after any iteration (event case completes) finishes. This will happen too fast for the user to move the mouse from the Save button to the stop button and press it.

 

In spite of the errors it appears that any changes to the Point i controls is picked up immediately when the Save Button is clicked.  Perhaps you are reading an older version of the file which has not yet been updated? The upper loop runs thousands of iterations per second (at least with empty files).  Perhaps you should consider some form of change checking.  The File/Directory Info function from the Advanced File palette has a last modification timestamp output.

 

Locking the Channel control to -1 forces the array index in the upper loop to be negative for the first two iterations of the for loop. Since you only have two iterations, the output of Index Array will always be the defalut value (0.00). Is that what you want?

 

Lynn

Message 6 of 14
(3,748 Views)

Thank you for your replies. I will answer all of you here (Otherwise, you may choose to go directly to EDIT below):

 

1. The Save button really belongs in the event case that handles the save button. And the event case should really be Value Change rahter than Mouse Down?
Put the save button terminal inside the event structure and set the mechanical action to latch when release.
-> Okay, I will put the button inside the event case and change it to latch when release. But I never understood why I have to put it inside the case. I saw that in some other examples too.
Also as far as I know, a "Value change" event would create two events for clicking and unclicking it. Wouldn't it make the event structure run two times when in fact, I only want it to run once? Also, I'm not expecting anyone to use the keyboard here.

 

Create a stop event and put the stop button terminal inside it.
2. Your stop button has the wrong mechanical action. Since it is Switched until released, it is only true while you are pressing it. It will stop the top loop, but can't stop the bottom loop because your bottom loop will also be waiting for the save button to be pressed. Stop will always be false because you can't press two buttons at once.
Try making your mechanical action for the stop button to be Switched When Released. You'll have to reset the button back to false programmatically after both loops end. It would actually be better to have an event case for the stop button to stop the lower loop.

-> Okay, so that's why my stop button never worked! Thanks!

 

Why do you use a filter event?
-> I really don't know. I will change it to Mouse down if that's the problem.

 

Don't make the top loop run as fast as possible, put a delay in it.
What is the purpose of the top loop, why are you openaing and closing the same file in both loop, couldn't it be done in the same loop?
I don't understand what you are trying to do in the upper loop. You are rereading a file as fast as you possibly can, and it looks to be the same file you are cre

-> The top loop is a part of a read system, it will go somewhere else independent of the bottom loop with some more changes. I had them initially in the same loop, but there were some errors, especially <not a path> used to come up all the time. This one worked, so I kept it.


Thanks for your comments.

 

EDIT: 

I made the changes, so the stop button now works. But the formula node still needs to make two calculations to arrive at a final answer, which is my main concern. Also my save button appears to be "ON" all the time, even though it isn't and clicking on it still works (minor issue).

If it's not evident what I'm talking about, please run these steps:

 

1. start, change to channel 0 in "select channel" and click on save once.

2. now make changes to values, specially on the right side (doesn't matter, but in reality, changes will be made there mostly)

3. now click save once, see the values "m" and "b". Click on save again to see how they change.

 

To make it more clear where the problem is, start highlight execution just before step 3. You will notice that the output of the formula node shows two different values on each click even though the input remains the same. It's almost as if the formula node is calculating the values in two steps.

 

I'm attaching the new VI file, if anyone wants it. I've removed the top loop since it's no longer relevant (and is distracting) to the conversation.

0 Kudos
Message 7 of 14
(3,738 Views)

I changed the save to "Value change" rather than Mouse down, and now it works as expected. Thanks.

 

 

0 Kudos
Message 8 of 14
(3,719 Views)

I still do not see the two values effect that you see. The outputs change if the inputs change and not otherwise, regardless of how many times I click Save.

 

What gets written to the file? It appears that only the last "save" gets written to the file. Selecting a previously written channel does not seem to put the new data for an old channel into the file, although I have not checked very carefully.

 

Lynn

0 Kudos
Message 9 of 14
(3,704 Views)

I really don't know the reason, but I tried to get screenshots to show the values of m and b with each click to show you what happened. I used the old "Mouse down" setting for this.

Everytime I tried to take screenshots, it calculated in the first attempt. Whenever I did that without taking screenshots, it took two attempts. Finally I gave up. It's one of those situations when you have a fever but the moment you go to the doctor, it goes away.

Maybe it was about the fact that the formula was trying to get to the solution during the moment the mouse was down, which gave it less time to calculate? Maybe? But given the capacity of my processor, I highly doubt it. Your thoughts?

Or it was plain old timing.

So you'll just have to take my word for it.

 

EDIT: using highlight execution (I gave enough time for the execution to complete), please find attached file.

0 Kudos
Message 10 of 14
(3,671 Views)