arduino“编译错误:'class DateTime'没有名为'get'的成员”

问题描述 投票:0回答:1

我有 arduino uno r3 和 adafruit 数据记录器屏蔽 (https://learn.adafruit.com/adafruit-data-logger-shield/using-the-real-time-clock-3)。

下面是我的代码,但是当我尝试编译时,出现“编译错误:‘class DateTime’没有名为‘get’的成员”。

我还应该使用其他库吗?我认为这是 SD.h 库的问题,因为我使用 logfile.writeError() 函数也收到错误

/*
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.writeError || !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.get()); // 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.get()); // 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

}
logging arduino-uno sd-card real-time-clock
1个回答
0
投票

Adafruit 教程似乎已经过时了。

.get
已于 2010 年 5 月从库中删除
GitHub 上的示例已于 2010 年 6 月更改为使用
.unixtime()

“自 2000 年以来的秒数”的正确方法是
.secondstime()

因此将

now.get()
更改为
now.secondstime()
或将其删除。

© www.soinside.com 2019 - 2024. All rights reserved.