HITI MultiTimer (manual-reset)

Modified on Thu, 5 Jan, 2023 at 1:53 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 manual-reset to trigger the punctual emission of a coded light signal by the on-board LED. The MultiTimer is reset using a "Virtual" Button. When the program starts, the MultiTimer immediately runs once. Then an action on the Button is required to restart the MultiTimer.


Note : Use a MultiTimer in manual-reset for consecutive tasks occurring punctually.



Sketch

Upload this sketch : 2_Timing \ 4_MultiTimerManualReset


We start our code by including the MultiTimer library and by creating a MultiTimer variable containing 4 Timers : HC_MultiTimer timers(quantity). 


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


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

// pins assignment
const int pin_LED = LED_BUILTIN;

// Digital Data assignment
const int dd_ResetButton = 0;

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

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

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

    // configure MultiTimer for manual reset
    timers.manualReset();
}


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. We use isStarting() to detect when the MultiTimer starts and delay(timer index, duration) to detect when a specific Timer ends. Each time, the LED is turned on or off accordingly. 


Then, we reset() the MultiTimer if the Virtual Button has been activated (from Command Panel DD0).


Finally, we display the Timers Start Times and 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);

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

        // reset MultiTimer
        timers.reset();
    }

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


 

Control Panels

1) Display the DATA Control Panels

2) Reset and restart the MultiTimer. Check that the on-board LED emits the coded light signal each time. Also, check the elapsed times of the 4 Timers and see how they are successively activated. Repeat this operation several times and see how the Start Times increases each time. These values are referenced to the program start 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