How can I generate a filename to use with SD.open( fileName, FILE_WRITE )?
I've spent the morning going through forum posts and the online guides for using variable names for SD.open() calls. Some of the posts predate the library's update to accept String parameters. (Using a char[] causes a compiler error, so the API that uses that info seems to be deprecated.)
The error I'm tending to get is:
C:\Program Files (x86)\Arduino\libraries\SD\src\SD.cpp:456:12: warning: 'pathidx' may be used uninitialized in this function [-Wmaybe-uninitialized]
filepath += pathidx;
^
C:\Program Files (x86)\Arduino\libraries\SD\src\SD.cpp:450:7: note: 'pathidx' was declared here
int pathidx;
I am definitely setting default values for the fileName. If I use a hard-coded value for the file name, it works fine. If I use a variable, it throws the warning and doesn't write to the SD card. The String is using the 8.3 format.
The plan is to grab an incrementing number from EEPROM so it gives a unique filename every power cycle.
String logData = "";
unsigned long timeTracker;
int intervalTimer = 0;
int shockPosition;
unsigned int heartbeat = 0;
bool SDworking = false;
unsigned int heartBeatMax = 75;
String fileName = "SWDL_.txt";
//char fileName[] = "SWDL_000.txt";
File SDdataFile;
int logTracker = 0;
/*******************************************************************************
*
* setup()
*
* One-time run on power-up. Check that the SD card is available, and generate
* a filename from the EEPROM tracking info.
*
******************************************************************************/
void setup()
{
// see if the card is present and can be initialized:
pinMode( SDSelect, OUTPUT );
SDworking = SD.begin( SDSelect );
if( SDworking )
{
heartBeatMax = 500;
//Let's not burn extra EEPROM if there's no SD card.
EEPROM.get( trackingNumber, logTracker );
logTracker++;
if( logTracker > 999 )
{
logTracker = 0;
}
EEPROM.update( trackingNumber, logTracker );
}
}
/*******************************************************************************
*
* loop()
*
* Runs from top to bottom, the back to the top, as long as power is applied.
*
******************************************************************************/
void loop()
{
wdt_reset();
//This will flash super fast if the SD card is failed, or slow if it's fine.
if( heartbeat > heartBeatMax )
{
heartbeat = 0;
digitalWrite( LED_BUILTIN, !( digitalRead( LED_BUILTIN ) ) );
}
if( intervalTimer >= 10 )
{
intervalTimer = 0;
if( SDworking )
{
shockPosition = analogRead( positionSensor );
//It's the only way string concatenation seems to work on Arduino.
logData = "";
logData += timeTracker;
logData += ": ";
logData += shockPosition;
logData += "," ;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
SDdataFile = SD.open( fileName, FILE_WRITE );
// if the file is available, write to it:
if( SDdataFile )
{
SDdataFile.println( logData );
SDdataFile.close();
}
}
}
}