For many applications, data is acquired from devices at constant intervals. These are the applications that the Timing parameter of a channel is designed for. For some applications you may want to read data at varying intervals and Timing will work for that as well. Some applications you will only want to read data when you need it. For these types of applications there is the read() function of the channel. In order to use the read function though you must still create a channel to identify what input you wish to read. Since you are using the read() function for acquiring the data you should set the Timing parameter for your channel to 0.
Now, assuming we've created a channel called Input with a Timing of 0, we can use something like the following to read the channel at a varying time interval:
while(1)
Input.read()
delay(readinterval)
endwhile
This sequence will read Input at an interval determined by readinterval. readinterval could then be set using a page component allowing the user to specify the acquisition rate without changing the channel parameters.
Another use for the read statement is for process control loops. One way to implement process control loops is to read your inputs at fixed rate using the Timing parameter and use the channel's event. The event sequence code is executed every time a new value arrives on the channel, so you can evaluate the input and perform an output based on that input value. Alternatively, you can combine the polling and the control into a single loop. For example:
while(1)
Pressure.read()
if (Pressure[0] > 50) // if pressure is too high
VacuumPump = 1 // start pump
while(Pressure[0] > 50) // wait until pressure drops
delay(1)
Pressure.read() // read pressure again to check current value
endwhile
VacuumPump = 0 // then stop pump
endif
Temperature.read()
if (Temperature[0] < 30) // if temperature too low
Heater = 1 // turn on heater
while (Temperature[0] < 30) // wait until warmer
delay(1)
Temperature.read()
endwhile
Heater = 0 // turn off heater
endif
delay(1)
endwhile
Although a simple example, it is pretty straight forward when things occur relative to each other. The pressure is read in from the device, then it is checked, then the temperature is read, then it is checked, then we wait a second and repeat. We could continue this and make a more complex control system. When doing process control, and many other sequence tasks it is usually much easier to split things into separate sequences. In our example above, we are monitoring temperature and pressure. If the pressure gets too high, we turn the pump on until it drops. If the temperature is too low, we turn on the heater until it warms up. The problem here is two fold. First, if both the temperature drops and the pressure rises at the same time, the system will have to pump down the system until the pressure drops below 50 before it can start heating the system up. This may be desired, but if not, this is not the fastest way to get the system under limits. Second, this makes for a more complicated sequence. Instead we can make two sequences:
Sequence MonitorPressure:
while(1)
Pressure.read()
if (Pressure[0] > 50) // if pressure is too high
VacuumPump = 1 // start pump
while(Pressure[0] > 50) // wait until pressure drops
delay(1)
Pressure.read() // read pressure again to check current value
endwhile
VacuumPump = 0 // then stop pump
endif
delay(1)
endwhile
Sequence MonitorTemperature:
while(1)
Temperature.read()
if (Temperature[0] < 30) // if temperature too low
Heater = 1 // turn on heater
while (Temperature[0] < 30) // wait until warmer
delay(1)
Temperature.read()
endwhile
Heater = 0 // turn off heater
endif
delay(1)
endwhile
These two sequence can run concurrently within DAQFactory and so both temperature and pressure can be monitored and adjusted simultaneously. If the pressure rises too high and the temperature drops at the same time, the system can heat and pump down at the same time, being monitored by two separate sequences.
In general, you would probably stick with channel events for simple control like this. To see why, here is the event code for Pressure, assuming you had a Timing of 1 on the Pressure channel to poll it. The code to control the temperature would be very similar:
if (Pressure[0] > 50)
VacuumPump = 1
else
VacuumPump = 0
endif
or even more succinctly:
VacuumPump = (Pressure[0] > 50)
or for hysteresis, which is not implemented in any of the above code:
if (Pressure[0] > 50)
VacuumPump = 1
endif
if (Pressure[0] < 40)
VacuumPump = 0
endif