Serial monitor and SD card show different data

Hardware:

Mega 2560 with MegaShield
Windows 7 ultimate (64 bit)

What I am trying to accomplish:

I am using the Mega for a general data logger. So far I am getting the analog sensors (not important, one is a infrared position sensor, the other is an ambient light sensor, last is anolog(2) grounded). I will be adding many more sensors to this setup once this proof of concept is completed.

Structure:

I am trying to use the millis() function to timestamp my data in the SD card. At the moment, I am happy with reading the sensors every 250mS and writing the data to the SD card. I tried to concatenate the millis() data to the left of the data, but could only get it to work by doing it on the right. Not sure on that but I am sure it is because of something I am doing wrong.

Main problems:

My issue is when I am running this while watching the serial monitor, the data will randomly stop. Once the data stops on serial, it stops on the SD card also. The data is different between them also. Here is an instance:

{This is from the Serial monitor}

Initializing SD card...card initialized.
62,881,0,414414
59,927,0,689689
60,886,27,951951
60,885,0,12141214
64,890,0,16861686
59,882,3,19491949
59,895,1,22122212
59,885,0,24752475
58,878,0,27382738
63,877,0,30013001
60,886,0,32643264
59,888,0,35273527
59,886,3,37893789
59,895,0,40524052
59,912,0,43164316
48,875,0,45784578
87,874,0,48414841
61,890,0,51045104
60,887,0,53675367
59,895,15,56305630
59,887,1,58935893
60,902,0,61566156
60,872,0,64196419
67,875,0,66826682
62,881,0,69446944
59,885,0,72077207
60,888,0,74717471
59,883,25,77347734

{This is from the SD card}

62,881,0,414
59,927,0,689
60,886,27,951
60,885,0,1214
64,890,0,1686
59,882,3,1949
59,895,1,2212
59,885,0,2475
58,878,0,2738
63,877,0,3001
60,886,0,3264
59,888,0,3527
59,886,3,3789
59,895,0,4052
59,912,0,4316
48,875,0,4578
87,874,0,4841
61,890,0,5104
60,887,0,5367
59,895,15,5630
59,887,1,5893
60,902,0,6156
60,872,0,6419
67,875,0,6682
62,881,0,6944
59,885,0,7207
60,888,0,7471
59,883,25,7734

As you can tell the timestamp data (on the far right) is correctly delimited with the coma but the data still freezes. Any help with this is appreciated!

Here is the code (Very green at this, please have mercy)

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 51
 ** MISO - pin 50
 ** CLK - pin 52
 ** CS - pin 53
 
 created  24 Nov 2010
 updated 2 Dec 2010
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SD.h>
long currenttime = 0;        // will store last time updated
//long interval = 250;           // interval of data sampling output

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 4) {
      dataString += ","; 
    }
  }


  unsigned long currenttime = millis();

  delay(250);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.

  //if(currenttime - previoustime > interval) {
  // save the last time you blinked the LED
  //  previoustime = currenttime;  

  //Serial.println(currenttime);


  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString += currenttime);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString += currenttime);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
  //}
}

My advice is to stop using String, and concatenate into a fixed buffer. Doing string concatenation using the String class is very likely to gobble up available RAM, as you fragment it.

You have currenttime defined twice.

As Nick says, dump the String class. You probably don't even need to do much with a fixed buffer. Just "print" each variable to complete the line, then println().

    dataFile.println(dataString += currenttime);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString += currenttime);

You are adding the time to the string twice. Pay attention to what you are copying and pasting.

I really appreciate everyone's help with this. I have not touched C in a very long time (just basic stuff) and the last coding I did was assembly and that was forever ago it seems. So as one might see from my code it was all hacked together because I was trying many things at once in desperation. I decided to start back at the beginning and work on serial printing the data in the correct format. Here is my very simple and inefficient code:

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

void loop()
{

  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    Serial.print(sensor);
    if (analogPin < 2){
      Serial.print(",");
    }
    else{
      Serial.println(""); 
      delay(250);
    } 
  }
}

I tried a bit to work on char arrays but I had issues with int ==> char data types. So what I think needs to happen based off of the suggestions:

  • Create a buffer
  • read analog data into said buffer
  • Separate data with a coma
  • print out entire array and all is good with the world

If this is remotely correct, how would one go about concatenating int data into an array that is seperated by commas?

I apologize for my painful noobish questions and appreciate everyones time.

