Go Down

Topic: Need help writing sensor data to SD card file (Read 131 times) previous topic - next topic

MadTinker1407

Jan 12, 2019, 06:33 pm Last Edit: Jan 12, 2019, 06:37 pm by MadTinker1407
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

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?

Code: [Select]
  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?

MadTinker1407

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?

Code: [Select]
  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. 

Code: [Select]
  //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]

arduarn

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.

MadTinker1407

#4
Jan 13, 2019, 12:53 pm Last Edit: Jan 13, 2019, 12:54 pm by MadTinker1407
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.

Code: [Select]
 /* 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.   :smiley-confuse:
[/code]
[/code]

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.

MadTinker1407

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.

Go Up