# Timer

A timer class called *LTimer* is provided to expose the native LinkIt SDK timer functionality to Arduino developers. Please refer to the source code of **File / Examples / LTimer / BlinkLED** as an example. It blinks the **on-board LED** and an **external LED with P6 pin** by using two timers: one is changing the LED state in every 500ms and the other is changing in every 250ms. Here we introduce how this example is built and what action the corresponding APIs would take.

![](/files/jABAsBnVTijbeaFY7pYi)

### Instantiation <a href="#timer-instantiation" id="timer-instantiation"></a>

nclude the header file first before using the LTimer class.

```
#include "LTimer.h"
```

Developers can use up to **two** timers at a time in the code. The corresponding enumeration IDs are *LTIMER\_0* and *LTIMER\_1*. To use timers in the code, the first step is to create the instance of the timers. This code instantiates both *LTIMER\_0* and *LTIMER\_1*:

```
LTimer timer0(LTIMER_0);
LTimer timer1(LTIMER_1);
```

### Initialization <a href="#timer-initialization" id="timer-initialization"></a>

After the timer instances are created, they need to be initialized. Call the *.begin()* function to initialize timers in the setup() part:

```
timer0.begin();
timer1.begin();
```

### Execution <a href="#timer-execution" id="timer-execution"></a>

When the initialization is done, you can start the timer by calling the *.start()* function. Four parameters are needed to start a timer:

```
LTimerStatus start(
  uint32_t timeoutMS,
  LTimerMode timerMode,
  ltimer_callback_t callbackFunc,
  void *userData
);
```

The purposes of parameters are:

* **\[IN]** *timeoutMS:* timeout time in milliseconds.
* **\[IN]** *timerMode*: one shot or repeat mode. The value can be *LTIMER\_ONESHOT\_MODE* or *LTIMER\_REPEAT\_MODE*. In the one shot mode, the callback function will be invoked only once when the timer is expired and in the repeat mode, the callback function would be invoked repeatedly for every *timeoutMS* interval until the timer is stopped / released.
* **\[IN]** *callbackFunc*: the callback function to be called when the timer is expired.
* **\[IN]** *userData*: data to be passed when the callback function is invoked.

And the function prototype of the callback function is:

```
typedef void (*ltimer_callback_t)(void *user_data);
```

The input parameter *user\_data* would be the same data as the *userData* passed when starting a timer.

### Stop <a href="#timer-stop" id="timer-stop"></a>

For a one shot mode timer, there is no need to stop it since it only runs once. However, for a repeat mode timer, developers might want to stop it in some situations. The *.stop()* function can be used to stop a timer. If the timer is needed later, another *.start()* can be called again to trigger its operation.

### Release <a href="#timer-release" id="timer-release"></a>

When a timer is no longer needed, the *.end()* function is used to release all resources allocated to it.

### Example Code <a href="#timer-examplecode" id="timer-examplecode"></a>

```
#include "Arduino.h"
#include "LTimer.h"

int led0 = 7;
int led1 = 6;
int val0 = 0;
int val1 = 0;

// instantiation
LTimer timer0(LTIMER_0);
LTimer timer1(LTIMER_1);

// callback function for timer0
void _callback0(void *usr_data)
{
  val0 = !val0;
  digitalWrite(led0, val0);
}

// callback function for timer1
void _callback1(void *usr_data)
{
  val1 = !val1;
  digitalWrite(led1, val1);
}

void setup() {
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  Serial.begin(115200);

  // initialization
  timer0.begin();
  timer1.begin();

  // start the execution
  timer0.start(500, LTIMER_REPEAT_MODE, _callback0, NULL);
  timer1.start(250, LTIMER_REPEAT_MODE, _callback1, NULL);
}

void loop() {
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cavedu.gitbook.io/linkit-7697/linkit-7697-development-guide-for-arduino-ide/developer-guide/timer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