DSMR:
If this is remotely correct, how would one go about concatenating int data into an array that is seperated by commas?

My suggestion was, don't.

Just print the values one at a time, followed by a newline. There's no point in creating a buffer.

Thank you for your reply! The reason I was thinking of using a buffer is that this data will need to be dumped into a SD card. Also, the number of analog pins that will be used for this project will change with time. I figured it would be nice to have an array that can change in size based off of how many inputs are put in.

The reason I would like the data to be in the X,Y,Z format is so that it is easily used in excel.

If you really think that you need to put the data in a buffer, create an array to hold the values of the sensors.

int vals[3];

Then, store the data in the array:

  for (int analogPin = 0; analogPin < 3; analogPin++)
  {
    vals[i] = analogRead(analogPin);
  }

Then, use sprintf to populate the buffer:

char buff[32];
sprintf(buff, "%i,%i,%i", vals[0], vals[1], vals[2]);

Then, you can print buff to the serial port or the SD card.

Thank you so much PaulS!!! The solution you have offered worked perfectly with a little tweeking. So now that I have the SD card stuff down I am hitting a brick wall with the GPS part of my project. I used the GPS example located here as my shell : Arduino Playground - GPS

So what I would like to do in my code is parse the data in such a way that I can throw it into the sprintf function mentioned previously. Here is a snippet of the code I am having the issue with (I hobbled this together with my meager skills so don't be too shocked):

     Serial.println("---------------");
   
        char GPS[12] ="";       
        for (int i=0;i<11;i++){
          for (int j=indices[i];j<(indices[i+1]-1);j++){
            GPS[i] =linea[j+1];

            Serial.print(GPS[i]);
           
          }

          Serial.print(" ");
        }

        Serial.println("");
        Serial.println("---------------");

This codes gives me the following o/p on SM:

---------------
014539 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014540 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014541 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014542 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014543 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014544 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014545 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014546 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------
---------------
014547 A 3607.0188 N 07946.2056 W 000.0 304.9 230212 008.1  
---------------

Now I am no codesmith by any means but I am just hitting my head at this point as how I can separate each part into an array. So what I would like to happen is GPS[0] = 014539, GPS[1] = A, GPS[2] = 3607.0188, ect.

What I am seeing that the data is being pushed into this loop. So it is not taking the entire chunk before deciding, "ok I have all the data in for GPS[0]". What I get is more like, "here is a stream of data and you will remember the last value. Does this makes sense?

If I use the string command, I can get it to work properly in just GPS testing but when I integrate it with the SD card code it seems as though I have hit my limit for RAM like before.

Again I appreciate all the help that is given to me and will post full details on the project when finished so that it might help some other noob like myself.

I think that I may have it. This might not be the most efficient, but so far it looks as though it will work. This is my new code which utilizes a 2 dimensional array:

        Serial.println("---------------");
   
        char GPS[12][20] = {""};       //This is where I set up my other scalar (I just made sure it would be big enough to fit in all the characters)
        
        for (int i=0;i<11;i++){
          int x = 0;
          for (int j=indices[i];j<(indices[i+1]-1);j++){
            
            GPS[i][x] = linea[j+1];
            
           x++;
          }
          

          
        }
        Serial.print(GPS[0]);
        Serial.println("");
        Serial.print(GPS[1]);
        Serial.println("");
        Serial.print(GPS[2]);
        Serial.println("");
        Serial.print(GPS[3]);
        Serial.println("");
        Serial.print(GPS[4]);
 
       //So on and so forth ...

        Serial.println("");
        Serial.println("---------------");
      }

Now I am going to try to use these new array items in Sprintf. I see that there is a data type called "s" for string. I wonder if I should use that?

Ok I am getting really close to this but I am still messing something up. It has to be something minor that I am not seeing. Here is my integrated code (I need to go back and comment everything)

/*
  
 MOSI - pin 51 (11)
 MISO - pin 50 (12)
 CLK - pin 52  (13)
 CS - pin 53   (10)
 
 Listen for the $GPRMC string and extract the GPS location data from this.
 Display the result in the Arduino's serial monitor.
 	 
 */

//-------------------Libraries----------------------//
#include <SD.h>
#include <string.h>
#include <ctype.h>

//-------------------Constants and Declarations---------------------//

