Go Down

Topic: Data Logger SD, String stops earlier than expected (Read 1 time) previous topic - next topic

simplytuff


i am trying to  log voltage & current of battery on SD card, the programme terminates before going thru complete loop of 20 readings each after 0.5Sec, if i remove current and voltage string from the code it prints ID upto 20 readings fine, but if i add voltage and/or current to string it stops randomly! some times after 6 readings some times after 13 etc.....

Code: [Select]
void loop()
{
  File logFile = SD.open("LOG.csv", FILE_WRITE);
  while (id<=20)
  {
  //Main battery Voltage
  int voltage = analogRead(1);
  //Sense Resistance/ Current Drawn
  int current = analogRead(2);
 
   
  //Create Data string for storing to SD card 
  String dataString = String(id)+ ", " +String(voltage)+", "+ String(current);

  if (logFile)
  {
    logFile.println(dataString);
   
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  //Increment ID number
  id++;
  delay(500);
  }
  logFile.close();
}

wanderson

Probably a result of the well documented memory leaks in the string library.
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

simplytuff

any solution to this problem? or alternate function to use instead of string?

dxw00d

There's no need to create Strings at all.

Instead of:
Code: [Select]
String dataString = String(id)+ ", " +String(voltage)+", "+ String(current);

  if (logFile)
  {
    logFile.println(dataString);
   
    Serial.println(dataString);
  }

Use:
Code: [Select]
if (logFile)
  {
    logFile.print(id);
    logFile.print(",");
    logFile.print(voltage);
    logFile.print(",");
    logFile.println(current);
   
    Serial.print(id);
    Serial.print(",");
    Serial.print(voltage);
    Serial.print(",");
    Serial.println(current);
  }

fat16lib

Looks like String corrupts the heap.  I added freeMemory() to a simplified version of the program and the result makes no sense.  The problem seems to be with the free list.

Here is the simple program that fails:
Code: [Select]

#include <MemoryFree.h>
void setup() {
  Serial.begin(9600);
  Serial.println(freeMemory());
}

void loop() {
  int id = 0;
  while (id <= 20)
  {
    //Main battery Voltage
    int voltage = 20*id;  //analogRead(1);
    //Sense Resistance/ Current Drawn
    int current = 30*id;  //analogRead(2);

    //Create Data string for storing to SD card 
    String dataString = String(id)+ ", " +String(voltage)+", "+ String(current)+", "+String(freeMemory());

    Serial.println(dataString);
    //Increment ID number
    id++;
    delay(500);
  }
}


Here is the printout.  The last column is the value returned by freeMemory() which is nonsense.
Quote

0, 0, 0, 1742
1, 20, 30, 1740
2, 40, 60, 13050
3, 60, 90, 24360
4, 80, 120, -29868
5, 100, 150, -17534
6, 120, 180, -3919
7, 140, 210, 10464
8, 160, 240, 23056
9, 180, 270, -29120

If I comment out this line in freeMemory
Code: [Select]

//    free_memory += freeListSize();

I get this
Quote

1828
0, 0, 0, 1742
1, 20, 30, 1703
2, 40, 60, 1703
3, 60, 90, 1703
4, 80, 120, 1703
5, 100, 150, 1703
6, 120, 180, 1703
7, 140, 210, 1703
8, 160, 240, 1703
9, 180, 270, 1703

So a bug in String must write over the free list.

fat16lib


simplytuff

Thnks for the help, both solution works fine,  i found dxw00d's solution much simpler!

Go Up