Ultimate GPS Shield and Logging?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dpaulc
 
Posts: 34
Joined: Thu May 09, 2013 11:40 am

Ultimate GPS Shield and Logging?

Post by dpaulc »

I'm new to Arduino and its programming. I have the Ultimate GPS shield working with the sample "shield_slog" sketch and am now making changes to it. I coded the change to convert latitude from ddmm.mmm to dd.dddd. Everything worked as expected. I then added the code to do the same thing to longitude but I get a message on the serial console that the file couldn't be created on the SD card.

Code: Select all

  Serial.println("Log");
    lat = (GPS.latitude);
    lon = (GPS.longitude);
    dtostrf(lat,10,5,charbuffer);    //float to string conversion (string in this case is char array)
    dtostrf(lon,11,5,charbuffer2);
    Serial.println(GPS.latitude, 8);
    Serial.println(GPS.longitude, 8);
    Serial.println(lat,10);
    Serial.println(lon,10);
    Serial.println(charbuffer);
    Serial.println(charbuffer2);
    latIntString = charbuffer;      //convert char array to actual string
    lonIntString = charbuffer2;
    latIntString = latIntString.substring(0,2);    //extract first 2 characters of latitude from string
    lonIntString = lonIntString.substring(0,3);
    Serial.println(latIntString);
    Serial.println(lonIntString);
    latDecString = charbuffer;      //put contents of charbuffer in latDedString
    lonDecString = charbuffer2;
    latDecString = latDecString.substring(2);  //extract everything but first 2 digits of latitude
    lonDecString = lonDecString.substring(3);  //extract everything but first 3 digits of longitude
    Serial.println(latDecString);
    Serial.println(lonDecString);
    latDecString.toCharArray(latCharBuffer,8);  //convert string to char array
    lonDecString.toCharArray(lonCharBuffer,9);
/*    latCorr = atof(latCharBuffer);              //convert char array to float
    lonCorr = atof(lonCharBuffer);
    latCorr = latIntString.toInt()+(latCorr/60);//perform conversion of dddd.mmmmm to dd.dddd
    lonCorr = lonIntString.toInt()+(lonCorr/60);
    Serial.println(latCorr,7);
    Serial.print("-");
    Serial.println(lonCorr,7); */
I've tracked it to this:

1. latDecString.toCharArray(latCharBuffer,8); //convert string to char array
2. lonDecString.toCharArray(lonCharBuffer,9);

If I comment 2 out there is no problem. If I move 2 before line 1 and comment out line 1 (now in position 2) there is no problem. Is there a problem with how I've used toCharArray?

The code for opening the SD file for writing the file is in the setup loop:

Code: Select all

  char filename[15];
  strcpy(filename, "GPSLOG00.TXT");
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = '0' + i/10;
    filename[7] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }
  }

  logfile = SD.open(filename, FILE_WRITE);
  if( ! logfile ) {
    Serial.print("Couldnt create "); Serial.println(filename);
    error(3);
  }
  Serial.print("Writing to "); Serial.println(filename);
How can the sketch not create the file for a problem that occurs later in the void loop() section?

Any ideas?

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Ultimate GPS Shield and Logging?

Post by adafruit_support_bill »

It is possible that with all the string manipulation, you have run out of SRAM and some buffer has been corrupted. Try commenting out some other sections of code and see if the problem moves or goes away.

User avatar
dpaulc
 
Posts: 34
Joined: Thu May 09, 2013 11:40 am

Re: Ultimate GPS Shield and Logging?

Post by dpaulc »

That's exactly what was happening. I got rid of all the Strings and found this post in the forum:

http://forum.arduino.cc/index.php?topic=122040.0

By using strncpy I was able to extract the values needed. I couldn't find the strncpy in any of the reference material for Arduino.

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Ultimate GPS Shield and Logging?

Post by adafruit_support_bill »

strncpy() is part of the standard C library. The Arduino team likes to focus on the "Arduino Language" as a friendly easy-to-learn language and not scare people with its C and C++ roots. But for advanced users, there is a lot more power available under the hood if you know where to look. :D
http://www.cplusplus.com/

User avatar
dpaulc
 
Posts: 34
Joined: Thu May 09, 2013 11:40 am

Re: Ultimate GPS Shield and Logging?

Post by dpaulc »

Thanks for the link!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”