char GPS[12][20] = {""}; 
long previousMillis = 0;
long dataRate = 250;            // Sets how fast(ms) the unit will refresh logged values
const int chipSelect = 53;     // CS for SD card
const int xPin = 22;           // X output of the accelerometer
const int yPin = 23;           // Y output of the accelerometer
int vals[3];                   // Initialize 3 character array 
long count = 0;                // Initialize count
int ledPin = 13;                  // LED test pin
int rxPin = 19;                    // RX PIN
int byteGPS=-1;                   //Initialize byteGPS 
char linea[300] = "";             //Create 300 character array
char comandoGPR[7] = "$GPRMC";    //Initialize character
int cont=0;                       
int ok=0;
int conta=0;
int indices[13];
int pulseX, pulseY;
int accelerationX; 
int accelerationY;
char buff[300] = ""; 


//----------------------------------------------------Setup--------------------------------------//
void setup()
{
 
  Serial1.begin(4800);
  Serial.begin(4800);
  Serial.print("Initializing SD card...");  
  pinMode(53, OUTPUT);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(ledPin, OUTPUT);       // Initialize LED pin
  pinMode(rxPin, INPUT);
  pulseX = pulseIn(xPin,HIGH);  
  pulseY = pulseIn(yPin,HIGH);
  accelerationX = ((pulseX / 10) - 500) * 8;
  accelerationY = ((pulseY / 10) - 500) * 8;

  for (int i=0;i<300;i++){       // Initialize a buffer for received gps data
    linea[i]=' ';
  }  

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }

  Serial.println("card initialized.");
}

//------------------------Loop--------------------------------------------------//
void loop()
{

  digitalWrite(ledPin, HIGH);
  byteGPS=Serial1.read();         // Read a byte of the serial port
  if (byteGPS == -1) {           // See if the port is empty yet
    delay(100);
  } 
  else {

    linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
    conta++; 

    if (byteGPS== 13){            // If the received byte is = to 13, end of transmission
      digitalWrite(ledPin, LOW);   

      cont=0;
      ok=0;
      for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
        if (linea[i]==comandoGPR[i-1]){
          ok++;

        }
      }
      if(ok==6){               // If yes, continue and process the data
        for (int i=0;i<300;i++){
          if (linea[i]==','){    // check for the position of the  "," separator
            indices[cont]=i;
            cont++;
          }
          if (linea[i]=='*'){    // ... and the "*"
            indices[12]=i;
            cont++;
          }
        }   

        char GPS[12][20];       

        for (int i=0;i<11;i++){
          int x = 0;
          for (int j=indices[i];j<(indices[i+1]-1);j++){

            GPS[i][x] = linea[j+1];

            x++;
          }
        }
      }

      //-------------------------------------Analog Portion of Code------------------------------------------//
      
      delay(dataRate);
      //unsigned long currentMillis = millis();
      //if(currentMillis - previousMillis > dataRate) 
      //previousMillis = currentMillis;

      // read three sensors and append to the string:
      for (int analogPin = 0; analogPin < 3; analogPin++) {

        vals[analogPin] = analogRead(analogPin);

      }
      count++;

      //---------------------SPRINTF portion (if you are adding new sensors to be logged, this is where you will format it for output to SDcard!--------------------//

      sprintf(buff, "%i,%i,%i,%i,%i,%i,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", count, vals[0], vals[1], vals[2], accelerationX, accelerationY, GPS[0],GPS[1],GPS[2],GPS[3],GPS[4],GPS[5],GPS[6],GPS[7],GPS[8],GPS[9],GPS[10]);

      File dataFile = SD.open("datalog.txt", FILE_WRITE);

      // if the file is available, write to it:

      if (dataFile) {
        dataFile.println(buff);
        dataFile.close();
        // print to the serial port too:
        Serial.println(buff);
      }  
      // if the file isn't open, pop up an error:
      else {
        Serial.println("error opening datalog.txt");
      } 
    }
  }
}

Here is the output that I am getting from it (In serial monitor):

