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
}
}
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.