User Manual

9
28.
Serial.begin(9600);
29.
while (!Serial) {
30.
; // wait for serial port to connect. Needed for native USB port only
31.
}
32.
33.
34.
Serial.print("Initializing SD card...");
35.
36.
if (!SD.begin(4)) {
37.
Serial.println("initialization failed!");
38.
while (1);
39.
}
40.
Serial.println("initialization done.");
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.
myFile = SD.open("test.txt", FILE_WRITE);
45.
46.
// if the file opened okay, write to it:
47.
if (myFile) {
48.
Serial.print("Writing to test.txt...");
49.
myFile.println("testing 1, 2, 3.");
50.
// close the file:
51.
myFile.close();
52.
Serial.println("done.");
53.
} else {
54.
// if the file didn't open, print an error:
55.
Serial.println("error opening test.txt");
56.
}
57.
58.
// re-open the file for reading:
59.
myFile = SD.open("test.txt");
60.
if (myFile) {
61.
Serial.println("test.txt:");
62.
63.
// read from the file until there's nothing else in it:
64.
while (myFile.available()) {
65.
Serial.write(myFile.read());
66.
}
67.
// close the file:
68.
myFile.close();
69.
} else {
70.
// if the file didn't open, print an error:
71.
Serial.println("error opening test.txt");
72.
}
73.
}
74.
75.
void loop() {
76.
// nothing happens after setup
77.
}