0
I have a Kingston 4GB micro SD card and a Datalogger code that deletes the successful file and creates a new one with the new values of the variables. Follow the code below:
void back_var() {
SD.remove("/backup/BACKUP.txt");
char var_up[] = "%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;";
sprintf(
variaveis, //Nome da String a ser criada
var_up, //Char utilizado para armazenar a String
valor1, //Informações
valor2,
valor3,
valor4,
valor5,
valor6,
valor7,
valor8,
valor9,
valor10,
valor11,
valor11,
valor12,
valor13,
valor14,
valor15
);
Serial.println("Print das variáveis:");
Serial.println(variaveis);
Serial.println(botoeira_entrada_S15);
SD.begin (4);
backup = SD.open("/backup/BACKUP.txt", FILE_WRITE);
if (backup) {
Serial.println("Inserindo variáveis no SD...");
backup.print(variaveis);
backup.print('|');
Serial.println("Variáveis inseridas no SD!");
backup.close();
}
else {
Serial.println("Erro ao abrir arquivo de Update");
}
}
The problem is that after certain time the txt file gets corrupted and create other unreadable files. Why does this happen? And how to solve it? Follow below created unreadable files:
have you tested the code with other cards? What is the result?
– Augusto Vasques
not yet tested with another card @Augustovasques, you think it might be the card?
– Sarah Tavares
Test other cards and IF POSSIBLE, do not want to be accused of disappearing with your data, format (complete formatting) that your 4GB Kingston SD.
– Augusto Vasques
I believe the mistake may be in this
SD.begin(4)
, you should call it only on setup(). Another thing, you should test if it was possible to initialize the card as follows:if (!SD.begin(4)) {
 Serial.println("Falha na inicialização!");
 while (1);
 }
– Esdras Xavier
edited my code with the photo of unreadable files that are created @Augustovasques
– Sarah Tavares
@Esdrasxavier the test to check the card boot is done... I edited my code with the image of files created inside the card
– Sarah Tavares
@Esdrasxavier, it is not possible to call it only in setup() because it is also used the communication with the internet... has another way?
– Sarah Tavares
In the code you shared you do not check whether it was initialized correctly, you only check whether it is opened or not. To check the startup you must use
if (!SD.begin(4)) Serial.println("Falha na inicialização!");
. Something else on the top has aSD.remove()
, before the SD.Begin().– Esdras Xavier
@Sarahtavares What do you mean, communication with the Internet is also used. ?
– Esdras Xavier
@Esdrasxavier send the data by SPI to a database
– Sarah Tavares
Got it. In this case then at the end of your function try to put a
SD.end()
, to end the connection, it would be like a "Safely remove"– Esdras Xavier
@Esdrasxavier I’ll try, thank you! SD.remove() is before SD.Save() even, I’ll change here, thanks! Anything else in my code? I’ll try again
– Sarah Tavares
Another thing, all these variables of yours are like double?
– Esdras Xavier
@Esdrasxavier the variable is int... could be this?
– Sarah Tavares
changed the variable to double now @Esdrasxavier.... the int variable may have influenced on the corrupted sd card for some reason?
– Sarah Tavares
In this case you are using sprintf, what I would do is to write variable by variable, maybe it may be that this corrupting the memory of the Arduino :p, but I do not guarantee, makes a cololocar test:
backup.print(variavel); backup.print('|'); backup.print(variavel2);
, and see if it gives the same problem. And you can keep the int– Esdras Xavier
thanks, I’ll do it!
– Sarah Tavares