1,0,0,64,921,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjhhhhhh
2,0,0,65,922,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
3,0,0,69,913,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
4,0,0,70,919,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
5,0,0,68,914,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
6,0,0,64,915,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
7,0,0,79,902,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
8,0,0,63,917,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
9,0,0,63,911,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
10,0,0,64,952,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Initializing SD card...card initialized.
1,0,23,70,902,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
2,0,5,70,905,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
3,0,0,64,916,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
4,0,0,68,917,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
5,0,0,64,918,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
6,0,0,68,911,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
7,0,0,75,900,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhjhhhhhhhhhhhhhhhhhhhhhhhhhhh
8,0,0,101,908,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
9,0,3,71,914,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
10,0,0,65,915,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Initializing SD card...card initialized.
1,0,0,64,920,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
2,0,0,64,954,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
3,0,0,64,915,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
4,0,0,69,943,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
5,0,0,65,909,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
6,0,1,69,911,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
7,0,0,64,917,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
8,0,0,64,922,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
9,0,0,68,923,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
10,0,0,69,915,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
11,0,0,69,939,-104,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

Any help is greatly appreciated as usual.

char GPS[12][20] = {""};

You have a 12 element array of strings. You have initialized the first one to an empty string. What is in the rest of them? Who knows?

If you don't supply any initializers, the compiler will properly initialize all the elements.

        char GPS[12][20];

Then, you have a local variable of the same name. Really not a good idea. Local variables are NOT initialized.

          for (int j=indices[i];j<(indices[i+1]-1);j++){

            GPS[i][x] = linea[j+1];

            x++;
          }

The linea array is not a string. It is an array of chars. So, GPS[ i ] is not a string, either.

      sprintf(buff, "%i,%i,%i,%i,%i,%i,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", count, vals[0], vals[1], vals[2], accelerationX, accelerationY, GPS[0],GPS[1],GPS[2],GPS[3],GPS[4],GPS[5],GPS[6],GPS[7],GPS[8],GPS[9],GPS[10]);

But here, you use the string format specifier to print something that is NOT a string.

After that loop above, you need to NULL terminate each collection of characters so that the collection of characters IS a string, and you can use the string format specifier.

GPS[j] = '\0';

You have a Mega 2560, which means plenty of flash RAM.

Use sprintf. Much less convoluted and much easier to understand.

PaulS:
Then, you have a local variable of the same name. Really not a good idea. Local variables are NOT initialized.

Which local variable is of the same name?

PaulS:
The linea array is not a string. It is an array of chars. So, GPS[ i ] is not a string, either.

      sprintf(buff, "%i,%i,%i,%i,%i,%i,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", count, vals[0], vals[1], vals[2], accelerationX, accelerationY, GPS[0],GPS[1],GPS[2],GPS[3],GPS[4],GPS[5],GPS[6],GPS[7],GPS[8],GPS[9],GPS[10]);

But here, you use the string format specifier to print something that is NOT a string.

After that loop above, you need to NULL terminate each collection of characters so that the collection of characters IS a string, and you can use the string format specifier.

GPS[j] = '\0';

What I do not understand is that if I run the GPS part of the code separately it works as it should:

  for (int i=0;i<11;i++){
          int x = 0;
          for (int j=indices[i];j<(indices[i+1]-1);j++){

            GPS[i][x] = linea[j+1];

            x++;
          } 
        }

        sprintf(buff, "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", GPS[0],GPS[1],GPS[2],GPS[3],GPS[4],GPS[5],GPS[6],GPS[7],GPS[8],GPS[9],GPS[10]);   

        Serial.println(buff);

This gives me this in the SM:

165026,A,3607.0207,N,07946.1900,W,000.0,304.3,230212,008.1,
165027,A,3607.0207,N,07946.1900,W,000.0,304.3,230212,008.1,
165028,A,3607.0207,N,07946.1900,W,000.0,304.3,230212,008.1,
165029,A,3607.0208,N,07946.1901,W,000.0,304.3,230212,008.1,
165030,A,3607.0208,N,07946.1901,W,000.0,304.3,230212,008.1,
165031,A,3607.0209,N,07946.1903,W,000.0,304.3,230212,008.1,
165032,A,3607.0209,N,07946.1902,W,000.0,304.3,230212,008.1,
165033,A,3607.0209,N,07946.1901,W,000.0,304.3,230212,008.1,
165034,A,3607.0209,N,07946.1902,W,000.0,304.3,230212,008.1,
165035,A,3607.0209,N,07946.1902,W,000.0,304.3,230212,008.1,
165036,A,3607.0209,N,07946.1902,W,000.0,304.3,230212,008.1,
165037,A,3607.0209,N,07946.1902,W,000.0,304.3,230212,008.1,
165038,A,3607.0211,N,07946.1904,W,000.0,304.3,230212,008.1,
165039,A,3607.0210,N,07946.1903,W,000.0,304.3,230212,008.1,
165040,A,3607.0210,N,07946.1902,W,000.0,304.3,230212,008.1,
165041,A,3607.0210,N,07946.1902,W,000.0,304.3,230212,008.1,
165042,A,3607.0210,N,07946.1902,W,000.0,304.3,230212,008.1,
165043,A,3607.0210,N,07946.1903,W,000.0,304.3,230212,008.1,
165044,A,3607.0212,N,07946.1905,W,000.9,304.3,230212,008.1,
165045,A,3607.0224,N,07946.1911,W,000.0,304.3,230212,008.1,
165046,A,3607.0224,N,07946.1911,W,000.0,304.3,230212,008.1,
165047,A,3607.0224,N,07946.1911,W,001.1,304.3,230212,008.1,
165048,A,3607.0223,N,07946.1911,W,001.1,304.3,230212,008.1,
165049,A,3607.0225,N,07946.1914,W,001.0,304.3,230212,008.1,

