First, let me point out a problem in your code, completely unrelated to
your question:
AcX = Wire.read()<<8|Wire.read();
The C++ standard does not specify in which order the two reads will be
performed. This may work well with the particular version of the
particular compiler you are using, but it can break the day you (or an
update to the Arduino IDE) changes the compiler version or a compiler
flag. You should instead do a single read per statement, e.g.:
AcX = Wire.read() << 8;
AcX |= Wire.read();
Next, I would like to say that the simplest Arduino way of doing what
you are trying to do is to use neither a String
nor a character array.
Instead, open the file first, then format and send the data on the fly
using print()
or println()
:
dataFile.print(Time0); dataFile.print(",");
dataFile.print(AcX); dataFile.print(",");
[...]
This way you never need to store the whole string in memory. It is then
a very memory-friendly solution.
Now, I realize this seems inconvenient if you want to print the data to
both the SD file and the serial port. In such a case you could put the
printing code inside a function that can print to any Print
object
(for instance the data file and Serial
), and you can then call the
function twice:
static void printImuData(Print &printer, int16_t Time0,
int16_t AcX, int16_t AcY, int16_t AcZ,
int16_t GyX, int16_t GyY, int16_t GyZ)
{
printer.print(Time0); printer.print(",");
printer.print(AcX); printer.print(",");
[...]
}
[...]
printImuData(dataFile, Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
if (Serial_plus_SD)
printImuData(Serial, Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
Alternatively, you could use a kind of tee to duplicate the data to
both streams. See for example this implementation.