计时器#

介绍#

计时器会跟踪项目从开始到结束的时间间隔。它可以用来测量持续时间、在设定的时间后触发事件,或重置计时器以进行新的计时操作。以下是所有可用方法的列表:

动作——控制计时器。

  • 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 check button is pressed
    if (Brain.buttonCheck.pressing()) {
      Brain.Timer.reset();
    }
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Time: %.2f", Brain.Timer.value());
    wait(50, msec);
  }
}

盖特#

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 calls a function after a specified amount of time.

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

参数

描述

callback

当计时器事件发生时执行的函数。

delay

函数调用前的延迟,以毫秒为单位。

// Play a sound after 2 seconds
void tadaSound() {
  Brain.playSound(tada);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  Brain.Timer.event(tadaSound, 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 a new timer after two seconds
  wait(2, seconds);
  timer stopwatch;

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

    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Timer:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", Brain.Timer.value());

    Brain.Screen.setCursor(4, 1);
    Brain.Screen.print("Stopwatch:");
    Brain.Screen.newLine();
    Brain.Screen.print("%.3f", stopwatch.value());

    wait(100, msec);
  }
}