I'm hanging with a simple project that must have been done in millions over the last 10 years...
I have an old UNO (with the DIP ATMega328) and an RTC/SD-shield for it which is still sold from several suppliers. IDE is 2.2.1
But I can't get the SD functions and RTC functions working in the same sketch, to fullfill my simple aim: Logging some measurements/events with timestamps.
Writing something to the SD card works, using (built-in) SD.h.
Setting and reading the RTC works using RTC.h from "Manjunath CV".
But if I try to combine the two sketches, all readings from RTC like RTC.getDay() result in strange numbers, until I strip off anything which initializes the SD card.
What's the problem?
I found a YT tutorial that propagates another RTC lib, but I did not get how to install this lib. Simply copying the lib folder into the library folder did not work.
Edit:
I copied - according to the old YT tutorial - the alternative RTC lib into the Arduino lib folder in "Program Files(x86)"... obviously the latest IDE resides in "Program Files".
So this cannot work.
The folder structure in "Program Files/Arduino IDE" is much more complicated, how does it work to install a library which is not handled by the Library Manager?
Of course, after my first despair I kept on trying.
So I set the filter in the library manager to "recommended", and the search for "RTC" showed the Adafruit RTClib.h first in row, not Manjunath's RTC.h
Uninstalled the latter and installed Adafruit's - and the example code for a datalogger works.
And yes, the sketch includes not only SD.h and RTClib.h, but also Wire.h and SPI.h.
//more Information at: https://www.aeq-web.com/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
const int chipSelect = 10; //10 is default by shield, but normally on Pin 4
int interval = 5; //Log to SD Card every 5 seconds
long timer;
String timestring;
String mvalue;
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
delay(3000);
Serial.println("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("SD Card error");
return;
}
Serial.println("card initialized");
if (! rtc.begin()) {
Serial.println("No RTC found");
} else {
Serial.println("RTC clock found");
}
if (! rtc.isrunning()) {
Serial.println("RTC is not configured");
}
}
void loop() {
if ((timer + interval * 1000) < millis()) {
timer = millis();
get_logvalue(); //Get your value
get_time(); //Get time from RTC
write_data(); //Write value and Time to SD
}
}
void get_logvalue() {
mvalue = "My Value"; //mvalue is your log parameter eg. Temperature
}
void get_time(){ //Read Time from RTC
DateTime now = rtc.now();
timestring = now.day();
timestring += "-";
timestring += now.month();
timestring += "-";
timestring += now.year();
timestring += " ";
timestring += now.hour();
timestring += ":";
timestring += now.minute();
timestring += ":";
timestring += now.second();
Serial.println(timestring);
}
void write_data() { //Write to SD card
String dataString = mvalue + "," + timestring;
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error writing datalog.txt");
}
}
This is crazy.
Although a have a working example now, I wanted to find back to the situation where I was so despaired yesterday. Maybe I forgot to include Wire.h?
But I can't get back to the sketch that malfunctioned yesterday.
The malfunction was:
The RTC was (probably successfully) set to the date 23-10-2023.
But reading the day with String(RTC.getDay()) always gave me back "165".
Unless I stripped off everything which dealt with the SD card.
That's what I remember from yesterday.
Today, I can mistreat my code in any way - it still gives me back 23.
Even skipping Wire.h and SPI.h, and just including SD.h and Manjunath's I2C_RTC.h: Still works.