Pages: [1]   Go Down
Author Topic: Problem with datalogger example in SD library  (Read 1626 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found a bug in the datalogger program in the SD lib.

Took me a while to figure it out. Program ran fine for several iterations, then crashed.
If you want, I can post the datalogger that I'm developing. It uses a DS1307 clock to timestamp the data from analog and digital events.


Code as posted to lib

...............

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

                              <<This creates a NEW string in each loop
                              <<move the create to global and null out the
                              <<string in loop

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

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

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

..................................
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
   Well done for finding that.

I guess the ultimate fix is to replace the string object with a static buffer that way there is no dependency on the string class or how it manages memory internally which could change between Arduino versions.

Duane B

rcarduino.blogspot.com


Logged


Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 24
Posts: 1764
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I can post the datalogger that I'm developing. It uses a DS1307 clock to timestamp the data from analog and digital events.

Please do! Some of us are working on these same things...
Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Pages: [1]   Go Up
Jump to: