Need help writing sensor data to SD card file

I am a newbie and I started this project last month. I have worked with many sites example sketches and have compiled this sketch. The part that I cannot figure out now is how to take what I am seeing in my serial monitor for output from my accelerometer and write it to an SD card file.

I can open a file and write to it on the SD card (attached .txt file). I tried to create a string and write it to the file but I scrapped that idea as I could not figure it out. How do I get the accelerometer values to write to the file like I see them in the serial monitor?

Any help much appreciated

Project Details:
Arduino Uno WiFi Rev2
Grove Base Shield v2
Seeed SD Card Shield
Grove 6 Axis Accelerometer & Gyroscope

Connects_to_Internet_and_has_data_1_12_19c.ino (7.71 KB)

DATALOG.TXT (384 Bytes)

You seem to have enough in your code to suggest that you should be able to figure out how to write the data out. Are you getting confused because you have tried to write to the file after closing it?

  if (dataFile) {
    dataFile.println("testing 1,2,3.");
    dataFile.close();    //   <<<<<<<<<<This needs to happen after writing your data out to the file first.
    // print to the serial port too:
    Serial.println("done.");
  }

Or do I not understand your problem?

arduarn:
You seem to have enough in your code to suggest that you should be able to figure out how to write the data out. Are you getting confused because you have tried to write to the file after closing it?

  if (dataFile) {

dataFile.println("testing 1,2,3.");
    dataFile.close();    //  <<<<<<<<<<This needs to happen after writing your data out to the file first.
    // print to the serial port too:
    Serial.println("done.");
  }




Or do I not understand your problem?

Hi Arduarn,
Thanks for the response.

With the above piece of code you have referenced I was trying to prove to myself that I could write something to the card.

The Accelerometer code below is what I have been trying to write to the SD card. I can see it perfectly in the serial monitor (attachment) but I cannot get it to write to the datalog file on the sd card. All I see on the card it the test 1,2,3.

  //Accelerometer
  long x, y, z ;                                  // Prints data in series coma delimited on one line horizontally
  //Serial.print("\nAccelerometer:\n");           // Prints Accelerometer before each set of X,Y,Z and a blank line behind
  Serial.print(",aX = ");                         // actual printed line
  Serial.print(myIMU.readFloatAccelX(), 4);       // causes the driver to go get data from the IMU. The data is also passed to the print function.
  Serial.print(",aY = "); //actual printed line
  Serial.print(myIMU.readFloatAccelY(), 4);       // causes the driver to go get data from the IMU. The data is also passed to the print function.
  Serial.print(",aZ = "); //actual printed line
  Serial.println(myIMU.readFloatAccelZ(), 4);     // causes the driver to go get data from the IMU. The data is also passed to the print function.
[code]

Serial Monitor.jpg

MadTinker1407:
The Accelerometer code below is what I have been trying to write to the SD card. I can see it perfectly in the serial monitor (attachment) but I cannot get it to write to the datalog file on the sd card. All I see on the card it the test 1,2,3.

Ok, you see the output on the serial because you write it to Serial.
Try writing it to dataFile like you did the "test 1,2,3". But don't close the file first.

arduarn:
Ok, you see the output on the serial because you write it to Serial.
Try writing it to dataFile like you did the "test 1,2,3". But don't close the file first.

I changed it to write to dataFile. I took out the close line also. It will create the file named datalog.txt on the SD card but there is no data in it.

 /* if the file is available, write to it */
  if (dataFile) {
    dataFile.print(",aX = ");
    dataFile.print(myIMU.readFloatAccelX(), 4);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }[code]

I also put in code to write another file named IPLog.txt.  It will create the file and write all the information into it. 

[code]  dataFile = SD.open("IPlog.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.print("IP Address: ");
    dataFile.println(ip);
    dataFile.println("Truck#7");
    dataFile.print("Compile Date:");
    dataFile.println(__DATE__);
    dataFile.print(", ");
    dataFile.println(__TIME__);
    dataFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }[code]

I can still see the data on the serial monitor and it all looks great but I cannot get it to print.   :confused:

[/code]
[/code]

Serial Monitor2.jpg

Connects_to_Internet_and_has_data_1_13_19.ino (8.12 KB)

IPLOG.TXT (70 Bytes)

Ok, so now you are not closing the dataFile in printData() at all. That could be an issue since the SD library probably caches small writes until is feels it has enough to write out to the card; if you never close or sync/flush the file then the small writes may never be written out. In your case, you also keep reopening the already open file with FILE_WRITE, which may also cause issues.

So, if you are going to open the file at the beginning of the function, then you need to close it again at the end of the function.

arduarn:
Ok, so now you are not closing the dataFile in printData() at all. That could be an issue since the SD library probably caches small writes until is feels it has enough to write out to the card; if you never close or sync/flush the file then the small writes may never be written out. In your case, you also keep reopening the already open file with FILE_WRITE, which may also cause issues.

So, if you are going to open the file at the beginning of the function, then you need to close it again at the end of the function.

Thank you Arduarn! It works great!!

Complete code attached.

Connects_to_Internet_and_has_data_1_13_19a.ino (8.34 KB)