Auto Reset

A HITI Timer is a variable which allows you to control the duration of a task using non-blocking functions and to trigger events accordingly. Inside the loop(), use it instead of delay() which is a blocking function that disturbs serial communication with HITIPanel.

A Timer is typically used in auto-reset to control the duration of a repeated task.

This example shows how to 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.

 

1) Sketch: 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;


// run once at power on
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.

// run repeatedly after setup()
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);
        }
    }
}

 

2) Control Panels

  1. Display the DIGITAL I/O Control Panel (“IO” button).
  2. Check that the on-board LED blinks as expected.
    image230.png

 

3) Chart

  1. Open the Chart window (CTRL+T or Tools\Chart).
  2. Start data acquisition.
  3. The LED state is plotted. New data are added every 10ms and only the last 3s are displayed.
    image231.png

  4. Stop data acquisition.
  5. Zoom in on 1 positive oscillation by selecting with your mouse the area you want to zoom in.
    image232.png

  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.
    image233.png

    image234.png

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 exist 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 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?
0 out of 0 found this helpful