Skip to content

Commit 86e3d4a

Browse files
committed
updated SD examples with new constants, and commented them
1 parent dce5e09 commit 86e3d4a

File tree

3 files changed

+106
-34
lines changed

3 files changed

+106
-34
lines changed

libraries/SD/examples/Datalogger/Datalogger.pde

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
** CS - pin 4
1414
1515
created 24 Nov 2010
16+
updated 2 Dec 2010
1617
by Tom Igoe
1718
1819
This example code is in the public domain.
@@ -31,8 +32,9 @@ void setup()
3132
{
3233
Serial.begin(9600);
3334
Serial.print("Initializing SD card...");
34-
35-
pinMode(10, OUTPUT);
35+
// make sure that the default chip select pin is set to
36+
// output, even if you don't use it:
37+
// pinMode(10, OUTPUT);
3638

3739
// see if the card is present and can be initialized:
3840
if (!SD.begin(chipSelect)) {
@@ -58,7 +60,7 @@ void loop()
5860
}
5961

6062
// open the file:
61-
File dataFile = SD.open("datalog.txt", true, true);
63+
File dataFile = SD.open("datalog.txt", FILE_APPEND);
6264

6365
// if the file is available, write to it:
6466
if (dataFile) {

libraries/SD/examples/Files/Files.pde

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,76 @@
1+
/*
2+
SD card basic file example
3+
4+
This example shows how to create and destroy an SD card file
5+
The circuit:
6+
* SD card attached to SPI bus as follows:
7+
** MOSI - pin 11
8+
** MISO - pin 12
9+
** CLK - pin 13
10+
** CS - pin 4
11+
12+
created Nov 2010
13+
by David A. Mellis
14+
updated 2 Dec 2010
15+
by Tom Igoe
16+
17+
This example code is in the public domain.
18+
19+
*/
120
#include <SD.h>
221

3-
File f;
22+
File myFile;
423

524
void setup()
625
{
726
Serial.begin(9600);
827
Serial.print("Initializing SD card...");
9-
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
10-
// used as the CS pin, the hardware SS pin (10 on most Arduino boards,
11-
// 53 on the Mega) must be left as an output or the SD library
12-
// functions will not work.
28+
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
29+
// Note that even if it's not used as the CS pin, the hardware SS pin
30+
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
31+
// or the SD library functions will not work.
32+
1333
if (!SD.begin(4)) {
14-
Serial.println("failed!");
34+
Serial.println("initialization failed!");
1535
return;
1636
}
17-
Serial.println("done.");
18-
19-
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
20-
else Serial.println("example.txt doesn't exist.");
37+
Serial.println("initialization done.");
38+
39+
if (SD.exists("example.txt")) {
40+
Serial.println("example.txt exists.");
41+
}
42+
else {
43+
Serial.println("example.txt doesn't exist.");
44+
}
2145

46+
// open a new file and immediately close it:
2247
Serial.println("Creating example.txt...");
23-
f = SD.open("example.txt", true);
24-
f.close();
48+
myFile = SD.open("example.txt", FILE_TRUNCATE);
49+
myFile.close();
2550

26-
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
27-
else Serial.println("example.txt doesn't exist.");
51+
// Check to see if the file exists:
52+
if (SD.exists("example.txt")) {
53+
Serial.println("example.txt exists.");
54+
}
55+
else {
56+
Serial.println("example.txt doesn't exist.");
57+
}
2858

59+
// delete the file:
2960
Serial.println("Removing example.txt...");
3061
SD.remove("example.txt");
3162

32-
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
33-
else Serial.println("example.txt doesn't exist.");
63+
if (SD.exists("example.txt")){
64+
Serial.println("example.txt exists.");
65+
}
66+
else {
67+
Serial.println("example.txt doesn't exist.");
68+
}
3469
}
3570

3671
void loop()
3772
{
73+
// nothing happens after setup finishes.
3874
}
3975

4076

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,77 @@
1+
/*
2+
SD card read/write
3+
4+
This example shows how to read and write data to and from an SD card file
5+
The circuit:
6+
* SD card attached to SPI bus as follows:
7+
** MOSI - pin 11
8+
** MISO - pin 12
9+
** CLK - pin 13
10+
** CS - pin 4
11+
12+
created Nov 2010
13+
by David A. Mellis
14+
updated 2 Dec 2010
15+
by Tom Igoe
16+
17+
This example code is in the public domain.
18+
19+
*/
20+
121
#include <SD.h>
222

3-
File f;
23+
File myFile;
424

525
void setup()
626
{
727
Serial.begin(9600);
828
Serial.print("Initializing SD card...");
9-
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
10-
// used as the CS pin, the hardware SS pin (10 on most Arduino boards,
11-
// 53 on the Mega) must be left as an output or the SD library
12-
// functions will not work.
29+
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
30+
// Note that even if it's not used as the CS pin, the hardware SS pin
31+
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
32+
// or the SD library functions will not work.
33+
1334
if (!SD.begin(4)) {
14-
Serial.println("failed!");
35+
Serial.println("initialization failed!");
1536
return;
1637
}
17-
Serial.println("done.");
38+
Serial.println("initialization done.");
1839

19-
f = SD.open("test.txt", true, false);
20-
if (f) {
40+
// open a file:
41+
myFile = SD.open("test.txt", FILE_TRUNCATE);
42+
43+
// if the file opened okay, write to it:
44+
if (myFile) {
2145
Serial.print("Writing to test.txt...");
22-
f.println("testing 1, 2, 3.");
23-
f.close();
46+
myFile.println("testing 1, 2, 3.");
47+
// close the file:
48+
myFile.close();
2449
Serial.println("done.");
2550
} else {
51+
// if the file didn't open, print an error:
2652
Serial.println("error opening test.txt");
2753
}
2854

29-
f = SD.open("test.txt");
30-
if (f) {
55+
// re-open the file for reading:
56+
myFile = SD.open("test.txt");
57+
if (myFile) {
3158
Serial.println("test.txt:");
32-
while (f.available()) Serial.write(f.read());
33-
f.close();
59+
60+
// read from the file until there's nothing else in it:
61+
while (myFile.available()) {
62+
Serial.write(myFile.read());
63+
}
64+
// close the file:
65+
myFile.close();
3466
} else {
67+
// if the file didn't open, print an error:
3568
Serial.println("error opening test.txt");
3669
}
3770
}
3871

3972
void loop()
4073
{
74+
// nothing happens after setup
4175
}
4276

4377

0 commit comments

Comments
 (0)