LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Parallel While Loop + Timer Problem

Solved!
Go to solution

Hello, 
I am have difficulty trying to have my main code run alongside two while loops, each of which contain a timer.  

The code takes in information from 3 temperature sensors, 2 soil moisture sensors and a humidity sensor.  The information is then processed through the code and appropriate action is then outputted.  I am using a USB-6008 which outputs to 5 digital outputs that switch on / off a bank of relays to control various instrumentation. 

There are two timers that feed into one another, this is designed such that a on state can be obtained and then an off state.  This is such that a grow light can be put on and off for a set amount of time.  The timing works correctly, however i am hoping to have my main code run at the same time as the timers, this is such that continuous measurements can be taken of temperature, moisture.. ect while output action can be applied.    

I have tried taking the output DAQ outside of the while loop, and while this fixes the problem allowing all while loops to run together, it prevents an actual output to occur, thus not turning on / off relays. 

Any help would be much welcome, 
Cheers.

0 Kudos
Message 1 of 5
(3,656 Views)

The temptation with LabVIEW is to "just start coding, don't worry about the design, just slap some Express VIs together and it will work".  Oh, well.

 

Some basic questions about your timer.  Am I correct that you have a single Timer, it is either On or Off, and when it goes On, it will stay on for some time, then go Off, and stay off for some time, then go On, and stay On for some time, etc.?

 

This is a single While Loop with a Shift Register holding "On/Off State", a Boolean, and a Case Structure.  If, on entry, the Shift Register (I'll call it the "State") is False (= Off), have the State changed to On and start the On Timer.  Now all you have to do is get this information "out" so everyone else knows that the State is On.  There are various ways to communicate such "global" information to the rest of your code -- one simple way is to have a Global Variable ("State") that is written in only one place (this "Clock" routine), but can be read wherever you need it in your program.

 

Bob Schor

0 Kudos
Message 2 of 5
(3,630 Views)
Solution
Accepted by topic author jt0117

I know National Instruments makes it look simple, but they really do new programmers a huge disservice with all the "Express VI's and only really teaching OBL programming architecture (One Big Loop) .

 

Here is something I read on the forum a few days ago 

 

  If you were writing C code, you wouldn't dump all your code into main(), so why would you dump all your LabVIEW code into one VI? (or Loop) It may look different, but it's still a programming language and you should follow the same basic guidelines that you would use for any programming language. 

 

You need to start with a proper programming architecture, your project screams "State Machine".

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 3 of 5
(3,617 Views)

Hello. 
Thank you for both your help !  Looked up the state machine and defiantly found it useful, however managed to get a quick fix by bringing in the DAQ into the first timer loop. 

Cheers ! 

0 Kudos
Message 4 of 5
(3,612 Views)

Good advice so far. A few more comments:

 

  • None of your loops are "parallel", because they have data dependency. Each loop can only start after the previous loop has finished.
  • Yes, this wild mix of booleans, numerics, and dynamic wires is just plain ugly and unpredictable! 😄 On the right, you merge several scalar DBLs with several boolean arrays into dynamic data, just to convert it all into a 1D boolean array a nanosecond later. Can it be more confusing?
  • In your two false cases, you have two boolean arrays with the index set at 1 (instead of zero) and only one element showing. This is extremely confusing. Set the index to 0 and resize to show three elements. Now it is clear that there are three elements, what they are, and what the array size is. Since both diagram constants are the same, you only need one. You can branch the wire.
  • ...
  • ...

Yes, all you need is a simple state machine and one single loop.

0 Kudos
Message 5 of 5
(3,597 Views)