2.7.3 - StartInterval | LabJack
« Close

Datasheets and User Guides

Software & Driver


2.7.3 - StartInterval

Sets up a reoccurring interval timer based on the host system's clock.


                      int IntervalHandle,
                      int Microseconds)


IntervalHandle [in]
The user-generated interval identifier. This can be anything.
Microseconds [in]
The number of microseconds in the interval.


LJM errorcodes or 0 for no error.


This function allocates memory for the given IntervalHandle and begins a reoccurring interval timer. This function does not perform any waiting.

In many applications an interval timer is preferable to using a normal delay (sleep), as it is difficult to get an exact interval using a delay.  For example, say a loop makes an I/O call that takes 5 to 15 milliseconds (ms) each time, and this is followed by a 90 ms delay.  The loop will take 95 to 105 ms per iteration, and the average time per iteration over time could be anywhere in that range.  Using interval timers avoids this problem because the loop will always take the specified number of milliseconds (or else LJM_WaitForNextInterval will notify you of skipped intervals).

Jitter and clock skew may occur, but the average interval period over time (ignoring skipped intervals) will be equal to the number of specified Microseconds.

For more precise timing, use Lua scripting or streaming.


[C/C++] Read AIN0 once per second, 100 times.

Given a handle to a device, a function named ReadAndOutput that reads and outputs a registers, and an error-checking function called ErrorCheck, the following example reads AIN0 once per second, 100 times:

int err;
int SkippedIntervals;
const int INTERVAL_HANDLE = 1;
err = LJM_StartInterval(INTERVAL_HANDLE, 1000 * 1000);
ErrorCheck(err, "LJM_StartInterval");

for (int loop = 0; loop < 100; loop++) {
    ReadAndOutput(handle, "AIN0");

    err = LJM_WaitForNextInterval(INTERVAL_HANDLE, &SkippedIntervals);
    ErrorCheck(err, "LJM_WaitForNextInterval");
    if (SkippedIntervals > 0) {
        printf("SkippedIntervals: %d\n", SkippedIntervals);

err = LJM_CleanInterval(INTERVAL_HANDLE);
ErrorCheck(err, "LJM_CleanInterval");