Hello,
I intend to use RTC module to generate real-time date and time and assign those integer values to create new file in SD card for my data logger project. i converted the integers into string and then to character array and used with SD.open(); but i am unable to create the file in SD card. following is my code. Kindly suggest...
Thanks
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
RTC_DS1307 rtc;
const int chipSelect = 4;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
String fname = String (now.day()) + "/" + String (now.month()) + "/" + String (now.year()) + "_" + String (now.hour()) + ":" + String (now.minute()) + ":" + String (now.second()) + String (".txt");
char filename[30] = {0};
fname.toCharArray(filename, 30);
File dataFile = SD.open(filename, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("File created successfully @ " + fname);
dataFile.close();
// print to the serial port too:
Serial.println("File created successfully @ " + fname);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening " + fname);
}
Serial.println(filename);
while(1){}
}
test_RTC_SD.ino (1.74 KB)