I have been following a few previous posts, but unable to get the RTC value to the file name.
My code below is a bit of patchwork from a few tutorials:
// We need the SPI and SD libraries
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
byte second, minute, hour, weekDay, day, month, year;
// Is card inserted
#define SDmissing 7
// Some random input value
#define dataPin A0
// Logging sequence number (could be date/time from RTC)
int logNo = 1;
char filename[] = "00000000.CSV";
File dataLog;
//----------------------------------------------
// SETUP SETUP SETUP SETUP SETUP
//----------------------------------------------
void setup() {
// Serial monitor window
Serial.begin(9600);
Wire.begin();
RTC.begin();
Serial.println("Setup begins");
// SD inserted will be LOW when inserted and HIGH when missing
pinMode(SDmissing, INPUT_PULLUP);
// Common routine to connect to SD card
initSDCard();
Serial.println ("SD Card initialisation completed successfully");
}
//----------------------------------------------
// LOOP LOOP LOOP LOOP LOOP LOOP
//----------------------------------------------
void loop() {
DateTime now = RTC.now();
// Get some psuedo data value from somewhere
int dataVal = analogRead(dataPin);
String dataString = "";
if (!digitalRead(SDmissing)) {
// Now able to write to file
dataLog = SD.open(filename, FILE_WRITE);
// Log data
if (dataLog) {
dataString = String(logNo);
dataString += " data value now ";
dataString += String(dataVal);
Serial.print(",");
Serial.println(dataString);
dataLog.println(dataString);
// Close file
dataLog.close();
// Increment log msg no
logNo++;
}
else {
initSDCard();
}
}
else {
initSDCard();
}
}
//----------------------------------------------
// Common SD card initialisation routine
//----------------------------------------------
void getFileName(){
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
// Artificial delay to reduce data written to card!
delay(1000);
}
void initSDCard()
{
// Expected configuration for SPI/SD library:
// MOSI – pin 11
// MISO – pin 12
// SCK (CLK) – pin 13
// CS – pin 10
while (!SD.begin()) {
Serial.println("------------------------------");
Serial.println("SD Card not found / responding");
Serial.println("Insert a formatted SD card");
Serial.println("------------------------------");
SD.end();
delay(1000);
}
return;
}
I have a system where the SD card can be removed and re-inserted, which collects the data without the need for restarting the arduino uno board. My aim is to get the current RTC value as the initial file name.
Anyone able to point me in the right direction on how to achieve this?