Pages: [1]   Go Down
Author Topic: Data Logger SD, String stops earlier than expected  (Read 1709 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
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();
}
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 31
Posts: 887
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Probably a result of the well documented memory leaks in the string library.
Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There's no need to create Strings at all.

Instead of:
Code:
String dataString = String(id)+ ", " +String(voltage)+", "+ String(current);
 
  if (logFile)
  {
    logFile.println(dataString);
   
    Serial.println(dataString);
  }
Use:
Code:
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);
  }
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
//    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.
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Gasp!  The problem appears to be in free() http://arduino.cc/forum/index.php/topic,115552.msg869983.html#msg869983.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: