Go Down

Topic: Code stops when adding SD instructions (Read 548 times) previous topic - next topic

Soundstorm

Jan 06, 2013, 04:30 pm Last Edit: Jan 06, 2013, 04:36 pm by Soundstorm Reason: 1
Hey,

I'm working on some code to log GPS data. Still in testing phase, but everything seems to work. Now I'm trying to save the stuff to an SD card, instead of sending it through the serial port.

Code looks like this:
Code: [Select]
#include <SD.h>
void setup() {
 if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
   return;
 }

Code: [Select]
void data_parse() {
 Serial.readBytes(read_data,400);
 count_A = 0;
 count_B = 0;
 gps_blocks = 1;
 while (read_data[count_A] != 0) {
   //Serial.println("Not empty");
   if ( (read_data[count_A] != ',') and (read_data[count_A] != 0x0A) and (read_data[count_A] != 0x0D) ) {
     gps_data[count_B] += read_data[count_A];
     //Serial.println("Add Char");
   }
   else {
     count_B++;
     gps_blocks++;
     //Serial.println("Next Block");
   }
   count_A++;
 }
 if (read_data[count_A-1] == ',') {
   gps_blocks--;
 }
 //Serial.println("No More data");
 Serial.print("GPS Blocks: ");
 Serial.println(gps_blocks);
 for (count_A = 0; count_A < gps_blocks; count_A++) {
   Serial.print("GPS Data ");
   Serial.print(count_A);
   Serial.print(": ");
   Serial.println(gps_data[count_A]);
 }
 Serial.println("File open");
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 dataFile.print("GPS Blocks: ");
 dataFile.println(gps_blocks);
 for (count_A = 0; count_A < gps_blocks; count_A++) {
   dataFile.print("GPS Data ");
   dataFile.print(count_A);
   dataFile.print(": ");
   dataFile.println(gps_data[count_A]);
 }
 dataFile.close();
 Serial.println("File close");
}


For some reason, when I add the part in between the Serial.print Open and Close file, the code doesn't work anymore and the Arduino 'locks'.

When the code is with the SD part, it outputs let's say 40 'gps blocks' (depending on the NMEA sentences I use). When the SD code is used, it stops at 38 and then hangs.

Any idea why this is happening? I'm using a Seedstudiob shield if that makes a difference.

Thanks!

PaulS

Quote
For some reason, when I add the part in between the Serial.print Open and Close file, the code doesn't work anymore and the Arduino 'locks'.

The SD library needs 1/4 of the UNO's (or other 328 based Arduino) memory. You probably do not have that much available for it to use.

The F() macro deserves research.

Of course, if you really have a 400 byte array to hold the data read from the GPS, that is far too large.

Soundstorm

Thanks! That did the trick. I held place for 400 bytes because I wasn't sure how much was going to get transferred every time.

I decided to go with GGA, VTC and ZDA, so reduced it to 160 bytes and 40 blocks (some spare just in case).

Is there a way to see how much space is used? Apart from the sketch size.

PaulS

Quote
Apart from the sketch size.

The sketch size tells you nothing about the amount of SRAM used. There is a FreeMemory() function that will tell you how much SRAM is still available.

Go Up