/*
* MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
Serial.begin(9600);
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
myFile = SD.open("test.txt", FILE_WRITE); // open the file. note that only one file can be open at a time, so you have to close this one before opening another.
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3."); // if the file opened okay, write to it:
myFile.close(); // close the file:
Serial.println("done.");
} else {
Serial.println("error opening test.txt"); // if the file didn't open, print an error:
}
myFile = SD.open("test.txt"); // re-open the file for reading:
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
Serial.write(myFile.read()); // read from the file until there's nothing else in it:
}
myFile.close();
} else {
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
|