Arduino编译报错:DateTime类无get成员及SD库相关问题咨询
Arduino编译错误修复方案
错误原因与修复步骤
1. 'class DateTime' has no member named 'get' 错误
RTClib库更新后移除了DateTime.get()方法(原用于获取2000年以来的秒数)。替代方案:
- 计算2000年起始的秒数:用
now.unixtime() - DateTime(2000, 1, 1).unixtime()替换所有now.get()调用 - 若无需严格从2000年开始,可直接使用
now.unixtime()获取Unix时间戳(1970年起始)
2. logfile.writeError 错误
Arduino的File类没有名为writeError的成员属性,正确的错误检查方式是调用getWriteError()方法:
- 将
logfile.writeError替换为logfile.getWriteError()
修改后的关键代码片段
时间戳输出(loop函数内)
// 替换原now.get()代码 logfile.print(now.unixtime() - DateTime(2000, 1, 1).unixtime()); // 2000年以来的秒数 logfile.print(", ");
串口同步修改:
Serial.print(now.unixtime() - DateTime(2000, 1, 1).unixtime()); // 2000年以来的秒数 Serial.print(", ");
写入错误检查(setup函数内)
#if ECHO_TO_SERIAL // 修复错误判断逻辑 if (logfile.getWriteError() || !logfile.sync()) { error("write header"); } #endif
完整可编译代码
/* The (UNO) circuit: Define Uno digital pin 2 as RX - connect to TFMini TX (brown) Define Uno digital pin 3 as TX - connect to TFMini RX (blue) GND - TFMini black 5V - TFMini red */ #include "SoftwareSerial.h" #include "TFMini.h" #include "RTClib.h" #include "SD.h" #include <Wire.h> #define LOG_INTERVAL 1000 // milliseconds between sensor readings #define ECHO_TO_SERIAL 1 // echo data to serial port #define WAIT_TO_START 0 // Wait for serial input in setup() // Setup software serial port SoftwareSerial Serial1(2,3); //define software serial port name as Serial1 and define pin2 as RX and pin3 as TX TFMini tfmini; // DATA LOGGER RTC_DS1307 RTC; // define the Real Time Clock object char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; const int chipSelect = 10; // for the data logging shield, we use digital pin 10 for the SD cs line File logfile; // the logging file void error(char *str) // prints out the error to the Serial Monitor, turns on the red error LED // and then sits in a while(1); loop forever, also known as a halt { Serial.print("error: "); Serial.println(str); // red LED indicates error // digitalWrite(redLEDpin, HIGH); while(1); } void setup() { // Step 1: Initialize hardware serial port (serial debug port) Serial.begin(9600); //set bit rate of serial port connecting Arduino with computer while (!Serial); // wait for serial port to connect. Needed for native USB port only Serial.println ("Initializing TF03 LiDAR..."); // Step 2: Initialize the data rate for the SoftwareSerial port (set bit rate of serial port connecting LiDAR with Arduino) Serial1.begin(TFMINI_BAUDRATE); // Step 3: Initialize the TF Mini sensor tfmini.begin(&Serial1); #if WAIT_TO_START Serial.println("Type any character to start"); while (!Serial.available()); #endif //WAIT_TO_START // Initialize the SD card Serial.print("\nInitializing SD card..."); pinMode(10, OUTPUT); // make sure that the default chip select pin is set to output, even if you don't use it // see if the card is present and can be initialized: if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); // don't do anything more: return; } Serial.println("card initialized."); // create a new file char filename[] = "LOGGER00.CSV"; for (uint8_t i = 0; i < 100; i++) { filename[6] = i/10 + '0'; filename[7] = i%10 + '0'; if (! SD.exists(filename)) { // only open a new file if it doesn't exist logfile = SD.open(filename, FILE_WRITE); break; // leave the loop! } } if (! logfile) { error("couldnt create file"); } Serial.print("Logging to: "); Serial.println(filename); // kick off the RTC by initializing the Wire library and poking the RTC to see if its alive. Wire.begin(); if (!RTC.begin()) { logfile.println("RTC failed"); #if ECHO_TO_SERIAL Serial.println("RTC failed"); #endif //ECHO_TO_SERIAL } // Print the header (first line of the file) // helps your spreadsheet or math program identify whats coming up next. // The data is in CSV (comma separated value) format so the header is too: "millis,time" // The first item millis is milliseconds since the Arduino started, // Time is the time and date from the RTC // The logfile.print() call is what writes data to our file on the SD card, // it works pretty much the same as the Serial version logfile.println("millis,time,distance,strength"); #if ECHO_TO_SERIAL Serial.println("millis,time,distance,strength"); #endif #if ECHO_TO_SERIAL// attempt to write out the header to the file if (logfile.getWriteError() || !logfile.sync()) { error("write header"); } #endif } void loop() { DateTime now; // delay for the amount of time we want between readings delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // log milliseconds since starting uint32_t m = millis(); logfile.print(m); // milliseconds since start logfile.print(", "); #if ECHO_TO_SERIAL Serial.print(m); // milliseconds since start Serial.print(", "); #endif // fetch the time now = RTC.now(); // log time logfile.print(now.unixtime() - DateTime(2000, 1, 1).unixtime()); // seconds since 2000 logfile.print(", "); logfile.print(now.year(), DEC); logfile.print("/"); logfile.print(now.month(), DEC); logfile.print("/"); logfile.print(now.day(), DEC); logfile.print(" "); logfile.print(now.hour(), DEC); logfile.print(":"); logfile.print(now.minute(), DEC); logfile.print(":"); logfile.print(now.second(), DEC); #if ECHO_TO_SERIAL Serial.print(now.unixtime() - DateTime(2000, 1, 1).unixtime()); // seconds since 2000 Serial.print(", "); Serial.print(now.year(), DEC); Serial.print("/"); Serial.print(now.month(), DEC); Serial.print("/"); Serial.print(now.day(), DEC); Serial.print(" "); Serial.print(now.hour(), DEC); Serial.print(":"); Serial.print(now.minute(), DEC); Serial.print(":"); Serial.print(now.second(), DEC); #endif //ECHO_TO_SERIAL // Take one TF Mini distance measurement uint16_t dist = tfmini.getDistance(); uint16_t strength = tfmini.getRecentSignalStrength(); // log ditance and signal strength logfile.print(", "); logfile.print(dist); // cm logfile.print(", "); logfile.println(strength); #if ECHO_TO_SERIAL Serial.print(", "); Serial.print(dist); Serial.print(" cm sigstr: "); // Serial.print(", "); Serial.println(strength); #endif //ECHO_TO_SERIAL }
内容的提问来源于stack exchange,提问作者sophivar
相关产品推荐
相关产品推荐

