HITI Timer (auto-reset)

Modified on Wed, 4 Jan, 2023 at 10:23 PM

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


To illustrate this, let’s use a HITI Timer in auto-reset to control the blinking of the on-board LED at a specified rate. The Timer is automatically restarted every 0.5s and the LED state is toggled each time the timer ends. 


Note : Use a HITI Timer in auto-reset to control the duration of a repeated task. 



Sketch

Upload this program : 2_Timing \ 1_TimerAutoReset


We start our program by creating a HITI Timer variable (HC_Timer timer). The Timer 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>

// pins assignment
const int pin_LED = LED_BUILTIN;

// HITI Timer
HC_Timer timer;

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

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


Inside the loop(), we start and run the Timer using its delay(duration) function. When the Timer ends, it automatically resets and restarts. This entails that the Timer restarts every 500ms.


We also use delay(duration) to know when the Timer ends (this function returns true when the Timer ends). When this happens, we toggle the LED state.


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

    // run Timer 
    if (timer.delay(500))
    {
        // when Timer ends (every 0.5s) =>  toggle LED state
        if (digitalRead(pin_LED) == LOW)
            digitalWrite(pin_LED, HIGH);
        else
            digitalWrite(pin_LED, LOW);
    }
}


 

Control Panels

1) Display the I/O Control Panels.

2) Check that the on-board LED blinks as expected.


 

 

 

Chart

1) Open the Chart window.

2) Start data acquisition.

3) The LED state is plotted. New data are added every 10ms and only the last 3s are displayed. 



4) Stop data acquisition.

5) Zoom in on 1 positive oscillation by selecting with your mouse (left button) the area you want to zoom in.



6) Note the Time coordinates of the start and end values of a positive oscillation and subtract them. You should retrieve approximately the duration of the Timer. In our example, we have 22.660s - 22.160s = 0.500s = 500ms.




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

  • By default, the library requires 1 excess cycle to check the end of the timer. This will add few milliseconds to your measurement. To tackle this issue, you can set the timer to frequency generator mode. In this mode, the timer calculates when it ends, and the excess cycle is removed.
  • the clock used to add data to the plot is the computer clock while the clock used by the Timer 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 Timer 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 Timer will have difficulty to detect precisely when it 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 at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article