Use Time Intervals for More than Storing Data: Decisions and Control

Learn how to use the TimeIsBetween() and TimeIntoInterval() instructions in your CRBasic programs.
Janet Albers

Clock with modem on/off indicators

We all know that Campbell Scientific dataloggers are really good at taking measurements and storing data. But did you know that they can also make decisions and control other devices? Often, device control is done based on a time interval. This article briefly illustrates how you can set time intervals using the TimeIsBetween() and TimeIntoInterval() instructions.

As a CRBasic programmer, you may already be using the DataInterval() instruction to store data on a specific interval, such as every hour at the top of the hour, like this:

DataInterval() instruction

Other things, besides storing data, can be done on an interval. For example, you can use the TimeIsBetween() instruction or the TimeIntoInterval() instruction within a logic statement to turn a device on or off, take a measurement, or perform a calculation.

Both instructions return the values of TRUE or FALSE depending on the datalogger’s real-time clock.

  • Use the TimeIsBetween() instruction to determine if the datalogger's real-time clock falls within a range of time. If so, the instruction returns TRUE. The instruction returns TRUE for the entire interval specified.
  • Use the TimeIntoInterval() instruction to determine if the datalogger’s real-time clock matches the time specified in the program at the start of a scan. If so, the instruction returns TRUE. The instruction only returns TRUE for the one scan that matches the interval specified.
TimeIsBetween() instruction TimeIntoInterval() instruction

In this example, a modem is set to turn on between the hours of 9:00 a.m. and 5:00 p.m.

In this example, a modem is set to turn on at 9:00 a.m. and turn off at 5:00 p.m.

Program code examples for TimeIsBetween() and TimeIntoInterval() instructions

Tip: Notice that in both programs, when you turn a device on, you also need to turn the device off. If you try to rely on a condition being FALSE to turn off a device, the FALSE condition will not change the state of the port, and your device will remain turned on.

The nuances between the two programs above matter the most if your program misses an exact time. In this first instance, because the program is sent at 8:20 a.m. (prior to the 9:00 a.m. start time), both instructions return TRUE at 9 a.m. and remain TRUE until 5 p.m.

TimeIsBetween() Modem TimeIntoInterval() Modem

8:00 AM

  Off

  Off

8:20 AM send program 

  Off

  Off

9:00 AM

  On

  On

...

  On

  On

5:00 PM

  Off

  Off

In this second instance, because the program is sent at 9:20 a.m. (after the 9:00 a.m. start time), the first scan returns FALSE for the TimeIntoInterval() instruction, and the instruction remains FALSE until 9:00 a.m. the next day.

TimeIsBetween() Modem TimeIntoInterval() Modem

8:00 AM

  Off

  Off

9:00 AM

  On

  On

9:20 AM send program 

  On

  Off

10:00 AM 

  On

  Off

...

  On

  Off

5:00 PM

  Off

  Off

Note: The TimeIsBetween() instruction was introduced with the release of OS 28 for the CR3000, CR1000, and CR800-series dataloggers, and with the release of OS 01 for the CR6.

Recommended for You: For more information about the TimeIsBetween() instruction, read the “Tips and Tricks: More Decisions, Decisions, Decisions…” newsletter article.

How have you used the TimeIsBetween() and TimeIntoInterval() instructions in your CRBasic programs? Share your story below.

Connect With Us

envelope icon
Phone Numbers
General: (435) 227-9000
location icon
Corporate Address
815 W 1800 N
Logan, UT 84321
send icon
Email Address
sales@campbellsci.com

As a trusted leader in measurement systems for more than 50 years, our top priority at Campbell Scientific has always been to provide the most accurate, reliable data for research and industry.