Redefinition of class DateTime

Hello All,
I am working on a program to log IR sensor data to an SD card with a timestamp using a DS3231 RTC, I have finally gotten the code to verify but gives an error stating "Error compiling for board Arduino Mega or Mega2560" as well as:

"In file included from C:\Users\jacob\OneDrive\Documents\Arduino\ReadWritei\ReadWritei.ino:7:0:
C:\Users\jacob\OneDrive\Documents\Arduino\libraries\DS3231/DS3231.h:27:7: error: redefinition of 'class DateTime'
class DateTime {
^~~~~~~~
In file included from C:\Users\jacob\OneDrive\Documents\Arduino\ReadWritei\ReadWritei.ino:4:0:
C:\Users\jacob\OneDrive\Documents\Arduino\libraries\RTClib/RTClib.h:81:7: note: previous definition of 'class DateTime'
class DateTime {
^~~~~~~~
"

I have no clue what is causing this error, and there is not much documentation on any of the libraries I am using, if any of you smart guys and gals can point me in the "Write" direction (couldn't help myself) it would be much obliged, I will post the entirety of my code at the bottom.
Thank you in advanced.

#include <Time.h>
#include <TimeLib.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS3231.h> // Include the library
#include <SPI.h>
#include <SD.h>
#define SENSOR 23 // define pint 2 for sensor
#define ACTION 22 // define pin 9 as for ACTION

File myFile;
//RTC_DS3231 rtc;
RTC_DS3231 rtc;

String TimeString() {
  DateTime now = rtc.now();
  String theyear = String(now.year(), DEC);
  String mon = String(now.month(), DEC);
  String theday = String(now.day(), DEC);
  String thehour = String(now.hour(), DEC);
  String themin = String(now.minute(), DEC);
  //Put all the time and date strings into one String
  String dataString = String( theyear + "/" + mon + "/" + theday + ", " + thehour + ":" + themin);

  return dataString;
}

// the setup function runs once when you press reset or power the board
void setup() {
  // E18-D80NK Obstacle Sensor Code by Robojax.com 2018022
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(SENSOR, INPUT_PULLUP);// define pin as Input  sensor
  pinMode(ACTION, OUTPUT);// define pin as OUTPUT for ACTION

  rtc.begin();
  if (! rtc.begin()) {//verifies RTC begins
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
}

// the loop function runs over and over again until power down or reset
void loop () {

  Serial.print ( "Initializing SD card..." );

  if (!SD.begin ( 53 )) {//change pin number
    Serial.println ( "initialization failed!" );
    while (1);
  }
  Serial.println ( "initialization done." );


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open ( "test.txt", FILE_WRITE );
  int L = digitalRead(SENSOR); // read the sensor
  if (L == LOW) {

    digitalWrite(ACTION, HIGH);
    // if the file opened okay, write to it:
    if (myFile) {
      Serial.print ( "Writing to test.txt..." );
      Serial.print (TimeString());
      myFile.print("Cat Passed at: ");
      myFile.print (TimeString());

      // close the file:
      myFile.close ();
      Serial.println ( "done." );
      delay(500);
    }
  } else {

    Serial.println("  === All clear");
    digitalWrite(ACTION, LOW); // turn the relay OFF
  }
}

As does the error message read, you got some conflicting libraries. Libraries that you are not using, that you are not using.

Are you using those libraries?

You certainly don't need to include the same library multiple times

#include <Time.h>
#include <TimeLib.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeLib.h>
//#include <DS3231.h> // Include the library

and the RTClib already has a class for the DS3231 so you don't need that

//#include <Time.h>
#include <TimeLib.h>
#include <RTClib.h>
//#include <Time.h>
//#include <TimeLib.h>
//#include <DS3231.h> // Include the library

Yeah, I tried various libraries to see which would work based off of other program examples, for example i dont know which library i am actually using for the line RTC_DS3231 rtc. With the lack of documentation for each RTC library and the fact that a lot of them us a class called DS3231 or RTC_DS3231, im not sure which to keep or remove. Im also not exactly clear on how the timeLib.h, Time.h or RTCLib.h work together. If you could explain that or tell me where to look for documentation on them it would help a lot. Thank you

I'd just open up the libraries and read the code.

Why not try just using one library for the RTC and comment out the rest and see what happens?

You should be using the DS3231.h or the RTClib.h Library, not both.
Time.h is a C++ standard library, so it is not useful with the DS3231 unless you have a specific purpose.
You have Time.h and TimeLib.h included twice.
Did you make sure that you have the correct RTC module?

You literally have the source code on your PC. Look at it. It is not hidden or sacred. It is just code you are using from someone else.

Possibly a bit much asked from a beginner :wink:

Maybe. But you could see what classes are in there, even if you don't understand it all.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.