//https://forum.arduino.cc/t/gravando-dados-cartao-sd/1271808
#include <SD.h>
long count = 0;
const int chipSelect = 15;
File myFile;
//------------------------------------------------------------------
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
// wait for Serial Monitor to connect. Needed for native USB port boards only:
while (!Serial);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed");
while (true);
}
Serial.println("initialization done.");
// 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 (!SD.exists("test.txt")) {
myFile = SD.open("test.txt", FILE_WRITE); //If no exist, create file
if (myFile) { // if the file opened okay, write to it:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
}
//------------------------------------------------------------------
void loop() {
delay(2000);
count += 25;
readSD();
}
//------------------------------------------------------------------
void readSD() {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", O_RDWR); // Open no append
if (myFile) { // if the file opened okay, write to it:
myFile.println(count);
myFile.close();
} else {
Serial.println("error opening test.txt");
}
myFile = SD.open("test.txt");
if (myFile) {
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening test.txt");
}
}