Ticker

请使用前加载<Ticker.h>

once()

功能:

在设定的时间点(s)只执行一次回调函数.

函数原型:

void once(float seconds, callback_t callback)

参数 类型 描述
seconds float 设置执行时间
callback callback_t 回调函数

使用示例:

#include <M5Stack.h>
#include <Ticker.h>

Ticker ticker;  // 建立Ticker用于实现定时功能.  Build Ticker to implement timing function

// 在Tinker对象控制下,此函数将会定时执行.
// Under the control of the Tinker object, this function will be executed periodically
void sayHi() {
    Serial.print("Hi,Ticker!");
}

void setup() {
    M5.begin();
    // 第三秒时调用sayHi函数一次. The sayHi function is called once at the third second
    ticker.once(3, sayHi);
}

void loop() {
    // 在Tinker对象控制下,M5Stack可以定时执行其它任务.
    // Under the control of Tinker object, M5Stack can perform other tasks periodically
    Serial.println("Ticker");
    delay(1000);
}

once_ms()

功能:

在设定的时间点(ms)回调函数只运行一次.

函数原型:

void once_ms(uint32_t milliseconds, callback_t callback)

参数 类型 描述
milliseconds float 设置执行时间
callback callback_t 回调函数

使用示例:

#include <M5Stack.h>
#include <Ticker.h>

Ticker ticker;  // 建立Ticker用于实现定时功能.  Build Ticker to implement timing function

// 在Tinker对象控制下,此函数将会定时执行.
// Under the control of the Tinker object, this function will be executed periodically
void sayHi() {
    Serial.println("Hi,Ticker!");
}

void setup() {
    M5.begin();
    // 第3000ms时调用sayHi函数一次. Call the sayHi function once at 3000ms
    ticker.once_ms(3, sayHi);
}

void loop() {
    // 在Tinker对象控制下,M5Stack可以定时执行其它任务.
    // Under the control of Tinker object, M5Stack can perform other tasks periodically
    Serial.println("Ticker");
    delay(1000);
}

attach()

功能:

每隔一定时间(s)执行一次回调函数

函数原型:

void attach(float seconds, callback_t callback)

参数 类型 描述
seconds float
callback callback_t 回调函数名称

使用示例:

#include <M5Stack.h>
#include <Ticker.h>

Ticker ticker;  // 建立Ticker用于实现定时功能.  Build Ticker to implement timing function
int count;  // 计数用变量.  variable for counting

// 在Tinker对象控制下,此函数将会定时执行. Under the control of the Tinker
// object, this function will be executed periodically
void sayHi() {
    count++;
    Serial.print("Hi ");
    Serial.println(count);
}

void setup() {
    M5.begin();
    // 每隔一秒钟调用sayHi函数一次. Call the sayHi function every one second
    ticker.attach(1, sayHi);
}

void loop() {
    // 在Tinker对象控制下,M5Stack可以定时执行其它任务. Under the control of
    // Tinker object, M5Stack can perform other tasks periodically
    Serial.println("Ticker");
    delay(3000);
}

attach_ms()

功能:

每隔一定时间(ms)执行一次回调函数

函数原型:

void attach_ms(uint32_t milliseconds, callback_t callback)

参数 类型 描述
milliseconds float 毫秒
callback callback_t 回调函数名称

使用示例:

#include <M5Stack.h>
#include <Ticker.h>

Ticker tickerSetHigh;
Ticker tickerSetLow;

void display(int number) {
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.print(number);
}
void setup() {
  M5.begin();
  tickerSetLow.attach_ms(1000, display, 0);
  tickerSetHigh.attach_ms(2000, display, 1);
}
void loop() {
}

attach()

功能:

每隔一段时间(s)执行一次带参数的回调函数.

函数原型:

void attach(float seconds, void (*callback)(TArg), TArg arg)

参数 类型 描述
seconds float
*callback callback_t 带参数回调函数
TArg arg 函数参数

使用示例:

#include <M5Stack.h>
#include <Ticker.h>

Ticker tickerSetHigh; // 建立Ticker用于实现定时功能.  Build Ticker to implement timing function
Ticker tickerSetLow;

void display(int number) {
    M5.Lcd.setCursor(0, 0);
    M5.Lcd.print(number);
}
void setup() {
    M5.begin();
    tickerSetLow.attach(1, display, 0); //每隔1s执行一次带参数的回调函数. Execute a callback function with parameters every 1s
    tickerSetHigh.attach(2, display, 1);
}
void loop() {}

attach_ms()

功能:

每隔一段时间(ms)执行一次带参数的回调函数

函数原型:

void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)

参数 类型 描述
milliseconds float 毫秒
*callback callback_t 带参数回调函数
TArg arg 参数

使用示例:

#include <M5Stack.h>
#include <Ticker.h>

Ticker tickerSetHigh;
Ticker tickerSetLow;

void display(int number) {
    M5.Lcd.setCursor(0, 0);
    M5.Lcd.print(number);
}
void setup() {
    M5.begin();
    tickerSetLow.attach_ms(1000, display, 0);
    tickerSetHigh.attach_ms(2000, display, 1);
}
void loop() {}
On This Page