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}
0 commit comments