There are several different functions to help you control the timing of sequences.
The simplest of these functions are the delay() statements.
The delay() function take a time in seconds to wait:
Output = 1
delay(60)
Output = 0
This will set Output to 1, wait 1 minute, then set Output to 0.
If you pass 0 or a negative value for the delay time, the script will cause the current thread (sequence) to drop to the lowest priority and release the CPU to allow other threads to run. Once the thread gets the CPU back, it will reset back to its original priority and continue. This allows you to run background tasks at high speed without hanging other things.
Note: in previous releases of DAQFactory there was also a wait() function which worked slightly differently than delay() and could create issues. For this reason it was removed as a separate function. To allow for some backwards compatibility, the wait() function now works identical to the delay() function. To implement the non-propagating feature of the old wait() function you should instead use a variable to track the next time you want something to happen along with a function like waituntil(). For example:
private nexttime = systime()
while(1)
Output = !Output
nextTime += 1
waituntil(nexttime)
endwhile
The above will toggle Output between 0 and 1 (assuming it is already set to 0 or 1) every second no matter how long it takes to actually update Output.
The waituntil command will wait until an absolute time:
Output = 1
waituntil(16h)
Output = 2
The above sequence will set Output to 1, then wait until 4pm today and set Output to 2. If it is already past 4pm, the sequence will set Output to 1 then immediately set Output to 2.
Application: Repeating something at the same time every day:
The most common use of waituntil() is to repeat something at the same time every hour, day or week. Here's an example that uses waituntil() to repeat something every day:
while (1)
WaitUntil(16h + 86400)
do something...
endwhile
Each time 16h is evaluated, it will automatically add in today's date, whatever that may be when the waituntil() line is executed. Depending on when the sequence started, it may already be past 4pm and we don't want the "do something" to execute until tomorrow. So we simply add 86400 to 4pm today which gives us 4pm tomorrow because there are 86400 seconds in a day. Thus the "do something" will execute at 4pm every day starting tomorrow at 4pm.
If you want the "do something" to start at 4pm today if sequence is started before 4pm, then you have to add some steps:
if (SysTime() < 16h) // if its not 4pm yet
WaitUntil(16h) // wait until 4pm
else
WaitUntil(16h+86400) // otherwise, wait until 4pm tomorrow
endif
while (1)
do something...
WaitUntil(16h + 86400) // wait until 4pm tomorrow.
endwhile
By reorganizing the loop to do something first, then wait until 4pm tomorrow we can add an if statement in front of the loop to determine when we want "do something" to execute first. If its not 4pm yet, then we simply wait until 4pm today, otherwise we wait until 4pm tomorrow.
A key to these samples is that while today's date is added to 16h each time the steps with 16h are executed, it only does so once. So while waiting for 4pm tomorrow (16h + 86400), the 4pm is not reevaluated at midnight thus pushing it back to 4pm the next day, and the next, forever in the future.
waitfor() is a bit different than the other wait statements. waitfor() will wait for a particular expression to evaluate to true:
output = 1
waitfor(Input[0] > 10,1)
output = 2
This sequence sets output to 1, then waits for Input to go above 10, then when that occurs sets output to 2. This of course assumes that you are reading Input somewhere else, either by setting the Timing parameter in the Input channel, or reading the channel through another sequence. The second parameter of the WaitFor statement determines how often the expression is evaluated. In this case, "Input[0] > 10" is evaluated once a second. To conserve processor power, this value should be set as high as possible. There is no practical use for setting it smaller then the rate the values in the expression are being updated, and it should never be smaller than 0.01 unless you have a multiprocessor computer and have planned for it. In general it should be set to maximum acceptable delay between the time the expression would evaluate to true and the time the next statement needs to execute. So in our example, if it is acceptable for Output to be set to 2 up to five seconds after Input goes above 10, then we should do waitfor(Input[0] > 10,5)
The above example is the same as:
output = 1
while (Input[0] <= 10)
delay(1)
endwhile
output = 2
The waitfor() version is just a touch cleaner.
Note: the second parameter of waitfor(), the interval, must be a scalar number and cannot be a variable. If you need to use a variable loop time, use the normal while() loop syntax.