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
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
@abduzeedo I made a small change to the code. Try it now.
– Avelino
Now it worked. What was wrong ?
– abduzeedo
In the
else if(i===2)there is a while, I just removed that while :)– Avelino
Thank you! D :D
– Avelino
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 ?
– abduzeedo
Let’s go continue this discussion in chat.
– abduzeedo