File tree Expand file tree Collapse file tree 7 files changed +209
-268
lines changed Expand file tree Collapse file tree 7 files changed +209
-268
lines changed Original file line number Diff line number Diff line change 2020Sd2Card card;
2121SdFatFs fatFs;
2222
23- void setup ()
24- {
23+ void setup () {
2524 bool disp = false ;
2625 // Open serial communications and wait for port to open:
2726 Serial.begin (9600 );
2827
29- while (!Serial);
28+ while (!Serial)
29+ ;
3030 Serial.print (" \n Initializing SD card..." );
31- while (!card.init (SD_DETECT_PIN)) {
31+ while (!card.init (SD_DETECT_PIN)) {
3232 if (!disp) {
3333 Serial.println (" initialization failed. Is a card inserted?" );
3434 disp = true ;
@@ -66,9 +66,9 @@ void setup()
6666 Serial.println (fatFs.fatType (), DEC);
6767 Serial.println ();
6868
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
7272 Serial.print (" Volume size (bytes): " );
7373 Serial.println (volumesize);
7474 Serial.print (" Volume size (Kbytes): " );
@@ -85,6 +85,13 @@ void setup()
8585 // list all files in the card with date and size
8686 root.ls (LS_R | LS_DATE | LS_SIZE);
8787 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+
8895 Serial.println (" ###### End of the SD tests ######" );
8996}
9097
Original file line number Diff line number Diff line change 1818#define SD_DETECT_PIN SD_DETECT_NONE
1919#endif
2020
21- uint32_t A[] = { A0, A1, A2};
21+ uint32_t A[] = { A0, A1, A2 };
2222
2323File dataFile;
2424
25- void setup ()
26- {
25+ void setup () {
2726 // Open serial communications and wait for port to open:
2827 Serial.begin (9600 );
2928 while (!Serial) {
30- ; // wait for serial port to connect. Needed for Leonardo only
29+ ; // wait for serial port to connect. Needed for Leonardo only
3130 }
3231
3332 Serial.print (" Initializing SD card..." );
3433 // 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)) {
3735 delay (10 );
3836 }
3937 delay (100 );
@@ -52,8 +50,7 @@ void setup()
5250 }
5351}
5452
55- void loop ()
56- {
53+ void loop () {
5754 // make a string for assembling the data to log:
5855 String dataString = " " ;
5956
@@ -70,7 +67,7 @@ void loop()
7067 // if the file is available, write to it:
7168 if (dataFile) {
7269 dataFile.println (dataString);
73- dataFile.flush (); // use flush to ensure the data written
70+ dataFile.flush (); // use flush to ensure the data written
7471 // print to the serial port too:
7572 Serial.println (dataString);
7673 }
Original file line number Diff line number Diff line change 1919#define SD_DETECT_PIN SD_DETECT_NONE
2020#endif
2121
22- void setup ()
23- {
22+ void setup () {
2423 // Open serial communications and wait for port to open:
2524 Serial.begin (9600 );
2625 while (!Serial) {
27- ; // wait for serial port to connect. Needed for Leonardo only
26+ ; // wait for serial port to connect. Needed for Leonardo only
2827 }
2928
3029
3130 Serial.print (" Initializing SD card..." );
3231 // 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)) {
3533 delay (10 );
3634 }
3735 delay (100 );
@@ -52,9 +50,11 @@ void setup()
5250 else {
5351 Serial.println (" error opening datalog.txt" );
5452 }
53+ if (!SD.end ()) {
54+ Serial.println (" Failed to properly end the SD." );
55+ }
5556 Serial.println (" ###### End of the SD tests ######" );
5657}
5758
58- void loop ()
59- {
59+ void loop () {
6060}
Original file line number Diff line number Diff line change 1818
1919File myFile;
2020
21- void setup ()
22- {
21+ void setup () {
2322 // Open serial communications and wait for port to open:
2423 Serial.begin (9600 );
2524 while (!Serial) {
26- ; // wait for serial port to connect. Needed for Leonardo only
25+ ; // wait for serial port to connect. Needed for Leonardo only
2726 }
2827
2928
3029 Serial.print (" Initializing SD card..." );
3130
32- while (!SD.begin (SD_DETECT_PIN))
33- {
31+ while (!SD.begin (SD_DETECT_PIN)) {
3432 delay (10 );
3533 }
3634 Serial.println (" initialization done." );
3735
3836 if (SD.exists (" example.txt" )) {
3937 Serial.println (" example.txt exists." );
40- }
41- else {
38+ } else {
4239 Serial.println (" example.txt doesn't exist." );
4340 }
4441
4542 // open a new file and immediately close it:
4643 Serial.println (" Creating example.txt..." );
44+ Serial.flush ();
4745 myFile = SD.open (" example.txt" , FILE_WRITE);
4846 myFile.close ();
4947
5048 // Check to see if the file exists:
5149 if (SD.exists (" example.txt" )) {
5250 Serial.println (" example.txt exists." );
53- }
54- else {
51+ } else {
5552 Serial.println (" example.txt doesn't exist." );
5653 }
5754
@@ -61,14 +58,15 @@ void setup()
6158
6259 if (SD.exists (" example.txt" )) {
6360 Serial.println (" example.txt exists." );
64- }
65- else {
61+ } else {
6662 Serial.println (" example.txt doesn't exist." );
6763 }
64+ if (!SD.end ()) {
65+ Serial.println (" Failed to properly end the SD." );
66+ }
6867 Serial.println (" ###### End of the SD tests ######" );
6968}
7069
71- void loop ()
72- {
70+ void loop () {
7371 // nothing happens after setup finishes.
7472}
You can’t perform that action at this time.
0 commit comments