2010-12-03 16:35:17 +01:00
|
|
|
/*
|
|
|
|
SD card basic file example
|
|
|
|
|
|
|
|
This example shows how to create and destroy an SD card file
|
|
|
|
The circuit:
|
|
|
|
* SD card attached to SPI bus as follows:
|
|
|
|
** MOSI - pin 11
|
|
|
|
** MISO - pin 12
|
|
|
|
** CLK - pin 13
|
|
|
|
** CS - pin 4
|
|
|
|
|
|
|
|
created Nov 2010
|
|
|
|
by David A. Mellis
|
2012-04-09 15:51:35 +02:00
|
|
|
modified 9 Apr 2012
|
2010-12-03 16:35:17 +01:00
|
|
|
by Tom Igoe
|
|
|
|
|
|
|
|
This example code is in the public domain.
|
|
|
|
|
|
|
|
*/
|
2010-11-20 19:07:59 +01:00
|
|
|
#include <SD.h>
|
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
File myFile;
|
2010-11-20 19:07:59 +01:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
2012-04-02 15:07:58 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2010-11-20 19:07:59 +01:00
|
|
|
Serial.begin(9600);
|
2012-04-09 15:51:35 +02:00
|
|
|
while (!Serial) {
|
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
|
|
|
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2010-11-20 19:07:59 +01:00
|
|
|
Serial.print("Initializing SD card...");
|
2010-12-03 16:35:17 +01:00
|
|
|
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
|
|
|
|
// Note that even if it's not used as the CS pin, the hardware SS pin
|
|
|
|
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
|
|
|
|
// or the SD library functions will not work.
|
2010-12-03 21:29:56 +01:00
|
|
|
pinMode(10, OUTPUT);
|
2010-12-03 16:35:17 +01:00
|
|
|
|
2010-11-20 19:50:45 +01:00
|
|
|
if (!SD.begin(4)) {
|
2010-12-03 16:35:17 +01:00
|
|
|
Serial.println("initialization failed!");
|
2010-11-20 19:50:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-12-03 16:35:17 +01:00
|
|
|
Serial.println("initialization done.");
|
2010-12-03 21:29:56 +01:00
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
if (SD.exists("example.txt")) {
|
2010-12-03 21:29:56 +01:00
|
|
|
Serial.println("example.txt exists.");
|
2010-12-03 16:35:17 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-12-03 21:29:56 +01:00
|
|
|
Serial.println("example.txt doesn't exist.");
|
2010-12-03 16:35:17 +01:00
|
|
|
}
|
2010-12-03 21:29:56 +01:00
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
// open a new file and immediately close it:
|
2010-11-20 19:07:59 +01:00
|
|
|
Serial.println("Creating example.txt...");
|
2010-12-22 22:52:09 +01:00
|
|
|
myFile = SD.open("example.txt", FILE_WRITE);
|
2010-12-03 21:29:56 +01:00
|
|
|
myFile.close();
|
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
// Check to see if the file exists:
|
|
|
|
if (SD.exists("example.txt")) {
|
2010-12-03 21:29:56 +01:00
|
|
|
Serial.println("example.txt exists.");
|
2010-12-03 16:35:17 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-12-03 21:29:56 +01:00
|
|
|
Serial.println("example.txt doesn't exist.");
|
2010-12-03 16:35:17 +01:00
|
|
|
}
|
2010-12-03 21:29:56 +01:00
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
// delete the file:
|
2010-11-20 19:07:59 +01:00
|
|
|
Serial.println("Removing example.txt...");
|
|
|
|
SD.remove("example.txt");
|
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
if (SD.exists("example.txt")){
|
2010-12-03 21:29:56 +01:00
|
|
|
Serial.println("example.txt exists.");
|
2010-12-03 16:35:17 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-12-03 21:29:56 +01:00
|
|
|
Serial.println("example.txt doesn't exist.");
|
2010-12-03 16:35:17 +01:00
|
|
|
}
|
2010-11-20 19:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2010-12-03 21:29:56 +01:00
|
|
|
// nothing happens after setup finishes.
|
2010-11-20 19:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-03 21:29:56 +01:00
|
|
|
|