Skip to content

Commit 5e542ba

Browse files
committed
SD.open fail check in readSDfile example
1 parent 8540bda commit 5e542ba

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

examples/reading_from_sd_card/reading_from_sd_card.ino

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,27 @@ void setup() {
3636

3737

3838
CSV_Parser cp(/*format*/ "dd", /*has_header*/ true, /*delimiter*/ ',');
39-
cp.readSDfile("/file.csv"); // this wouldn't work if SD.begin wasn't called before
40-
41-
int16_t *column_1 = (int16_t*)cp["column_1"];
42-
int16_t *column_2 = (int16_t*)cp["column_2"];
43-
44-
if (column_1 && column_2) {
45-
for(int row = 0; row < cp.getRowsCount(); row++) {
46-
Serial.print("row = ");
47-
Serial.print(row, DEC);
48-
Serial.print(", column_1 = ");
49-
Serial.print(column_1[row], DEC);
50-
Serial.print(", column_2 = ");
51-
Serial.println(column_2[row], DEC);
39+
40+
// The line below (readSDfile) wouldn't work if SD.begin wasn't called before.
41+
// readSDfile can be used as conditional, it returns 'false' if the file does not exist.
42+
if (cp.readSDfile("/file.csv")) {
43+
int16_t *column_1 = (int16_t*)cp["column_1"];
44+
int16_t *column_2 = (int16_t*)cp["column_2"];
45+
46+
if (column_1 && column_2) {
47+
for(int row = 0; row < cp.getRowsCount(); row++) {
48+
Serial.print("row = ");
49+
Serial.print(row, DEC);
50+
Serial.print(", column_1 = ");
51+
Serial.print(column_1[row], DEC);
52+
Serial.print(", column_2 = ");
53+
Serial.println(column_2[row], DEC);
54+
}
55+
} else {
56+
Serial.println("ERROR: At least 1 of the columns was not found, something went wrong.");
5257
}
5358
} else {
54-
Serial.println("At least 1 of the columns was not found, something went wrong.");
59+
Serial.println("ERROR: File called '/file.csv' does not exist...");
5560
}
5661

5762
// output parsed values (allows to check that the file was parsed correctly)

0 commit comments

Comments
 (0)