HITI Timer (manual-reset)

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

This example shows how to use a HITI Timer to control the duration of a task using non-blocking function, 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 manual-reset to trigger the lighting of the on-board LED for 2 seconds. The LED is turned on when the Timer starts and is turned off when the Timer ends. The Timer is reset using a Button. When the program starts, the Timer immediately runs once. Then an action on the Button is required to restart the Timer.


Note : Use a Timer in manual-mode to control the duration of a punctual task. 



Sketch

Upload this sketch : 2_Timing \ 2_TimerManualReset


We start our code by creating a HITI Timer variable (HC_Timer timer).

Then, during the setup(), we declare pin 13 as an output to control the on-board LED. We also set the Timer to manual-reset using manualReset().


#include <HITIComm.h>

// pins assignment
const byte pin_LED = LED_BUILTIN;

// Digital Data assignment
const byte dd_ResetButton = 0;

// Analog Data assignment
const byte ad_StartTime   = 0;
const byte ad_ElapsedTime = 1;

// HITI Timer
HC_Timer timer;

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

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

    // set Timer to manual reset mode
    timer.manualReset();
}


Inside the loop(), we start and run the Timer using its delay(duration) function. When the Timer starts, the LED is turned on. Then, after a duration of 2000ms, the Timer ends, and the LED is turned off. We use isStarting() to know when the Timer starts and delay(duration) to know when the Timer ends.


Then, we reset() the Timer if the Virtual Button has been clicked (using Command Panel DD0). 


Finally, we display the Timer Start Time and Elapsed Time in Command Panels AD0 and AD1. 


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

    // when Timer starts
    if (timer.isStarting()) 
        digitalWrite(pin_LED, HIGH); 

    // after 2000 ms
    if (timer.delay(2000))
        digitalWrite(pin_LED, LOW); 

    // if the Button is activated (from Command Panel DD0) 
    if (HC_digitalDataRead(dd_ResetButton) == HIGH)
    {
        // deactivate the Button
        HC_digitalDataWrite(dd_ResetButton, LOW);

        // reset Timer
        timer.reset();
    }

    // display Start Time and Elapsed Time (in Command Panels AD0 and AD1)
    HC_analogDataWrite(ad_StartTime,   timer.getStartTime());
    HC_analogDataWrite(ad_ElapsedTime, timer.getElapsedTime());
}

 


Control Panels

1) Display the DATA Control Panels.

2) Reset and restart the Timer. Check that the on-board LED does turn on for 2s. Also, check the Elapsed Time. Repeat this operation several times and see how the Timer Start Time increases each time. This value is referenced to the program start time. In our example, the last time the Timer was started is 21654ms after the program began to run.



 





 

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