Compilation error: request for member 'begin' in 'rtc', which is of non-class type 'DS3231()'

I just started using Arduino. Now, I am trying to use SD card module to a Arduino Pro Mini 328 to create, record, and delete data which came from a sensor. As the suggest, I am having an error and I can't figure out what to do in it. Pls help.

/*
SENSORS
IDK AKDNL KNEKFNQW:KFNQ:KFN
*/

//Lib - SD Card
#include <SD.h>
#include <DS3231.h> //Clock

//Var - SD Card
#define SD_CS_PIN 10

DS3231 rtc(); //NSITIC
File dataFile;


void setup() {
    //SD Card
      Serial.begin(9600);
      pinMode(SD_CS_PIN, OUTPUT);
      if (!SD.begin(SD_CS_PIN)) {
        Serial.println("SD card initialization failed!");
        return;
        }
      Serial.println("SD card initialization done.");
      rtc.begin();
}

void loop() {
    //SD Card
      DateTime now = rtc.now();
      String filename = String(now.year(), DEC) + String(now.month(), DEC) + String(now.day(), DEC) + ".txt";
      if (!SD.exists(filename)) {
       dataFile = SD.open(filename, FILE_WRITE);
       dataFile.println("Pulse rate data for " + String(now.month(), DEC) + "/" + String(now.day(), DEC) + "/" + String(now.year(), DEC));
       dataFile.close();
      }
      dataFile = SD.open(filename, FILE_WRITE);
      if (dataFile) {
        // record pulse rate
        int pulseRate = get_pulse_rate(); // replace this with your actual code to get pulse rate
        dataFile.println(now.hour(), DEC) + ":" + String(now.minute(), DEC) + ":" + String(now.second(), DEC) + "," + String(pulseRate);
        dataFile.close();
      } else {
        Serial.println("Error opening file " + filename);
      }

      // delete files older than a month
      DateTime aMonthAgo = now - TimeSpan(30, 0, 0, 0);
      String oldFilename = String(aMonthAgo.year(), DEC) + String(aMonthAgo.month(), DEC) + String(aMonthAgo.day(), DEC) + ".txt";
      if (SD.exists(oldFilename)) {
        SD.remove(oldFilename);
        Serial.println("Deleted file " + oldFilename);
        }
      delay(60000); // wait for a minute
      }

      int get_pulse_rate() {
      // replace this with your actual code to get pulse rate
      return random(60, 120); // simulate pulse rate between 60 and 120 bpm
      }
}

There is no .begin() for this class, you can delete that line.

1 Like

Oh.
Then, the other problem became:
Compilation error: 'class DS3231' has no member named 'now'
for
DateTime now = rtc.now();

I need it
because it will affect the other code below it.
How do I make it better?

Try

DateTime now = RTClib.now();
1 Like

It is still an error :confused:,
Compilation error: expected primary-expression before '.' token

When the constructor has no argument you MUST leave off the parens:
DS3231 rtc; //NSITIC

The compiler saw the parens and decided you were declaring a function named 'rtc' that returned a value of type DS3231.

Take out the parens and leave in the "rtc.begin();"

1 Like

Compilation error: 'class DS3231' has no member named 'begin'

Which library? Does your use of it slavishly follow any example code it came with?

Did you get the RTC to work with example code you had nothing to do with writing?

Looks like the wrong library.

a7

NOW remove the "rtc.begin();" line. :slight_smile:

Please post the updated code.

Also please copy the entire set of error messages and paste them into your post, between code tags (but not the same tags as your code).

The
DateTime now = rtc.now();
has errors of

In file included from C:\Users\Lenovo\AppData\Local\Arduino15\RemoteSketchbook\ArduinoCloud\4b247300-0260-413c-87b7-c1024461a2d3\CAPSTONE\Rando\Rando.ino:9:0:
c:\Users\Lenovo\Documents\Arduino\libraries\DS3231/DS3231.h:33:7: error: redefinition of 'class DateTime'
 class DateTime {
       ^~~~~~~~
In file included from C:\Users\Lenovo\AppData\Local\Arduino15\RemoteSketchbook\ArduinoCloud\4b247300-0260-413c-87b7-c1024461a2d3\CAPSTONE\Rando\Rando.ino:8:0:
c:\Users\Lenovo\Documents\Arduino\libraries\RTClib\src/RTClib.h:143:7: note: previous definition of 'class DateTime'
 class DateTime {
       ^~~~~~~~

exit status 1

Compilation error: exit status 1

Because it needs definition, I think
I changed the DS3231 rtc; to RTC_DS3231 rtc; through the examples and use of #include <RTClib.h> which fixed alot of things.

This is the updated coding:

//Lib - SD Card
#include <SD.h>
#include <RTClib.h>
#include <DS3231.h> //Clock

//Var - SD Card
#define SD_CS_PIN 10

RTC_DS3231 rtc;
File dataFile;

void setup() {
    //SD Card
      Serial.begin(9600);
      pinMode(SD_CS_PIN, OUTPUT);
      if (!SD.begin(SD_CS_PIN)) {
        Serial.println("SD card initialization failed!");
        return;
        }
      Serial.println("SD card initialization done.");
}


void loop() {
    //SD Card
      DateTime now = rtc.now(); 
      String filename = String(now.year(), DEC) + String(now.month(), DEC) + String(now.day(), DEC) + ".txt";
      if (!SD.exists(filename)) {
       dataFile = SD.open(filename, FILE_WRITE);
       dataFile.println("Pulse rate data for " + String(now.month(), DEC) + "/" + String(now.day(), DEC) + "/" + String(now.year(), DEC));
       dataFile.close();
      }
      dataFile = SD.open(filename, FILE_WRITE);
      if (dataFile) {
        // record pulse rate
        int pulseRate = get_pulse_rate(); // replace this with your actual code to get pulse rate
        dataFile.println(now.hour(), DEC) + ":" + String(now.minute(), DEC) + ":" + String(now.second(), DEC) + "," + String(pulseRate);
        dataFile.close();
      } else {
        Serial.println("Error opening file " + filename);
      }

      // delete files older than a month
      DateTime aMonthAgo = now - TimeSpan(30, 0, 0, 0);
      String oldFilename = String(aMonthAgo.year(), DEC) + String(aMonthAgo.month(), DEC) + String(aMonthAgo.day(), DEC) + ".txt";
      if (SD.exists(oldFilename)) {
        SD.remove(oldFilename);
        Serial.println("Deleted file " + oldFilename);
        }
      delay(60000); // wait for a minute
      }

      int get_pulse_rate() {
      // replace this with your actual code to get pulse rate
      return random(60, 120); // simulate pulse rate between 60 and 120 bpm
      }

I was just searching what the error in my coding was.
One comment said to check sources for a conflicting definitions, and so it was.
The #include <RTClib.h> and #include <DS3231.h> was conflicting with each other.

Thank you for assisting me in coding. God Bless!

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