Hello
I'm studying the use of SDCARD in Arduino and I need to write the values of the sensors into a .TXT file, but it gets duplicate information in the .TXT file as it goes through the FOR loop, why? I'm cracking my ass all night, but I'm not finding the error, it may be a silly thing, but I'm not locating. What is wrong?
You're 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