带伺服和RTC的Arduino

时间:2016-05-10 01:17:51

标签: arduino real-time-clock servo

我正在制作一个涉及RTC和伺服电机的项目,以便它只在某个时间开启。循环片段是:

void loop() {
    DateTime now = rtc.now();
    if (DateTime == 19:10) {
        //Some stuff
    } else {
        return();
    }
}

我的错误是:

Arduino: 1.6.8 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\User\Documents\Arduino\Servo_motor\Servo_motor.ino: In function 'void loop()':

Servo_motor:36: error: expected primary-expression before '==' token

  if (DateTime == 19:10) {

               ^

Servo_motor:36: error: expected ')' before ':' token

  if (DateTime == 19:10) {

                    ^

Servo_motor:45: error: expected primary-expression before '}' token

  }

  ^

Servo_motor:45: error: return-statement with a value, in function returning 'void' [-fpermissive]

Servo_motor:45: error: expected ';' before '}' token

Multiple libraries were found for "RTClib.h"
 Used: C:\Program Files (x86)\Arduino\libraries\RTClib
 Not used: C:\Users\User\Documents\Arduino\libraries\arduino_786051
exit status 1
expected primary-expression before '==' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

我真的很困惑。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

我将假设您正在使用位于here的Adafruit RTClib,因为这可能是可从IDE访问的,或者是教程将使用的。它也是另一个可用的RTClib的分支,因此这个答案可能与两者都有关。

如果您选中RTClib.h,您会找到DateTime和所有RTC课程的公开方法。如果您这样做,您会注意到没有operator==方法,通常这意味着您无法将其用作比较形式。

为了做你想做的事,你需要使用DateTime.minute()DateTime.hour()并单独比较它们。在你的循环块中,它看起来如下:

void loop() {
    DateTime now = rtc.now();
    if (now.hour() == 19 && now.minute() == 10) {
        //Some stuff
    } else {
        return;
    }
}

但是,这可能会运行相关代码很多次,因为每次循环运行时,此检查将在RTC滴答到19:10后的一分钟内成功。

答案 1 :(得分:0)

您似乎正在将该类型与常量(没有引号的时间)进行比较。

你的意思不是这样吗?

if (now == "19:10") {
相关问题