Problem with datalogger example in SD library
i found bug in datalogger program in sd lib.
took me while figure out. program ran fine several iterations, crashed.
if want, can post datalogger i'm developing. uses ds1307 clock timestamp data analog , digital events.
code posted lib
...............
void loop()
{
// make string assembling data log:
string datastring = ""; <<<<<<<<<<<<<------------------bad, bad
<<this creates new string in each loop
<<move create global , null out
<<string in loop
// read 3 sensors , append string:
(int analogpin = 0; analogpin < 3; analogpin++) {
int sensor = analogread(analogpin);
datastring += string(sensor);
if (analogpin < 2) {
datastring += ",";
}
}
// open file. note 1 file can open @ time,
// have close 1 before opening another.
file datafile = sd.open("datalog.txt", file_write);
// if file available, write it:
if (datafile) {
datafile.println(datastring);
datafile.close();
// print serial port too:
serial.println(datastring);
}
// if file isn't open, pop error:
else {
serial.println("error opening datalog.txt");
}
}
..................................
took me while figure out. program ran fine several iterations, crashed.
if want, can post datalogger i'm developing. uses ds1307 clock timestamp data analog , digital events.
code posted lib
...............
void loop()
{
// make string assembling data log:
string datastring = ""; <<<<<<<<<<<<<------------------bad, bad
<<this creates new string in each loop
<<move create global , null out
<<string in loop
// read 3 sensors , append string:
(int analogpin = 0; analogpin < 3; analogpin++) {
int sensor = analogread(analogpin);
datastring += string(sensor);
if (analogpin < 2) {
datastring += ",";
}
}
// open file. note 1 file can open @ time,
// have close 1 before opening another.
file datafile = sd.open("datalog.txt", file_write);
// if file available, write it:
if (datafile) {
datafile.println(datastring);
datafile.close();
// print serial port too:
serial.println(datastring);
}
// if file isn't open, pop error:
else {
serial.println("error opening datalog.txt");
}
}
..................................
hi,
well done finding that.
i guess ultimate fix replace string object static buffer way there no dependency on string class or how manages memory internally change between arduino versions.
duane b
rcarduino.blogspot.com
well done finding that.
i guess ultimate fix replace string object static buffer way there no dependency on string class or how manages memory internally change between arduino versions.
duane b
rcarduino.blogspot.com
Arduino Forum > Community > Website and Forum > Problem with datalogger example in SD library
arduino
Comments
Post a Comment