定时器#

介绍#

V5 Brain 的计时器会记录项目开始以来经过的时间。它可用于测量持续时间、在设定时间后触发事件,或重置以进行新的计时操作。以下是所有可用方法的列表:

操作——控制计时器。

  • reset — Resets the timer to zero.

Getter — 返回当前计时器值。

  • value — Returns the elapsed time since the timer started.

回调函数——延迟一段时间后触发函数。

  • event — Calls a function after a specified number of milliseconds, with optional arguments.

构造函数——创建一个计时器来跟踪时间。

  • Timer — Create a new timer object that can be used with these methods.

行动#

reset#

reset sets the timer to zero.

Usage:
Brain.Timer.reset();

参数

描述

此方法没有参数。

int main() {
  vexcodeInit();
  // Initializing Robot Configuration. DO NOT REMOVE!
  while (true) {
    // Reset the timer every time the screen is pressed
    if (Brain.Screen.pressing()) {
      Brain.Timer.reset();
    }
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Time: %.2f", Brain.Timer.value());
    wait(50, msec);
  }
}

Getter#

value#

value returns the current elapsed time of the timer in milliseconds as a double.

Usage:
Brain.Timer.value()

参数

描述

此方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display the timer as it ticks up
  while (true) {
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Time: %.2f", Brain.Timer.value());

    wait(50, msec);
  }
}

打回来#

event#

event registers a function to be called once after a specified delay.

Usage:
Brain.Timer.event(callback, delay)

参数

描述

callback

一个预先定义的回调函数,会在定时器超时时自动调用。该函数必须符合所需的回调函数签名。更多信息请参见回调函数

delay

调用回调函数之前等待的时间,以毫秒为单位的双精度浮点数。

Callback Signature:
void callback();

论点

描述

此回调函数没有参数。

// Turn the screen orange after 2 seconds
void turnOrange() {
  Brain.Screen.clearScreen(orange);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  Brain.Timer.event(turnOrange, 2000);
}

构造函数#

Timer#

timer creates a new timer. A new timer will start measuring time immediately when it is created.

Usage:
timer();

范围

描述

此构造函数没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Display both timers after two seconds
  wait(2, seconds);
  timer stopwatch;

  while (true) {
    Brain.Screen.clearScreen();

    // Show built-in timer
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Timer:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", Brain.Timer.value());

    // Show new timer (2 seconds delayed)
    Brain.Screen.setCursor(4, 1);
    Brain.Screen.print("Stopwatch:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", stopwatch.value());

    wait(100, msec);
  }
}