Reading Serial Port and Writing to SD Card Problem

Sending MPU6050 data from an UNO to a MEGA and then writing to an SDCard on the MEGA.

For awhile my data seems to be reading and printing to the serial monitor just fine, but after is has been recording for awhile, it will sometimes have these lines where it looks like only part of the data is being printed.

I attached a picture of the serial monitor to show.

//ARDUINO MEGA 2560: RECEIVER


#include <SD.h> // Load the SD library
#include <SPI.h> // Load the SPI communication library 

File mySensorData;
int chipSelect = 53;

const byte numChars = 64;
char data[numChars];
boolean newData = false;


void setup() {
  Serial.begin(9600);
  Serial3.begin(9600); //TX3 and RX3
  pinMode(chipSelect, OUTPUT);
  SD.begin(chipSelect);
}

void loop() {
  receiveData();
  writeToSD();
}

void receiveData(){

  static byte ndx = 0;
  char endMarker = '\n';
  char reading;

  if(Serial3.available() > 0){
    while (Serial3.available() > 0 && newData == false){

      reading = Serial3.read();
      
      if (reading != endMarker){
        data[ndx] = reading;
        ndx++;
        if(ndx >= numChars){
          ndx = numChars - 1;
        }
      }else{
        data[ndx] = '\0'; //terminate the string
        ndx = 0;
        newData = true;
      }
    }
  }
}

void writeToSD(){
  
  if(newData = true){
    mySensorData = SD.open("Test.txt", FILE_WRITE);
    if(mySensorData){
    mySensorData.print(data);
    mySensorData.print("\n");
    mySensorData.close();
    Serial.print(data);
    Serial.print("\n");
    newData = false;
  
  }else{
    Serial.println("FILE FAILED TO OPEN");
  }
  

}

ahayes24:

void writeToSD(){

if(newData = true){

}

You have an assignment there, not a test for equality

You are assigning the value of true (1) to newData, try this:

if(newData == true){

OR

if(newData){

Thanks for replying! Yeah, I noticed that problem earlier as well. Unfortunately, fixing that doesn't solve that problem either.

//ARDUINO MEGA 2560: RECEIVER


#include <SD.h> // Load the SD library
#include <SPI.h> // Load the SPI communication library 

File mySensorData;
int chipSelect = 53;

const byte numChars = 64;
char data[numChars];
boolean newData = false;


void setup() {
  Serial.begin(9600);
  Serial3.begin(9600); //TX3 and RX3
  pinMode(chipSelect, OUTPUT);
  SD.begin(chipSelect);
}

void loop() {
  receiveData();
  writeToSD();
}

void receiveData(){

  static byte ndx = 0;
  char endMarker = '\n';
  char reading;

  if(Serial3.available() > 0){
    while (Serial3.available() > 0 && newData == false){

      reading = Serial3.read();
      
      if (reading != endMarker){
        data[ndx] = reading;
        ndx++;
        if(ndx >= numChars){
          ndx = numChars - 1;
        }
      }else{
        data[ndx] = '\0'; //terminate the string
        ndx = 0;
        newData = true;
      }
    }
  }
}

void writeToSD(){
  
  if(newData == true){
    mySensorData = SD.open("Test.txt", FILE_WRITE);
    if(mySensorData){
    mySensorData.print(data);
    mySensorData.print("\n");
    mySensorData.close();
    Serial.print(data);
    Serial.print("\n");
    newData = false;
  
  }else{
    Serial.println("FILE FAILED TO OPEN");
  }
  
  }
}

The SD card does take some time to write the data to the card and 9600 baud is relatively slow so it takes time to print out things to the serial monitor.

Have you tried opening Serial at 115200?

blh64:
The SD card does take some time to write the data to the card and 9600 baud is relatively slow so it takes time to print out things to the serial monitor.

Have you tried opening Serial at 115200?

Thanks for replying! Unfortunately, changing the baud to 115200 didn't fix the problem.

It's not a huge problem because it only happens every few hundred data points, which isn't a big deal, but I'm afraid that this small error is something that could be the underlying cause of a much bigger error down the road. It also seems to happen more often when my MPU data is changing and not sitting still.

blh64:
The SD card does take some time to write the data to the card and 9600 baud is relatively slow so it takes time to print out things to the serial monitor.

I think this is the problem for sure. One way I got this to work was by only opening the file one time in the setup and then closing it at a timer event later on. This works for my project because we need to only be taking data for about 5 minutes, but is there a reason why this might go wrong or cause problems?

//ARDUINO MEGA 2560: RECEIVER


#include <SD.h> // Load the SD library
#include <SPI.h> // Load the SPI communication library
 

File mySensorData;
int chipSelect = 53;

const byte numChars = 64;
char data[numChars];
boolean newData = false;
char reading = '0';

unsigned long timer = 0;


void setup() {
  timer = 0;
  Serial.begin(9600);
  Serial3.begin(9600); //TX3 and RX3
  pinMode(chipSelect, OUTPUT);
  SD.begin(chipSelect);
  mySensorData = SD.open("Test.txt", FILE_WRITE);
  mySensorData.println("START!");
  Serial.println("START!");
}

void loop() {
  timer = millis();
  receiveData();

  if(timer >= 60000){
    mySensorData.println("FINISHED!");
    mySensorData.close();
    Serial.println("FINISHED!");
    while(1);
  }

}

void receiveData() {
  
  if (Serial3.available() > 0)
  {
    if(mySensorData)
    {
    
    while((Serial3.available() > 0) || timer >= 60000 )
    {
      reading = Serial3.read();
      mySensorData.print(reading);
      Serial.print(reading);
    }
  
    }else{
      Serial.print("File Failed to Open");
    }
  }
}