File tree 7 files changed +209
-268
lines changed
7 files changed +209
-268
lines changed Original file line number Diff line number Diff line change 20
20
Sd2Card card;
21
21
SdFatFs fatFs;
22
22
23
- void setup ()
24
- {
23
+ void setup () {
25
24
bool disp = false ;
26
25
// Open serial communications and wait for port to open:
27
26
Serial.begin (9600 );
28
27
29
- while (!Serial);
28
+ while (!Serial)
29
+ ;
30
30
Serial.print (" \n Initializing SD card..." );
31
- while (!card.init (SD_DETECT_PIN)) {
31
+ while (!card.init (SD_DETECT_PIN)) {
32
32
if (!disp) {
33
33
Serial.println (" initialization failed. Is a card inserted?" );
34
34
disp = true ;
@@ -66,9 +66,9 @@ void setup()
66
66
Serial.println (fatFs.fatType (), DEC);
67
67
Serial.println ();
68
68
69
- volumesize = fatFs.blocksPerCluster (); // clusters are collections of blocks
70
- volumesize *= fatFs.clusterCount (); // we'll have a lot of clusters
71
- volumesize *= 512 ; // SD card blocks are always 512 bytes
69
+ volumesize = fatFs.blocksPerCluster (); // clusters are collections of blocks
70
+ volumesize *= fatFs.clusterCount (); // we'll have a lot of clusters
71
+ volumesize *= 512 ; // SD card blocks are always 512 bytes
72
72
Serial.print (" Volume size (bytes): " );
73
73
Serial.println (volumesize);
74
74
Serial.print (" Volume size (Kbytes): " );
@@ -85,6 +85,13 @@ void setup()
85
85
// list all files in the card with date and size
86
86
root.ls (LS_R | LS_DATE | LS_SIZE);
87
87
root.close ();
88
+ if (!fatFs.deinit ()) {
89
+ Serial.println (" Failed to deinit card" );
90
+ }
91
+ if (!card.deinit ()) {
92
+ Serial.println (" Failed to deinit card" );
93
+ }
94
+
88
95
Serial.println (" ###### End of the SD tests ######" );
89
96
}
90
97
Original file line number Diff line number Diff line change 18
18
#define SD_DETECT_PIN SD_DETECT_NONE
19
19
#endif
20
20
21
- uint32_t A[] = { A0, A1, A2};
21
+ uint32_t A[] = { A0, A1, A2 };
22
22
23
23
File dataFile;
24
24
25
- void setup ()
26
- {
25
+ void setup () {
27
26
// Open serial communications and wait for port to open:
28
27
Serial.begin (9600 );
29
28
while (!Serial) {
30
- ; // wait for serial port to connect. Needed for Leonardo only
29
+ ; // wait for serial port to connect. Needed for Leonardo only
31
30
}
32
31
33
32
Serial.print (" Initializing SD card..." );
34
33
// see if the card is present and can be initialized:
35
- while (!SD.begin (SD_DETECT_PIN))
36
- {
34
+ while (!SD.begin (SD_DETECT_PIN)) {
37
35
delay (10 );
38
36
}
39
37
delay (100 );
@@ -52,8 +50,7 @@ void setup()
52
50
}
53
51
}
54
52
55
- void loop ()
56
- {
53
+ void loop () {
57
54
// make a string for assembling the data to log:
58
55
String dataString = " " ;
59
56
@@ -70,7 +67,7 @@ void loop()
70
67
// if the file is available, write to it:
71
68
if (dataFile) {
72
69
dataFile.println (dataString);
73
- dataFile.flush (); // use flush to ensure the data written
70
+ dataFile.flush (); // use flush to ensure the data written
74
71
// print to the serial port too:
75
72
Serial.println (dataString);
76
73
}
Original file line number Diff line number Diff line change 19
19
#define SD_DETECT_PIN SD_DETECT_NONE
20
20
#endif
21
21
22
- void setup ()
23
- {
22
+ void setup () {
24
23
// Open serial communications and wait for port to open:
25
24
Serial.begin (9600 );
26
25
while (!Serial) {
27
- ; // wait for serial port to connect. Needed for Leonardo only
26
+ ; // wait for serial port to connect. Needed for Leonardo only
28
27
}
29
28
30
29
31
30
Serial.print (" Initializing SD card..." );
32
31
// see if the card is present and can be initialized:
33
- while (!SD.begin (SD_DETECT_PIN))
34
- {
32
+ while (!SD.begin (SD_DETECT_PIN)) {
35
33
delay (10 );
36
34
}
37
35
delay (100 );
@@ -52,9 +50,11 @@ void setup()
52
50
else {
53
51
Serial.println (" error opening datalog.txt" );
54
52
}
53
+ if (!SD.end ()) {
54
+ Serial.println (" Failed to properly end the SD." );
55
+ }
55
56
Serial.println (" ###### End of the SD tests ######" );
56
57
}
57
58
58
- void loop ()
59
- {
59
+ void loop () {
60
60
}
Original file line number Diff line number Diff line change 18
18
19
19
File myFile;
20
20
21
- void setup ()
22
- {
21
+ void setup () {
23
22
// Open serial communications and wait for port to open:
24
23
Serial.begin (9600 );
25
24
while (!Serial) {
26
- ; // wait for serial port to connect. Needed for Leonardo only
25
+ ; // wait for serial port to connect. Needed for Leonardo only
27
26
}
28
27
29
28
30
29
Serial.print (" Initializing SD card..." );
31
30
32
- while (!SD.begin (SD_DETECT_PIN))
33
- {
31
+ while (!SD.begin (SD_DETECT_PIN)) {
34
32
delay (10 );
35
33
}
36
34
Serial.println (" initialization done." );
37
35
38
36
if (SD.exists (" example.txt" )) {
39
37
Serial.println (" example.txt exists." );
40
- }
41
- else {
38
+ } else {
42
39
Serial.println (" example.txt doesn't exist." );
43
40
}
44
41
45
42
// open a new file and immediately close it:
46
43
Serial.println (" Creating example.txt..." );
44
+ Serial.flush ();
47
45
myFile = SD.open (" example.txt" , FILE_WRITE);
48
46
myFile.close ();
49
47
50
48
// Check to see if the file exists:
51
49
if (SD.exists (" example.txt" )) {
52
50
Serial.println (" example.txt exists." );
53
- }
54
- else {
51
+ } else {
55
52
Serial.println (" example.txt doesn't exist." );
56
53
}
57
54
@@ -61,14 +58,15 @@ void setup()
61
58
62
59
if (SD.exists (" example.txt" )) {
63
60
Serial.println (" example.txt exists." );
64
- }
65
- else {
61
+ } else {
66
62
Serial.println (" example.txt doesn't exist." );
67
63
}
64
+ if (!SD.end ()) {
65
+ Serial.println (" Failed to properly end the SD." );
66
+ }
68
67
Serial.println (" ###### End of the SD tests ######" );
69
68
}
70
69
71
- void loop ()
72
- {
70
+ void loop () {
73
71
// nothing happens after setup finishes.
74
72
}
You can’t perform that action at this time.
0 commit comments