Skip to content

Commit 6f7c332

Browse files
masrodjiekrusita
andauthored
Fix Datalogger.ino example (#25)
* Fix Datalogger.ino example fragments the memory because of loop opening & closing the file. This will make the memory not able to allocate anymore. Keep the file opened and use flush to write the data instead opening & closing the file. Co-authored-by: Arnas MasRodjie <[email protected]>
1 parent 68eeafa commit 6f7c332

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

examples/Datalogger/Datalogger.ino

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
uint32_t A[] = { A0, A1, A2};
2222

23+
File dataFile;
24+
2325
void setup()
2426
{
2527
// Open serial communications and wait for port to open:
@@ -36,6 +38,18 @@ void setup()
3638
}
3739
delay(100);
3840
Serial.println("card initialized.");
41+
42+
// open the file. note that only one file can be open at a time,
43+
// so you have to close this one before opening another.
44+
dataFile = SD.open("datalog.txt", FILE_WRITE);
45+
// if the file is available, seek to last position
46+
if (dataFile) {
47+
dataFile.seek(dataFile.size());
48+
}
49+
// if the file isn't open, pop up an error:
50+
else {
51+
Serial.println("error opening datalog.txt");
52+
}
3953
}
4054

4155
void loop()
@@ -52,21 +66,17 @@ void loop()
5266
}
5367
}
5468

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

5970
// if the file is available, write to it:
6071
if (dataFile) {
61-
dataFile.seek(dataFile.size());
6272
dataFile.println(dataString);
63-
dataFile.close();
73+
dataFile.flush(); // use flush to ensure the data written
6474
// print to the serial port too:
6575
Serial.println(dataString);
6676
}
6777
// if the file isn't open, pop up an error:
6878
else {
69-
Serial.println("error opening datalog.txt");
79+
Serial.println("error on datalog.txt file handle");
7080
}
7181
delay(100);
7282
}

0 commit comments

Comments
 (0)