【问题标题】:millis(); Arduino "out of scope"毫秒(); Arduino“超出范围”
【发布时间】: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


【解决方案1】:

您必须在头文件中包含 arduino.h,这应该可以解决问题。

#include

【讨论】:

    猜你喜欢
    • 2019-08-20
    • 1970-01-01
    • 2023-03-22
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    相关资源
    最近更新 更多