This works perfect here. When I try and integrate both parts together it fails miserably (or I fail for that matter). I feel like I am doing something very bad when I am combining both code sets (maybe there is a bracket in the wrong place or something).

You make a valid point on initialization and I will fix that. I do not understand about the null character addition you mention. I thought that the array will automatically size it for me: Reference http://arduino.cc/it/Reference/Array

"Finally you can both initialize and size your array, as in mySensVals. Note that when declaring an array of type char, one more element than your initialization is required, to hold the required null character."

Someone else told me to look into structures. I will be studying this too I guess.

Again, thanks for all the help and any time you put in helping me out on this.

jfhaugh:
You have a Mega 2560, which means plenty of flash RAM.

Use sprintf. Much less convoluted and much easier to understand.

Correct me if I am wrong here, but I thought that I was using sprintf?

Thanks

Which local variable is of the same name?

GPS. You have a global array and a local array with the same name.

What I do not understand is that if I run the GPS part of the code separately it works as it should:

Lucky you. Not something you should rely on. Properly NULL terminate the array, and be good, instead of lucky.

Correct me if I am wrong here, but I thought that I was using sprintf?

You are.

Ok so I have went through and made the corrections you asked. Still getting junk though.

Here is what I did to append the NULL marker:

        for (int i=0;i<11;i++){
          int x=0;
          for (int j=indices[i];j<(indices[i+1]-1);j++){

            GPS[i][x] = linea[j+1];           
            if (j == (indices[i+1]-2)){
              GPS[i][x]= '\0';
            }
            x++;
          }
        }

Here is my complete code:

/*
  
 MOSI - pin 51 (11)
 MISO - pin 50 (12)
 CLK - pin 52  (13)
 CS - pin 53   (10)
 
 Listen for the $GPRMC string and extract the GPS location data from this.
 Display the result in the Arduino's serial monitor.
 	 
 */

//-------------------Libraries----------------------//
#include <SD.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

//-------------------Constants and Declarations---------------------//

char GPS[12][20]; 
long previousMillis = 0;
int dataRate = 250;            // Sets how fast(ms) the unit will refresh logged values
const int chipSelect = 53;     // CS for SD card
const int xPin = 22;           // X output of the accelerometer
const int yPin = 23;           // Y output of the accelerometer
int vals[3];                   // Initialize 3 character array 
long count = 0;                // Initialize count
int ledPin = 13;                  // LED test pin
int rxPin = 19;                    // RX PIN
int byteGPS=-1;                   //Initialize byteGPS 
char linea[300] = "";             //Create 300 character array
char comandoGPR[7] = "$GPRMC";    //Initialize character
int cont=0;                       
int ok=0;
int conta=0;
int indices[13];
int pulseX, pulseY;
int accelerationX; 
int accelerationY;
char buff[180] = ""; 


//----------------------------------------------------Setup--------------------------------------//
void setup()
{

  Serial1.begin(4800);
  Serial.begin(4800);
  Serial.print("Initializing SD card...");  
  pinMode(53, OUTPUT);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(ledPin, OUTPUT);       // Initialize LED pin
  pinMode(rxPin, INPUT);
  pulseX = pulseIn(xPin,HIGH);  
  pulseY = pulseIn(yPin,HIGH);


  for (int i=0;i<300;i++){       // Initialize a buffer for received gps data
    linea[i]=' ';
  }  

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }

  Serial.println("card initialized.");
}

