簡體   English   中英

如何在 Arduino 中使用時間庫?

[英]How to use time library in Arduino?

我正在嘗試將這個簡單的程序上傳到我的 Arduino 以獲取時鍾時間:

#include <Time.h>

time_t nowTime;

void setup() {
    nowTime = now();
}

但是,它無法編譯:

exit status 1
'now' was not declared in this scope

為什么now()沒有在這個范圍內聲明? 包括 Time.h 文件。 那么為什么不聲明now()呢? 我怎樣才能解決這個問題?

編譯失敗,因為編譯器找到的 Time.h 文件與 Paul Stoffregen ( https://github.com/PaulStoffregen/Time ) 等時間庫無關。

我嘗試了為 Arduino Uno 編譯的 Sketch,並看到了與您看到的相同的錯誤:Time.h 已解析(該文件存在於某處),但 now() 未由該 Time.h 定義

在我的 Windows PC 上搜索了一段時間后,我終於找到了我認為 #include 包含在我的安裝中的文件:C:\\Users\\Brad\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.2 \\firmwares\\wifishield\\wifiHD\\src\\time.h 或者 C:\\Users\\Brad\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino5\\avr\\include \\time.h

這些文件都沒有定義 now() 函數。

如果您想使用 Paul Stoffregen 的 Time 庫,請從https://github.com/PaulStoffregen/Time下載並安裝它。 相反,如果您希望使用 Michael Margolis 的時間庫,您可以在 Arduino IDE 中的工具/管理庫...下找到並安裝它,然后在搜索詞中輸入“時間”(不帶引號)。

正如其他人指出的那樣,Arduino 環境並不總是知道當前的日期和時間。 函數 mills() 和 micros() 分別返回自 Arduino 啟動以來的毫秒數或微秒數。 僅僅看時間的流逝,大多數人使用millis() 或micros() 而不是更復雜的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM