【发布时间】:2021-02-12 14:02:59
【问题描述】:
我已经建立了一个自定义时钟/气象站。但出于几个目的,我希望能够在我的代码中运行计时器。我以前在 C++ 中使用 SDL 库 (SDL_GetTicks()) 做过很多次。而且我知道我可以在 Arduino IDE 中通过使用 millis() 以相同的方式获取滴答声。
所以我只是复制粘贴了我以前用于 SDL 程序的计时器类的代码,并将 SDL_GetTicks() 替换为 millis()。
但是,现在它说millis() 超出范围。哪个我不明白? 我不允许在成员变量中使用millis吗?
#include "Timer.h"
namespace Timer
{
void Timer::startTimer()
{
if (!this->timerStarted)
{
this->current_time = millis();
this->timerStarted = true;
}
}
unsigned int Timer::showTimePassed()
{
this->time_passed = millis();
return this->time_passed - this->current_time;
}
bool Timer::ifTimePassed(unsigned int timePassed)
{
this->last_time = millis();
if (!this->timerStarted)
{
this->current_time = millis();
this->timerStarted = true;
}
if (this->timerStarted == true && this->last_time >= this->current_time + timePassed)
{
timerStarted = false;
return true;
}
return false;
}
void Timer::setLoop()
{
this->timerStarted = true;
this->last_time = this->current_time;
this->current_time = millis();
this->time_passed = this->current_time - this->last_time;
}
unsigned int Timer::getLoopTime()
{
return this->time_passed;
}
}
【问题讨论】:
-
我确定您可以在类方法中使用函数
millis。您能否引用 exact 错误消息,这总是有帮助的,并指出错误消息适用于代码的哪一行。
标签: c++ scope arduino-ide