Things wemos D1 arduino boards Application Notes 4- timer Fun

Foreword

In this section we learn WeMos D1 using a timer, the timer may be implemented using timing delay control function, PWM output, and the count of the detection of external events.

First, the basics

1.WeMos D1 timer Introduction

WeMos D1 timer realized by Ticker, Ticker is a timer library, timer function can be easily achieved by Ticker. IO is not currently recommended to prevent operation (network, serial, file) Ticker from the callback function. It is preferably provided a callback flag in the code, and then check the flag in the round functions.

2. WeMos D1 timer Description Function

Things wemos D1 arduino boards Application Notes 4- timer Fun

Second, examples

Examples proceeding two timers, a single timer is, 6 seconds after entering the timing of a single callback function, the other is a cyclic timing sequentially every 3 seconds into the callback function.

#include <Arduino.h>
#include <Ticker.h>

Ticker tk, tk1;

int timer_flag = 0;
int count = 0;

void timer_coming_once_cb(void)
{
 Serial.println("time coming only once.");
 tk1.detach();
}

void time_coming_cicyle_cb(void)
{
 timer_flag = 1;
 count++;
}

void setup() {
 Serial.begin(9600);
 tk1.once(6, timer_coming_once_cb);
 // call time_coming_cicyle_cb every 3s
 tk.attach(3, time_coming_cicyle_cb);
}

void loop() {
 if (1 == timer_flag)
 {
 Serial.println("time coming......");
 Serial.println(count);
 timer_flag = 0;

 if (10 == count)
 {
 Serial.println("stop timer");
 tk.detach();
 }
 }
}

The above-mentioned procedures, the use to attch (), detach (), once () function, use the same delay in milliseconds, but the timing of time becomes short.

Third, run

Click Upload, post-equipment operation Log are as follows:

Things wemos D1 arduino boards Application Notes 4- timer Fun

From the figure, we can now have a normal operation of the timer.

IV Conclusion

1. Summary:

End of this section, the actual operation needs attention are the following:

(1) Definition timer

If multiple timing mode, the result should be more defined type variable Ticker, as shown in the use of a single timing and the timing cycle, but also defines two variables, and tk respectively tk1.

2. Postscript:

If you have any questions in the course of further exchanges please add QQ group, mention may be github Issue.

QQ exchange group: 906 015 840 (NOTE: Internet of Things project communication)

Get the source: No public attention, you can reply wemos

Leaf lone sand Publisher: one grain of sand in the world, a Bodhi leaf

Things wemos D1 arduino boards Application Notes 4- timer Fun

Guess you like

Origin blog.51cto.com/14616151/2468255