| 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 (for MKRZero SD: SDCARD_SS_PIN) |
| 11 |
|
| 12 |
created Nov 2010 |
| 13 |
by David A. Mellis |
| 14 |
modified 9 Apr 2012 |
| 15 |
by Tom Igoe |
| 16 |
|
| 17 |
This example code is in the public domain. |
| 18 |
|
| 19 |
*/ |
| 20 |
#include <SPI.h> |
| 21 |
#include <SD.h> |
| 22 |
|
| 23 |
File myFile; |
| 24 |
|
| 25 |
void setup() { |
| 26 |
// Open serial communications and wait for port to open: |
| 27 |
Serial.begin(9600); |
| 28 |
while (!Serial) { |
| 29 |
; // wait for serial port to connect. Needed for native USB port only |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
Serial.print("Initializing SD card..."); |
| 34 |
|
| 35 |
if (!SD.begin(4)) { |
| 36 |
Serial.println("initialization failed!"); |
| 37 |
while (1); |
| 38 |
} |
| 39 |
Serial.println("initialization done."); |
| 40 |
|
| 41 |
if (SD.exists("example.txt")) { |
| 42 |
Serial.println("example.txt exists."); |
| 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 |
} else { |
| 56 |
Serial.println("example.txt doesn't exist."); |
| 57 |
} |
| 58 |
|
| 59 |
// delete the file: |
| 60 |
Serial.println("Removing example.txt..."); |
| 61 |
SD.remove("example.txt"); |
| 62 |
|
| 63 |
if (SD.exists("example.txt")) { |
| 64 |
Serial.println("example.txt exists."); |
| 65 |
} else { |
| 66 |
Serial.println("example.txt doesn't exist."); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void loop() { |
| 71 |
// nothing happens after setup finishes. |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|