2010-12-03 16:35:17 +01:00
|
|
|
/*
|
|
|
|
SD card read/write
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This example shows how to read and write data to and from an SD card file
|
2010-12-03 16:35:17 +01:00
|
|
|
The circuit:
|
|
|
|
* SD card attached to SPI bus as follows:
|
|
|
|
** MOSI - pin 11
|
|
|
|
** MISO - pin 12
|
|
|
|
** CLK - pin 13
|
|
|
|
** CS - pin 4
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
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
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
*/
|
2013-10-21 09:58:40 +02:00
|
|
|
|
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
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void setup() {
|
2013-10-21 09:58:40 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.begin(9600);
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2012-04-09 15:51:35 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.print("Initializing SD card...");
|
2013-10-21 09:58:40 +02: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.");
|
2013-10-21 09:58:40 +02: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);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
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.");
|
2013-10-21 09:58:40 +02:00
|
|
|
// close the file:
|
2010-12-03 16:35:17 +01:00
|
|
|
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");
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
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:");
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-12-03 16:35:17 +01:00
|
|
|
// read from the file until there's nothing else in it:
|
|
|
|
while (myFile.available()) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.write(myFile.read());
|
2010-12-03 16:35:17 +01:00
|
|
|
}
|
|
|
|
// close the file:
|
|
|
|
myFile.close();
|
2010-11-20 18:31:49 +01:00
|
|
|
} else {
|
2013-10-21 09:58:40 +02:00
|
|
|
// if the file didn't open, print an error:
|
2010-11-20 18:31:49 +01:00
|
|
|
Serial.println("error opening test.txt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void loop() {
|
2013-10-21 09:58:40 +02:00
|
|
|
// nothing happens after setup
|
2010-11-20 18:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|