Reading & writing to different SD card files in succession.

I have decades of experience with electronics and microcontrollers but this is my first post in these forums. Thanks in advance for your assistance - I hope to be able to return the favor at some point.

I have a project using a 'Genuino' Mega 2560, Adafruit datalogging shield, and the recommended Adafruit library.

I want to repeat the following tasks:

  1. Open an SD file, write some raw sensor data to it, then close that file.

  2. Open an SD file with a different name, write some averaged sensor data to it, then close that file.

The following code to accomplish task #1 works fine:

//SDCardSingleFile

//SD card library and defines.
#include <SD.h>
#include <SPI.h>
const int chipSelect = 10;

String outFileName;
String outLine;

// SETUP
void setup() 
{
  // Initialize serial port.
  Serial.begin(9600); 
  Serial.println("Setup");

  //Set up DS memory card.
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

}

void loop() 
{
  outFileName="TESTFILE";

  Serial.println("Open file: "+outFileName+".RAW");

  File dataFile = SD.open(outFileName+".RAW", FILE_WRITE);

  outLine="Here is some raw data.";

  Serial.println("Writing raw data: "+outLine);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(outLine);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error writing to SD");
  }
    
  Serial.println("Close raw file.");
  dataFile.close();


  delay(5000);
}

The following code to attempt #1 and then #2 gives me a compile error "redeclaration of 'SDLib::File dataFile" at the point I try to open the second file.

//SDCardMultiFile

//SD card library and defines.
#include <SD.h>
#include <SPI.h>
const int chipSelect = 10;

String outFileName;
String outLine;

// SETUP
void setup() 
{
  // Initialize serial port.
  Serial.begin(9600); 
  Serial.println("Setup");

  //Set up DS memory card.
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

}

void loop() 
{
  outFileName="TESTFILE";

  Serial.println("Open file: "+outFileName+".RAW");

  File dataFile = SD.open(outFileName+".RAW", FILE_WRITE);

  outLine="Here is some raw data.";

  Serial.println("Writing raw data: "+outLine);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(outLine);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error writing to SD");
  }
    
  Serial.println("Close raw file.");
  dataFile.close();

  Serial.println("Open file: "+outFileName+".TXT");

  File dataFile = SD.open(outFileName+".TXT", FILE_WRITE);

  outLine="Here is some processed data.";

  Serial.println("Writing processed data: "+outLine);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(outLine);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error writing to SD");
  }
    
  Serial.println("Close txt file.");
  dataFile.close();

  delay(5000);
}

If I change the second declaration to open dataFile2, that works, but concerns me because the last thing I need to do is:

  1. Accept a filename over the serial port, then open that file if it exists and transmit the contents over the serial port. I want to be able to make this happen an arbitrary number of times, over and over.

Can't I use the same file 'handle' (is that the term?) to open different files for different purposes, some read, some write, so long as I close one file before opening another? If I have to use different file 'handles' (dataFile, dataFile2, and so forth) what happens when I get to task 3 and want to send a file over the serial port, then open a different one?

I haven't seen this exact issue addressed anywhere in this forum or on the web. Hopefully I didn't miss something obvious, but if that happened please be kind.

Thanks,
Tom

"Computers do what you tell them to do, not what you want them to do." - Norman E. Gibbs, 1974.

declare "File dataFile" at the top of your program in the variables section. Then just use "dataFile": "dataFile = SD.open(...)"

It works!

Muchas gracias!

Tom