LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Run block of code periodically

Hello,
I am writing a program which controls the temperature in a room. The user enters the "Start Temp", and "Goal Temp". First the program checks the "Cur Temp". THe program compares this temp with the "Start Temp"; if (Cur Temp < Start Temp) then I turn  on heaters. Once the StartTemp has been reached I am ready to move on to the next portion of my code which is cooling the room down at a user defined rate of cooling. I would like to compare the temperatures periodically. This is where I run into a problem. I have tried setting a delay before the cooling down portion of my code but it delays the hole while loop (all code in one while loop). I would like the temperature comparions to only occur every x amount of seconds (configurable by the user). So for arguments sake, I set the comparison rate at 10 seconds, every ten seconds I want to compare the previous value with the current value and determine the rate of at which it is cooling. If that rate is within in the desired rate of cooling then everything is ok. Ideally this code would not interfer with other program funcitons I have going on. Does anyone have any ideas on how I can have this code run perioically?

Thanks
0 Kudos
Message 1 of 7
(4,235 Views)
There are plenty solutions possible, and they depend a little bit on what else the code does.

@rchaoua wrote:
Once the StartTemp has been reached I am ready to move on to the next portion of my code which is cooling the room down at a user defined rate of cooling.
Well, the right thing is to do all in the same portion of code using a state machine architecture. There should only be one main loop with a case structure that depends on the state.
 
Here are some possibilities:

You can place your periodic code inside another case and only activate it whenever the loop counters is an integer multiple of some number or if a certain time has elapsed

  1. for every 10th iteration, you would divide [i] by 10 using "quotient&remainder" and check if the remainder is zero.
  2. Use the "elapsed time" express VI.
  3. Do your own time tracking (e.g. using tick count and a shift register.
  4. ...

You can also place your slow periodic code in its seperate, parallel while loop.

0 Kudos
Message 2 of 7
(4,232 Views)
I have transitioned over to a state machine architecture for my program. It has helped a lot.

I decided to use the elapsed time VI to control when my code is ran. In case 0, I set up my intial conditions (adjust to room remperature). In case 1, when x seconds have elapsed a Boolean HI tells my code to run and collect data and perform calculations based on these measurements. After a certain temperature has been reached I transition to case 2. In case 2, I must run this code for y seconds. When this time has elapsed I exit this case. My problem lies when I begin the second elapsed timer. Both elapsed timers are in seperate case windows. However, the first elapsed timer performs correctly (counts up to x seconds). While the second timer does not count up to y seconds. Is there any reason this occurs? Has anyone come across this before?

Thanks for the help.
0 Kudos
Message 3 of 7
(4,194 Views)
Hi rchaoua,
 
Does the 2nd Elapsed Time VI count up at all?  Try putting an indicator on the "Elapsed Time" output (the VI must be in a loop to actually see it count up of course).
Regards,

James R.
National Instruments
0 Kudos
Message 4 of 7
(4,161 Views)
James, thanks for the reply, yes it does begin to count up. But it will stop at ~3 seconds but I have it set for 18! My whole code is in one signle while loop. Inside the while loop I have alot of my variables, constants, and indicators. Then I have my solid state machine. In theory I think that have two seperate timers in two seperate cases should work. They are essentially peices of code that do not interact. I just don't understand why the second timer does not count all the way up. If I can't get anything going I will post my mess I call a vi and let you guys pick it apart. Any other thoughts or suggestions are greatly appreciated.
0 Kudos
Message 5 of 7
(4,160 Views)
The Elapsed Time VI can definitely be confusing to work with.  It's hard to say right now what's going on...it may depend on the architecture of your program.  If you're still having trouble, please post it and we'll be glad to take a look!

Message Edited by JamesR on 10-26-2007 04:12 PM

Regards,

James R.
National Instruments
0 Kudos
Message 6 of 7
(4,134 Views)
I was able to get it working. I had the elapsed time set at five seconds (set from the dialog box) but wired a larger value to the Set time input. When I increased the default settings I was able to elapsed the desired time. Thanks for the help.
0 Kudos
Message 7 of 7
(4,106 Views)