HITI MultiTimer (auto-reset)

Modified on Wed, 04 Jan 2023 at 10:26 PM

This example shows how to use a HITI MultiTimer to control the durations of several consecutive tasks using non-blocking function, and to trigger events when a task starts or stops. Use it inside the loop() as a replacement for delay() which is a blocking function.


A HITI MultiTimer is a variable which holds several HITI Timers.


To illustrate this, let's use a MultiTimer in auto-reset to build a coded light signal emitted repeatedly by the on-board LED.


Note : Use a MultiTimer in auto-reset for consecutive tasks that are repeated.



Sketch

Upload this sketch : 2_Timing \ 3_MultiTimerAutoReset


We start our code by including the MultiTimer library, and by creating a MultiTimer variable containing 4 Timers, using HC_MultiTimer timers(quantity). The MultiTimer is by default in auto-reset mode.


Then during the setup(), we declare pin 13 as an output to control the on-board LED


#include <HITIComm.h>
#include <HC_MultiTimer.h>

// pins assignment
const int pin_LED = LED_BUILTIN;

// HITI MultiTimer with 4 Timers
HC_MultiTimer timers(4);

void setup()
{
    // initialize library
    HC_begin();

    // set pins mode
    pinMode(pin_LED, OUTPUT);
}


Inside the loop(), we start and run the Timers one after the other using delay(timer index, duration). When one Timer ends, the next one is started. When all Timers has ended, the MultiTimer automatically resets and restarts. We use isStarting() to detect when the first Timer starts and delay(timer index, duration) to detect when a specific Timer ends. Each time, the LED is turned on or off accordingly.


Finally, we display Timers Elapsed Times in Command Panels AD0 to AD3.


void loop()
{
    // communicate with HITIPanel
    HC_communicate();

    // when Timer 0 starts
    if (timers.isStarting())
       digitalWrite(pin_LED, HIGH); // LED is turned ON

    // after 250 ms (when Timer 0 ends)
    if (timers.delay(0, 250))
       digitalWrite(pin_LED, LOW); // LED is turned OFF

    // after 500 ms (when Timer 1 ends)
    if (timers.delay(1, 500))
       digitalWrite(pin_LED, HIGH); // LED is turned ON

    // after 1000 ms (when Timer 2 ends)
    if (timers.delay(2, 1000))
       digitalWrite(pin_LED, LOW); // LED is turned OFF

    // after 1500 ms (when Timer 3 ends), do nothing
    timers.delay(3, 1500);

    // display Elapsed Times (Timers 0 to 3 => AD0 to AD3)
    for (byte i = 0; i <= 3; i++)
       HC_analogDataWrite(i, timers.getTimer(i).getElapsedTime());
}

 

The equivalent code written using the standard blocking delay() function would be:


void loop()
{
    // communicate with HITIPanel
    HC_communicate();

    digitalWrite(pin_LED, HIGH); // LED is turned ON
    delay(250);

    digitalWrite(pin_LED, LOW); // LED is turned OFF
    delay(500);

    digitalWrite(pin_LED, HIGH); // LED is turned ON
    delay(1000);

    digitalWrite(pin_LED, LOW); // LED is turned OFF
    delay(1500);
}



Control Panels

1) Display the I/O Control Panels

2) Check that the on-board LED emits the coded light signal as expected.

 


3) Display the DATA Control Panels

4) Check the elapsed times of the 4 Timers managed by the MultiTimer. See how the Timers are successively and repeatedly activated.

 


 

Chart

1) Open the Chart window.

2) Start data acquisition.

3) New data are added every 10ms and only the last 3s are displayed. The LED state is plotted, and the coded signal is clearly visible: on (250ms), off (500ms), on (1000ms), off (1500ms).

 


4) Stop data acquisition.

5) Measure the whole duration of the signal. You should retrieve approximately 3250ms (= 250ms + 500ms + 1000ms + 1500ms). In our example, we find 12.440s – 9.180s = 3.260s = 3260ms.

 



You might not retrieve the exact value since the precision of this measurement is limited by several parameters: 

  • The clock used to add data to the plot is the computer clock while the clock used by the MultiTimer in your code is the Arduino on-board clock. These 2 clocks are slightly different, and a time drift may build up between them (something like 1 or 2ms per second).
  • The resolution of the MultiTimer relies on the loop() cycle time, that is, the time took by the Arduino CPU to complete 1 execution of the loop(). If this time becomes too high, because for instance of the serial communication, the Timers will have difficulty to detect precisely when they should end. See here how to measure the loop() cycle time.






Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article