]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/SD/examples/Files/Files.pde
first commit
[arduino] / books / pdummies / Libraries / SD / examples / Files / Files.pde
1 /*
2   SD card basic file example
3  
4  This example shows how to create and destroy an SD card file   
5  The circuit:
6  * SD card attached to SPI bus as follows:
7  ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
8   and pin #10 (SS) must be an output
9  ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
10   and pin #52 (SS) must be an output
11  ** Leonardo: Connect to hardware SPI via the ICSP header
12  
13  created   Nov 2010
14  by David A. Mellis
15  modified 9 Apr 2012
16  by Tom Igoe
17  
18  This example code is in the public domain.
19          
20  */
21 #include <SD.h>
22
23 File myFile;
24
25 // change this to match your SD shield or module;
26 //     Arduino Ethernet shield: pin 4
27 //     Adafruit SD shields and modules: pin 10
28 //     Sparkfun SD shield: pin 8
29 const int chipSelect = 4;
30
31 void setup()
32 {
33  // Open serial communications and wait for port to open:
34   Serial.begin(9600);
35    while (!Serial) {
36     ; // wait for serial port to connect. Needed for Leonardo only
37   }
38
39
40   Serial.print("Initializing SD card...");
41   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
42   // Note that even if it's not used as the CS pin, the hardware SS pin 
43   // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
44   // or the SD library functions will not work. 
45   pinMode(SS, OUTPUT);
46
47   if (!SD.begin(chipSelect)) {
48     Serial.println("initialization failed!");
49     return;
50   }
51   Serial.println("initialization done.");
52
53   if (SD.exists("example.txt")) {
54     Serial.println("example.txt exists.");
55   }
56   else {
57     Serial.println("example.txt doesn't exist.");
58   }
59
60   // open a new file and immediately close it:
61   Serial.println("Creating example.txt...");
62   myFile = SD.open("example.txt", FILE_WRITE);
63   myFile.close();
64
65   // Check to see if the file exists: 
66   if (SD.exists("example.txt")) {
67     Serial.println("example.txt exists.");
68   }
69   else {
70     Serial.println("example.txt doesn't exist.");  
71   }
72
73   // delete the file:
74   Serial.println("Removing example.txt...");
75   SD.remove("example.txt");
76
77   if (SD.exists("example.txt")){ 
78     Serial.println("example.txt exists.");
79   }
80   else {
81     Serial.println("example.txt doesn't exist.");  
82   }
83 }
84
85 void loop()
86 {
87   // nothing happens after setup finishes.
88 }
89
90
91