root / arduino-1.0 / libraries / SD / examples / Files / Files.ino @ 58d82c77
History | View | Annotate | Download (1.64 KB)
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 |
** MOSI - pin 11 |
8 |
** MISO - pin 12 |
9 |
** CLK - pin 13 |
10 |
** CS - pin 4 |
11 |
|
12 |
created Nov 2010 |
13 |
by David A. Mellis |
14 |
updated 2 Dec 2010 |
15 |
by Tom Igoe |
16 |
|
17 |
This example code is in the public domain. |
18 |
|
19 |
*/ |
20 |
#include <SD.h> |
21 |
|
22 |
File myFile; |
23 |
|
24 |
void setup() |
25 |
{ |
26 |
Serial.begin(9600); |
27 |
Serial.print("Initializing SD card..."); |
28 |
// On the Ethernet Shield, CS is pin 4. It's set as an output by default. |
29 |
// Note that even if it's not used as the CS pin, the hardware SS pin |
30 |
// (10 on most Arduino boards, 53 on the Mega) must be left as an output |
31 |
// or the SD library functions will not work. |
32 |
pinMode(10, OUTPUT); |
33 |
|
34 |
if (!SD.begin(4)) { |
35 |
Serial.println("initialization failed!"); |
36 |
return; |
37 |
} |
38 |
Serial.println("initialization done."); |
39 |
|
40 |
if (SD.exists("example.txt")) { |
41 |
Serial.println("example.txt exists."); |
42 |
} |
43 |
else { |
44 |
Serial.println("example.txt doesn't exist."); |
45 |
} |
46 |
|
47 |
// open a new file and immediately close it: |
48 |
Serial.println("Creating example.txt..."); |
49 |
myFile = SD.open("example.txt", FILE_WRITE); |
50 |
myFile.close(); |
51 |
|
52 |
// Check to see if the file exists: |
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 |
// delete the file: |
61 |
Serial.println("Removing example.txt..."); |
62 |
SD.remove("example.txt"); |
63 |
|
64 |
if (SD.exists("example.txt")){ |
65 |
Serial.println("example.txt exists."); |
66 |
} |
67 |
else { |
68 |
Serial.println("example.txt doesn't exist."); |
69 |
} |
70 |
} |
71 |
|
72 |
void loop() |
73 |
{ |
74 |
// nothing happens after setup finishes. |
75 |
} |
76 |
|
77 |
|
78 |
|