I was checking this code, and trying some others, and the one thing i can`t do is like, if you find this file continue writing on it, i have tried with other commands, and still keep deleting (overwriting) what a I had.
I have made, that in one sesion writes like a countdown like 1, 2, 3, 4, but if I turn off and turn on, it deletes and write again all the data.
/*
* SD Card Access
*
* platformio.ini
[env:esp wrover kit]
platform = espressif32
framework = arduino
board = esp-wrover-kit
monitor_speed = 115200
build_flags = -D LED_BUILTIN=21 -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue
*/
#include <SPI.h>
#include <SD.h>
#include <Arduino.h>
#define SD_CS 13
#define SPI_MOSI 15
#define SPI_MISO 2
#define SPI_SCK 14
File root;
void printDirectory(File dir, int numTabs);
long previousblinkTime = 0;
int ledState = LOW;
void printDirectory(File dir, int numTabs)
{
// Begin
dir.rewindDirectory();
while(true)
{
File entry = dir.openNextFile();
if (! entry)
{
// no more files
break;
}
for (uint8_t i=0; i<numTabs; i++)
{
Serial.print('\t'); // we'll have indentation
}
Serial.print(entry.name());
// Recurse for directories, otherwise print the file size
if (entry.isDirectory())
{
Serial.println("/");
printDirectory(entry, numTabs+1);
}
else
{
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void setup()
{
pinMode(12, OUTPUT);
Serial.begin(115200);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI, SD_CS);
Serial.println("starting");
if(!SD.begin(SD_CS, SPI))
{
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE)
{
Serial.println("No SD card attached");
return;
}
//card details
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
Serial.println("-----");
//list files
root = SD.open("/");
if (root)
{
printDirectory(root, 0);
root.close();
}
else
{
Serial.println("error opening root");
}
//File write
root = SD.open("/test.txt", FILE_WRITE);
if (root)
{
root.println("Hello worl4!");
root.flush();
root.close();
}
else
{
Serial.println("error opening test.txt");
}
delay(1000);
//File Read
root = SD.open("/test.txt");
if (root)
{
while (root.available())
{
Serial.write(root.read());
}
root.close();
}
else
{
Serial.println("error opening test.txt");
}
Serial.println("done!");
}
void loop()
{
// Blink task - 1000ms task
unsigned long CurrentblinkTime = millis();
if(CurrentblinkTime - previousblinkTime > 1000)
{
previousblinkTime = CurrentblinkTime;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(12, ledState);
}
}