SD card open second file.

Status
Not open for further replies.
I was going to put this on the Arduino forum, but seeing as it is using the Teensy SD library, I figured I would try here first. This is on a Teensy 3.6

What I am doing is I have two separate files I am working with. One is a general debug file and the other is a csv data file. The name of the debug file is constant, but the CSV file name is incremented each time the Teensy is reset.

I never have them both open at the same time. But nevertheless, it seems to be unable to initialize the CSV file.

Here is the relevant code. Sorry for a code dump:

Code:
void InitializeLogs()
{
	// Initialize SD card itself
	if (!SD.begin(BUILTIN_SDCARD))
	{
		Serial.println("! SD Card Error");
	}

	// Test Debug log file
	File debugFile = SD.open(DEBUG_LOG_NAME, FILE_WRITE);
	if (debugFile)
	{
		Serial.println(F("- Debug File Created"));
		debugFile.close();
	}
	else
	{
		Serial.println(F("! Debug File Error"));
	}


	// Generate datalog filename
	uint16_t DataLogNumber = 0;
	char candidateFilename[] = "DATA_00000.csv";
	while (SD.exists(candidateFilename))
	{
		DataLogNumber++;
		candidateFilename[5] = (DataLogNumber / 10000) % 10 + '0';
		candidateFilename[6] = (DataLogNumber / 1000) % 10 + '0';
		candidateFilename[7] = (DataLogNumber / 100) % 10 + '0';
		candidateFilename[8] = (DataLogNumber / 10) % 10 + '0';
		candidateFilename[9] = DataLogNumber % 10 + '0';
		if (DataLogNumber++ >= DATA_LOG_MAX)
		{
#ifdef DEBUG
			LOG_DEBUG("! Reached Max Logfiles. Appending to latest file.");
#endif
			break;
		}
	}
	strcpy(csvFileName, candidateFilename);


	// Generate CSV file header

	File csvFile = SD.open(csvFileName, FILE_WRITE);
	if (csvFile)
	{
		String csvHeader = "TIME, VBATT"; // These are always recorded
		for (uint8_t i = 0; i < SLOTS.size(); i++)
		{
			switch (SLOTS[i].getType())
			{
			case EXP_ADC_RAW:
				csvHeader += ", RAW";
				break;
			case EXP_CURRENT_LOOP:
			case EXP_CURRENT_SENSE:
				csvHeader += ", mA";
				break;
			case EXP_THERMOCOUPLE:
				csvHeader += ", C";
				break;
			default:
				break;
			}
			csvFile.close();
		}
		csvFile.println(csvHeader);
		csvFile.close();
		LOG_DEBUG(F("- CSV File Ready: ") + String(csvFileName));
		LOG_DEBUG(F("- CSV File Header: ") + csvHeader);
	}
	else
	{
		LOG_DEBUG(F("! CSV file Error: ") + String(csvFileName));
	}
}



void LOG_DEBUG(String message)
{
	Serial.println(message);
#ifdef DEBUG
	File debugFile = SD.open(DEBUG_LOG_NAME, FILE_WRITE);
	if (debugFile)
	{
		debugFile.println(message);
		debugFile.close();
	}
#endif
}


I have tried checking the status of debugFile and it indeed shows it as being closed right before I try to open the csvFile.
 
Status
Not open for further replies.
Back
Top