Why does my SD card corrupt in the Afghan rescue?

Asked

Viewed 137 times

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: inserir a descrição da imagem aqui

  • have you tested the code with other cards? What is the result?

  • 1

    not yet tested with another card @Augustovasques, you think it might be the card?

  • 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.

  • 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);
 }

  • edited my code with the photo of unreadable files that are created @Augustovasques

  • @Esdrasxavier the test to check the card boot is done... I edited my code with the image of files created inside the card

  • @Esdrasxavier, it is not possible to call it only in setup() because it is also used the communication with the internet... has another way?

  • 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 a SD.remove(), before the SD.Begin().

  • @Sarahtavares What do you mean, communication with the Internet is also used. ?

  • @Esdrasxavier send the data by SPI to a database

  • 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"

  • @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

  • Another thing, all these variables of yours are like double?

  • @Esdrasxavier the variable is int... could be this?

  • changed the variable to double now @Esdrasxavier.... the int variable may have influenced on the corrupted sd card for some reason?

  • 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

  • thanks, I’ll do it!

Show 12 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.