//------------------------Loop--------------------------------------------------//
void loop()
{

  digitalWrite(ledPin, HIGH);
  byteGPS=Serial1.read();         // Read a byte of the serial port
  if (byteGPS == -1) {           // See if the port is empty yet
    delay(100);
  } 
  else {

    linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer
    conta++; 

    if (byteGPS== 13){            // If the received byte is = to 13, end of transmission
      digitalWrite(ledPin, LOW);   

      cont=0;
      ok=0;
      for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
        if (linea[i]==comandoGPR[i-1]){
          ok++;

        }
      }
      if(ok==6){               // If yes, continue and process the data
        for (int i=0;i<300;i++){
          if (linea[i]==','){    // check for the position of the  "," separator
            indices[cont]=i;
            cont++;
          }
          if (linea[i]=='*'){    // ... and the "*"
            indices[12]=i;
            cont++;
          }
        }         

        for (int i=0;i<11;i++){
          int x=0;
          for (int j=indices[i];j<(indices[i+1]-1);j++){

            GPS[i][x] = linea[j+1];           
            if (j == (indices[i+1]-2)){
              GPS[i][x]= '\0';
            }
            x++;
          }
        }
      }
    }
  }
  //-------------------------------------Analog Portion of Code------------------------------------------//

  delay(dataRate);
  //unsigned long currentMillis = millis();
  //if(currentMillis - previousMillis > dataRate) 
  //previousMillis = currentMillis;

  accelerationX = ((pulseX / 10) - 500) * 8;
  accelerationY = ((pulseY / 10) - 500) * 8;
  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {

    vals[analogPin] = analogRead(analogPin);

  }
  count++;

  /*---------------------SPRINTF portion (if you are adding new sensors to be logged, this is where you will format it for output to SD card!--------------------*/

  sprintf(buff, "%i,%i,%i,%i,%i,%i,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", count, vals[0], vals[1], vals[2], accelerationX, accelerationY, GPS[0],GPS[1],GPS[2],GPS[3],GPS[4],GPS[5],GPS[6],GPS[7],GPS[8],GPS[9],GPS[10]);

  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:

  if (dataFile) {
    dataFile.println(buff);
    dataFile.close();
    // print to the serial port too:
    Serial.println(buff);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

Here is the SM output:

10,0,0,60,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
11,0,0,72,41,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
12,0,0,62,34,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
13,0,0,59,32,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
14,0,0,91,39,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
15,0,1,67,36,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
16,0,0,60,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
17,0,0,64,8,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
18,0,1,57,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
19,0,0,64,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
20,0,0,65,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
21,0,0,65,36,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
22,0,0,65,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh^hhhhhhhhh
23,0,0,101,39,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
24,0,0,64,35,-120,7hhhhhhhhhhhhhhhjhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
25,0,0,64,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
26,0,0,60,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
27,0,0,60,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
28,0,0,32,59,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
29,0,0,57,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
30,0,0,59,35,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
31,0,0,67,37,-120,7hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

Doesnt that look like I am reading from the wrong memory location??

Thanks

 byteGPS=Serial1.read();         // Read a byte of the serial port
  if (byteGPS == -1) {           // See if the port is empty yet
    delay(100);
  }

Please, please don't do this. You are building in a delay when none is needed. This should read something like:

if (Serial1.available ())
  {
  byteGPS = Serial1.read ();

  // process here

  }

After that I don't see where you are waiting for the carriage return to arrive before processing the data.

As far as I can see you "fall through" and process the data, even though it isn't all there yet.

Please read this:

Read up on buffering the data before processing it.

Here is what I did to append the NULL marker

That was wrong (and strange).

for (int i=0;i<11;i++)
{
  int x = 0;
  for (int j=indices[i];j<(indices[i+1]-1);j++)
  {
     GPS[i][x] = linea[j+1];
     x++;
  }
  GPS[i][x] = '\0';
}

Thank you for that! That is a much more elegant way to begin taking in the data. I was thinking about getting rid of that delay later on any how and your solution fits the bill.

It waits for it here:

 if (byteGPS== 13){

13 in ascii.

The GPS part always worked before (but I do like the change you suggested) so I am certain that was not the issue. I have ran the old GPS code all night with no issues. Thank you for your help and I will read that link you have suggested.