Fredx
July 8, 2020, 11:13pm
1
Hello!
I know it has been long dicussed and a lot of posts have been made about logging to SD card and saving the file as date/time. But I still can't manage to get it work. I am using Arduino Duemilanove with Uno bootloader, SD card module, LM35 temperature sensor and Adafruit DS3231 clock module.
So this is my simplified code, which works:
#include <RTClib.h>
#include <SD.h>
#include <SPI.h>
RTC_DS3231 rtc;
int val;
int tempPin = 1;
File myfile;
void setup () {
if (!SD.begin(10)) {while (1);}
myfile = SD.open("TEMPERATURE.TXT", FILE_WRITE);
}
void loop () {
DateTime now = rtc.now();
val = analogRead(tempPin);
float mv = ( val/1024.0)*5000;
float tempc = mv/10;
myfile.print(now.hour(), DEC);
myfile.print(":");
myfile.print(now.minute(), DEC);
myfile.print(" ");
myfile.println(tempc,DEC);
myfile.close();
delay(100000);
}
At the moment it is saving all data into single file, but I would like to make each day a new file.
Something like:
File myfile;
char fileName[12];
sprintf(fileName, "%d%02d%02d.txt", year, month, day);
void setup () {
getFileName();
myfile = SD.open(filename, FILE_WRITE);
myfile.println(",");
myfile.close();
}
void loop () {
getFileName();
day = bcdToDec(now.day());
month = bcdToDec(now.month());
year = bcdToDec(now.year());
myfile = SD.open(filename, FILE_WRITE);
myfile.print("testing123");
myfile.close();
}
Here is some code with some of the irrelevant stuff deleted. No library is used for the RTC. No changes required for DS3231.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PCD8544.h>
#include "Wire.h"
#include <SD.h>
#include <SPI.h>
#define DS1307_ADDRESS 0x68
char filename[] = "00000000.CSV";
File myFile;
static PCD8544 lcd;
//_________________________________________
void setup() {
lcd.begin(84, 48);
Wire.begin();
Serial.begin(9600);
Serial2.begin(9600);
GetClock();
today = day; // flag for later midnight check
getFileName();
lcd.setCursor(0,3);
lcd.println(filename);
delay(2000);
}
//________________________________________________________________
void loop() {
GetClock();
if (today != day)
{
today = day;
getFileName();
}
}
while (Serial2.available())
{
delay(3);
char c = Serial2.read();
readString += c;
}// end while
if (readString.length() >0)
{
getDump();
readString="";
} // end if
sensors.requestTemperatures();
power();
StringOut();
LCDdata();
k=k+1;
if ((k>9 ) && flowRate > 1)
{
WriteSD();
k=0;
p=p+1;
if (p>90)
{
supply = InTemp;
p=0;
}
if (OutTemp>maxOut)
{
maxOut = OutTemp;
}
}
pulseCount=0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
} // loop ends here
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void getFileName(){
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
sprintf(dailydate, "%02d%02d%02d", year, month, day);
}
void GetClock(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
day = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
Fredx
July 9, 2020, 7:59pm
3
Thank you! But I still can't get it work. Can someone please look my testcode:
#include <RTClib.h>
#include <SD.h>
#include <SPI.h>
RTC_DS3231 rtc;
char filename[] = "00000000.CSV";
File myfile;
void setup () {
Serial.begin(9600);
if (!SD.begin(10)) {while (1); }
if (! rtc.begin()) {Serial.println("Couldn't find RTC"); Serial.flush(); abort(); }
if (rtc.lostPower()) { Serial.println("RTC lost power, let's set the time!"); }
}
void loop () {
DateTime now = rtc.now();
sprintf(filename, "%02d%02d%02d.csv", now.day(), now.month(), now.year());
myfile = SD.open(filename, FILE_WRITE);
myfile.println("This is test.");
myfile.close();
delay(3000);
}
I can't comment any further because I don't know anything about the RTC library, but I notice that you are not defining the the clock,
#define DS1307_ADDRESS 0x68
which I imagine is a requirement irrespective of the library.
Fredx
July 10, 2020, 9:04am
5
Thank you. I managed to get it work using my last posted code.