Debian Codegraph @

app/testdata/libraries/SD_1.2.1/SD/examples/ReadWrite/ReadWrite.ino

raw
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 (for MKRZero SD: SDCARD_SS_PIN)
11
12 created Nov 2010
13 by David A. Mellis
14 modified 9 Apr 2012
15 by Tom Igoe
16
17 This example code is in the public domain.
18
19 */
20
21 #include <SPI.h>
22 #include <SD.h>
23
24 File myFile;
25
26 void setup() {
27 // Open serial communications and wait for port to open:
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 }
78
79