Hi (again),
I feel like everything is 2 steps forward one step back at the moment.
I'm basically trying to make a datalogger that logs accelerometer data once the ultrasonic sensor detects and object. Everything works fine if I just create a new file in the root folder each time there is a detection.
As soon I I introduce a daily folder to store each set of data for that day it starts to misbehave. (I'm using folders because I'm using an esp32 and sdFat isn't supported and so I have to use short filenames.)
The first set of data gets logged fine and the serial shows me the file path like this...
(//YYMMDD//HH_MM.csv)
//210731//19_12.csv
Once it tries to write a new file to the folder it creates the following path...
//210731//19_12.csv///19_13.csv
However if I reset the device it'll work as I want it but just the once, so it'll create a new file in the correct folder.
The double slash (//) seems to be a esp32-ism, I need one in the actual file path and one in the name for it work for it to write a file. Even if I want to just write to the root I still need the /.
If anyone can help me figure this out it would be appreciated.
I've simplified the recordData() as it's not the focus as it works fine.
#include <Wire.h>
#include <SPI.h>
#include "SD.h"
//-SD Card-
#define SD_CS = 5;
File dataFile;
//Filename
char fileName[20];//Time of vessel arrive time
char folderName[20]; //Dated folder for saving data
char file[20];//store file name
char folder[20];//store folder name
char fileDir[20];//full file path
char filePath[20];//full file path
void setup() {
Serial.begin(115200);
//SD Card
SD.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
createNewFolder();
}
void loop() {
if (distanceInMM <= 500) {
recordData();
}
}
void createFileName() {
DateTime now = rtc.now();
int nowHour = now.hour();
int nowMinute = now.minute();
sprintf(fileName, "/%02i_%02i.csv", nowHour, nowMinute);
delay(200);
strcpy(file, fileName);
}
void createNewFolder() {
DateTime now = rtc.now();
int nowYear = now.year();
int nowMonth = now.month();
int nowDay = now.day();
sprintf(folderName, "/%02i%02i%02i", nowYear - 2000, nowMonth, nowDay);
strcpy(folder, folderName);
SD.mkdir(folderName);
/*Serial.println(nowYear - 2000);
Serial.println(nowMonth);
Serial.println(nowDay);
Serial.println(folderName);*/
}
void getFilePath() {
createFileName();
strcat(fileDir, "/");
strcat(fileDir, folder);
strcat(fileDir, "/");
strcat(fileDir, file);
strcpy(filePath, fileDir);
}
void getAccelData() {
//Accel Sensor code in here
}
void recordData(){
getFilePath();
Serial.println(filePath);
Serial.println(folderName);
dataFile = SD.open(filePath, FILE_WRITE);
getAccelData();
dataFile.close();
}
Now I thought I could use SD.exist() then just write straight into the folder, but it doesn't seem to find the folder name, maybe because it's an array?
Thanks