From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Start Case Structure at the Same Time as Loop Structure

Hi All,

 

I have looked all over for similar questions/scenarios with not much luck. I believe my question is fairly simple. I have a loop structure where I use DAQ Assistant to collect the voltage of two sensors. At the same time the loop begins (by pressing the Boolean start button to data collection) I want a case structure to begin that has a sequence containing time delays and a digital out which opens and closes a relay. Both structures work as intended on their own but I need them both to start at the same time so I can set a known timing for the digital out found in the case structure. I tried using a stream and tag write/read for the true/false signal but it still didn't work.  Please find attached my VI.

 

I appreciate any suggestions/solutions and thank you in advance. 

0 Kudos
Message 1 of 12
(1,358 Views)

What exactly does "didn't work" mean?  What did you expect to happen?

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 2 of 12
(1,333 Views)

What is the purpose of the message channel in there?

 

Your case structure will only run once.  I don't  know channels in enough detail to know exactly what happens on their first execution.  But my guess is that since nothing is written to the channel until the code inside the while loop has started executing, the channel reader probably will start with a default value of false.  And it will only ever execute once.

 

I'm quite impressed that you've basically created a race condition using channels, which are alternatives to queues, and queues are supposed to prevent race conditions.

0 Kudos
Message 3 of 12
(1,307 Views)

You got some good advice already, here are a few more points:

 

  • There is no reason to maximize the front panel and diagram to the screen. At your state of learning you need to see at least three things at the same time (front panel, diagram, help window)!
  • If you want to only save while the boolean is held down, use the correct mechanical action (e.g. switch until released, like a car horn)
  • Your top code runs exactly once per session and the case structure gets whatever the value of the button is when the VI was started (probably FALSE, but whatever the state was when you started the program)
  • You can replace your sequence structure with a FOR loop autoindexing on an array constant of two booleans. One wait, one digital IO, half the code! ALso do something about the coercion dots.
  • It seems silly to do all that dynamic data operations just to end up with a DBL array of two elements
  • You could probably save front panel space by graphing both voltages in the same chart.
  • Using "write to spreadsheet file" is relatively resource hungry because it opens and closes the file with each iteration. I don't have DAQmx installed, so I cannot look at your assistant configuration. What's the acquisition speed and resulting loop rate target? your file IO is only OK if he loop rate is relatively slow.
  • Your stop should probably be connected to the "last element" input so the upper loop (once you have a loop!) will also stop.
  • Do you know about debugging techniques such as probes and execution highlighting? Try it to get a better feeling about the rules of dataflow.
0 Kudos
Message 4 of 12
(1,295 Views)

Hi paul_cardinale,

 

Thank you for your response.

 

What I was expecting to happen is that the case structure containing the digital out occurs only once while the case structure containing the saving continues to loop as it should. When I said "it didn't work" I meant that that upon pressing the start button the TRUE state is sent to the case structure containing the saving operation but not to the case structure outside of the loop which is used to toggle a relay (even after using tag/stream read and write, which I saw used on this forum to stop multiple loop structures at the same time with one button).

 

When moving the relay case structure within the loop it works, however the relay open and closes repeatedly as expected since it is inside a loop. I want the case structure containing the relay operation to only occur once upon the beginning of data saving which is why I'm trying to wire the relay case structure to the same button.

0 Kudos
Message 5 of 12
(1,246 Views)

Hi RavensFan,

 

I appreciate your input.

 

By message channel are you referring to the boolean read/write stream? I tried using this to send a TRUE state to the case structure outside the loop, after searching how to start/stop structures at the same time.

 

I initially had just a wire connecting the button to both case structures however the case structure containing the digital out never worked outside of the loop and still does not work even using the read/write stream.

0 Kudos
Message 6 of 12
(1,242 Views)

Yes.

 

When you say you want the case structure to start the same time as the loop, do you mean the True case of the structure?  Or the false case as well?

 

It seems like you are making this unnecessarily complicated.

 

Really, the digital output code belongs in the while with the acquisition.  Don't use wait functions to delay the true and false.  Just just the timing of your data acquisition to determine your loop rate and write a new digital output on each iteration of the lone while loop.

 

Altenbach already pointed out a number of issues you have including acquiring 1000 samples on each change in the DAQ Assistant, only throw out 999 of them.

0 Kudos
Message 7 of 12
(1,233 Views)

Hi altenbach,

 

I thank you for all of your suggestions.

 

First off, yes, the top code should run once per session but my problem is that it is not running at all. It does not toggle the relay after 1s of pressing the button. When I placed that case structure within the loop (to test) it worked, but repeatedly toggled the relay (as expected).

 

Secondly, I agree, I think it is silly that all I am converting the dynamic data to DBL. I did not want to use DAQ Assistant but I could not wrap my head around setting up virtual channels, I found the examples in labview confusing and I couldn't find one reading multiple voltages. Also, the two voltage readings are configured differently in NI MAX, one is RSE the other is Differential. If there are any helpful resources/examples you know of and could provide I'd appreciate it.

 

Lastly, for saving two columns of data what would you suggest?

0 Kudos
Message 8 of 12
(1,230 Views)

Hi RavensFan,

 

So you are basically suggesting to have two while loops with in the main loop? One that would open the relay at index 1000 (1s) and then at index 2000 to close the relay at 2s. How would I index the acquisition loop? Thanks.

0 Kudos
Message 9 of 12
(1,225 Views)

@PDE46 wrote:

By message channel are you referring to the boolean read/write stream? I tried using this to send a TRUE state to the case structure outside the loop, after searching how to start/stop structures at the same time.


 

What you researched isn't relevant to "a structure". It is specific to loops, and more about how to stop parallel loops than start them. A structure will start as soon as it has all inputs.

 

 

 


@PDE46 wrote:

I initially had just a wire connecting the button to both case structures however the case structure containing the digital out never worked outside of the loop and still does not work even using the read/write stream.


 

Yes, it never executed because it did not have all of its inputs. Actually, I suspect it executed once after you pressed stop and the final value passed out of the while loop. Have you tried watching any of this with Highlight Execution turned on? It should make it clear what is happening.

 

The thing you must understand is that any code not in some kind of loop will only execute once.

 

The way you have it now, what is happening is that on every loop iteration you are reading the Save button and writing its value to the stream. Your channel reader is not waiting for a True value, nor is your case structure. The very first time the Save button gets read and writes a False value to the stream, the reader will return that false value and your case structure will execute the False state, which is empty. It will then never execute again.

 

BTW, I'm not really very familiar with channel wires, but you have your stream size set to unlimited and you are only ever reading out a single element. I have a feeling that if you let this run long enough, you will crash due to running out of memory as the unread elements of the stream pile up.

 

What are you actually trying to do? It seems that you want to monitor data, but not save anything until the Save button is pressed? Should you be able to press save more than once, or should the VI exit after acquiring the saved data sample? Should the duration of the data to be saved match the timing on those relays? (Basically, can you describe your desired program flow?)

0 Kudos
Message 10 of 12
(1,202 Views)