0

In my recent project, one part is writing the incoming serial data into SD card. I'm successfully able to write data to SD card. But the problem is my serial data is continuous and because of that same data write multiple times.

My code is below:

#include <SPI.h>
#include <SD.h>

File myFile;

String response = "";
bool begin = false;
bool finished = false;

void setup()
{
  Serial.begin(9600);

  if (!SD.begin(53)) {
  } 
}

void loop()
{
   while((finished == false) || Serial.available())
   {
     if(Serial.available())
     {
      const char in = Serial.read();

      if(in == '{')
      {
        response = "";  // Blank the string
        begin = true;
        finished = false;
      } 

      if(begin)
      {
        response += (in); // Only write if within the {}
      }

      if(in == '}')
      { 
        begin = false;   // Prevent any chars between } and { leaking through.
        finished = true;
      }
    }
  }

  myFile = SD.open("maptps.txt", FILE_WRITE);  

  if(myFile)
  {
    myFile.println(response);
    myFile.close();
  }
}

And Output is like:

{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.85","MAP_ONE":"0.00","LOAD_ONE":"12"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"0.95","MAP_ONE":"0.40","LOAD_ONE":"24"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.05","MAP_ONE":"0.80","LOAD_ONE":"34"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}
{"TPS_ONE":"1.15","MAP_ONE":"1.20","LOAD_ONE":"46"}

Now as you can see data is repeated and I just want to write only once. Because I have to fetch this data also. So please suggest.

  • Do you only want to write it when a value changes? So do that. Remember what you last wrote, compare it with what you just read, and if they differ then write something new. Or, do you only want to write the latest value so you only have one entry in the file? In that case, either delete the file and recreate it, or seek() to the start of the file before writing. – Majenko Dec 15 '17 at 13:51
  • move all the myFile stuff into the if(in == '}') conditional – dandavis Dec 15 '17 at 15:17
  • the problem is that you are writing to SD at every iteration of loop() even when no serial data has arrived – jsotola Dec 15 '17 at 18:31
  • @dandavis Thank you... Now it is perfect... No repeated data... – Hasan Dec 16 '17 at 10:09
  • @Majenko I also thought about that but not able to perform that logic perfectly. – Hasan Dec 16 '17 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.