09-07-2018 05:02 AM
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.
Solved! Go to Solution.
09-07-2018 08:02 AM
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
09-07-2018 09:44 AM
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".
09-07-2018 10:05 AM
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 !
09-07-2018 11:12 AM
Good advice so far. A few more comments:
Yes, all you need is a simple state machine and one single loop.