2010-12-03 16:35:17 +01:00
|
|
|
/*
|
|
|
|
SD card read/write
|
|
|
|
|
|
|
|
This example shows how to read and write data to and from 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 18:31:49 +01:00
|
|
|
#include <SD.h>
|
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
File myFile;
|
2010-11-20 18:31:49 +01:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
2012-04-02 15:07:58 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2010-11-20 18:31:49 +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 18:31:49 +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-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-11-20 18:31:49 +01:00
|
|
|
|
2010-12-23 00:04:34 +01:00
|
|
|
// open the file. note that only one file can be open at a time,
|
|
|
|
// so you have to close this one before opening another.
|
2010-12-22 22:52:09 +01:00
|
|
|
myFile = SD.open("test.txt", FILE_WRITE);
|
2010-12-03 16:35:17 +01:00
|
|
|
|
|
|
|
// if the file opened okay, write to it:
|
|
|
|
if (myFile) {
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.print("Writing to test.txt...");
|
2010-12-03 16:35:17 +01:00
|
|
|
myFile.println("testing 1, 2, 3.");
|
|
|
|
// close the file:
|
|
|
|
myFile.close();
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.println("done.");
|
|
|
|
} else {
|
2010-12-03 16:35:17 +01:00
|
|
|
// if the file didn't open, print an error:
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.println("error opening test.txt");
|
|
|
|
}
|
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
// re-open the file for reading:
|
|
|
|
myFile = SD.open("test.txt");
|
|
|
|
if (myFile) {
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.println("test.txt:");
|
2010-12-03 16:35:17 +01:00
|
|
|
|
|
|
|
// read from the file until there's nothing else in it:
|
|
|
|
while (myFile.available()) {
|
|
|
|
Serial.write(myFile.read());
|
|
|
|
}
|
|
|
|
// close the file:
|
|
|
|
myFile.close();
|
2010-11-20 18:31:49 +01:00
|
|
|
} else {
|
2010-12-03 16:35:17 +01:00
|
|
|
// if the file didn't open, print an error:
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.println("error opening test.txt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2010-12-03 16:35:17 +01:00
|
|
|
// nothing happens after setup
|
2010-11-20 18:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|