Arduino Mega 2560 Storing Invalid (negative) Values on SD Shield

Hi all,

I’m having a very strange issue working with the Arduino Mega 2560, the Wireless SD Shield, and a handful of XBEE ZigBee Pro 2 RF Modules. Here’s the low down:

I want the Arduino to save the data package from the XBEEs (21 hex values, converted to HEX by the Arduino naturally) onto the SD card. I’m reading a continuous voltage onto two router transmitters XBEES through a “mothership” XBEE resident on the wireless SD shield. The data from the XB is sent through its own pins 2+3 (DOut and DIn) to Serial1 on the Mega, due to some other issues (which were resolved by sending the data to Serial1).

The strange part happens anywhere from a few seconds into the below code, to nearly 10 seconds into the code, where the data suddenly shifts from correct data format to simply (-1) values. I’ve attached a few text files of what I’m getting out of the SD card.

#include <SD.h>    // SD library
File build;    //saving directive

int CSpin = 4;
int hardware = 10;
int anPin;
int serialCheck;
int dataArray[] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int i;
int data;


void setup() {
  Serial1.begin(9600);
  pinMode(hardware,OUTPUT);
  
  SD.begin(CSpin);
  build = SD.open("01245.txt", FILE_WRITE);
  
  if(build) {
    build.println("\n\nXB Start Simple V3");
    build.println("Time(ms)\tSerial Status\tXB Data");
    build.close();
  }
}
void loop() {
  serialCheck = 0;  
  data = 0;
  
  if(Serial1.available() > 0) {  // is there serial connection
    serialCheck = 1;
    while(!data) {   // no new data in the form we want (starting with 0x7E)
      if(Serial1.read() == 0x7E) {  // delimeter - start of good data
        for(i=0; i < 21; ++i) {
          dataArray[i] = Serial1.read();
        }
        data = 1;  // finally got new data
      }
    }
  }

  build = SD.open("01245.txt", FILE_WRITE);  
  if(build) {
    build.print(millis());
    build.print("\t");
    if(serialCheck) {
      build.print("Good connection");
    }
    else {
      build.print("Bad Connection");
    }
    build.print("\t");    
    for(i = 0; i < 21; ++i) {
      build.print(dataArray[i]);
      build.print("\t");
    }
    build.println("");
    build.close();
  }
  delay(500);
}

Here is something I have noticed: the transmission seems nearly instant (<1 second) when the data is flowing correctly. When the data starts to be incorrect/corrupted, the transmission time goes to 3-4 seconds between packages. Could this have something to do with the corruption?

I would really appreciate any comments or suggestions to this problem!

XB_SSBU_V3.txt (3.07 KB)

01244.TXT (3.97 KB)

01243.TXT (11.6 KB)

You can’t just blindly read Serial1 like this.

       for(i=0; i < 21; ++i) {
          dataArray[i] = Serial1.read();
        }

You get good data in the beginning since data is in the Serial1 buffer.

It takes time, about 1 ms, to transfer each byte. When no data is available Serial1.read() returns -1.

You wait for the 0X7E byte and then quickly get 21 instances of -1, no data available. You then throw away the actual data waiting for another 0X7E.

Do something Like this:

       for(i=0; i < 21; ++i) {
          while (!Serial1.available()) {
             // do timeout check here
          }
          dataArray[i] = Serial1.read();
        }