Information Duplicating in SDCARD

Asked

Viewed 66 times

2

Hello

I’m studying the use of SDCARD on the Arduino and I need to record the values of the sensors in a file. TXT, but it keeps duplicating the information in the file . TXT as it goes through the FOR loop, why is it ? I’m cracking my skull all night, but I’m not finding the error, it may be something very silly, but I’m not locating it. What’s wrong ?

It’s recording like this:

Um
Dois
Um
Dois

My code:

 #include <SdFat.h>

 SdFat sdCard;
 SdFile meuArquivo;

 const int chipSelect = 4;

 void setup()
 {
 // nada a ser feito no setup
 Serial.begin(9600);

 // Inicializa o modulo SD
 if(!sdCard.begin(chipSelect,SPI_HALF_SPEED))sdCard.initErrorHalt();
 // Abre o arquivo TESTANDO.TXT
 if (!meuArquivo.open("testando.txt", O_RDWR | O_CREAT | O_AT_END))
 {
   sdCard.errorHalt("Erro na abertura do arquivo testando.txt!");
 } 

}

void loop()
{
   gravar();
 }

 void gravar()
 {
  for (int i=0; i <= 20; i++){

  Serial.println(i);
  delay(10);

  if(i == 1)
  {
    Serial.println("Um");
    meuArquivo.println("Um");
  }
  else if(i == 2)
  {
    Serial.println("dois");
    meuArquivo.println("Dois");
    meuArquivo.close();
    while(1){};
  }
   else
  {

  }
   delay(100);
 } 
}

Thank you

1 answer

1


The function loop() is performed infinitely and in function gravar() you create a variable i and assigns a value 0 every time the function gravar() is called (infinitely). Try doing so:

#include <SdFat.h>

 SdFat sdCard;
 SdFile meuArquivo;

 const int chipSelect = 4;
 int executaUmaVez = 0;
 void setup()
 {
 // nada a ser feito no setup
 Serial.begin(9600);

 // Inicializa o modulo SD
 if(!sdCard.begin(chipSelect,SPI_HALF_SPEED))sdCard.initErrorHalt();
 // Abre o arquivo TESTANDO.TXT
 if (!meuArquivo.open("testando.txt", O_RDWR | O_CREAT | O_AT_END))
 {
   sdCard.errorHalt("Erro na abertura do arquivo testando.txt!");
 } 

}

void loop()
{
    if(executaUmaVez ==0){
       gravar();
       executaUmaVez=1;
    }
 }

 void gravar()
 {
  for (int i=0; i <= 20; i++){

  Serial.println(i);
  delay(10);

  if(i == 1)
  {
    Serial.println("Um");
        meuArquivo.println("Um");
  }
  if(i == 2)
  {
    Serial.println("dois");
    meuArquivo.println("Dois");
    meuArquivo.close();
   }
   delay(100);

 } 
}
  • I tested and continues to duplicate the information recorded on the memory card, strange this, has no logic, is appearing like this ( One, Two, One, Two )

  • @abduzeedo I made a small change to the code. Try it now.

  • Now it worked. What was wrong ?

  • In the else if(i===2) there is a while, I just removed that while :)

  • Thank you! D :D

  • Avelino, very strange, I copied his code and pasted and it worked, but after using a few times, it starts to duplicate again, because it will be ?

Show 2 more comments

Browser other questions tagged

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