Im trying to use an SD card module with ESP32 but it seems that the function println is not working properly and is overwriting the data so it just keep the last data. Heres the code im using:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 5;
int dat = 0;
String datstring = "";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
datstring = String(dat);
File dataFile = SD.open("/datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(datstring);
dataFile.close();
// print to the serial port too:
Serial.println(datstring);
dat++;
}else{Serial.println("ERRO-FILE");}
delay(1000);
}
So in the archive it would be written a counting from line to line, like:
1
2
3
4
...
Buts its just keep the last number when i take de card or turn of the microcontroller. Heres the Serial monitor with verbose on:
When it got to 8 I took the card and in the archive only have the number 8.
Someone can save me???
PS: I did a txt archive with random characters and make a code who just open and close the archive, dont write nothing. And the file content was just erased, so it the open file function mabe?
Heres the code:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 5;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
File dataFile = SD.open("/datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.close();
}
}
void loop() {}
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.