SD card stops working after 11 loops

Hi, I've coded some stuff. Am quite new to Arduino and c++ and my code stops working after looping through void loop 11 times. 11 times every time. What the hell is going wrong here?

// dont question the chosen libraries
#include <OneWire.h> 
#include <DallasTemperature.h> 
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
File sdfile;
#include "RTClib.h"
RTC_DS1307 rtc;


void setup(void){
    while(!Serial);                                         
    Serial.begin(9600);
    Serial.println("starting sd card");
    if(!rtc.begin()){                                  
        Serial.println("rtc not found");
        while(1);
    } else {
        Serial.println("rtc found");      
    }
    if (!SD.begin(10)) {
        Serial.println("sd card did not start");
        while(1);
    }
    if(!rtc.isrunning()){                      
        Serial.println("rtc does not work.");
        rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 
    }
    Serial.println("sd card started.");
    sdfile = SD.open("test3.txt", FILE_WRITE);
    if(sdfile){
        Serial.println("opening file");
    } else {
      Serial.println("file not found");
    }
    
        sdfile.close(); //idk man
} 

void loop(void){ 
    sdfile = SD.open("test3.txt", FILE_WRITE);
  if(sdfile){
    printTime();    
         Serial.println("sd card found ");
  } else {
    Serial.println("sd card not found");
  }
  delay(950); // changing delay does nothing, still 11 times
  Serial.println("void loop() completed");
} 

void printTime(){ 
    sdfile = SD.open("test3.txt", FILE_WRITE);
    DateTime now = rtc.now();                     
    sdfile.print(now.day(), DEC);   
    sdfile.print('-');                             
    sdfile.print(now.month(), DEC);
    sdfile.print('-');
    sdfile.print(now.year(), DEC);
    sdfile.print(";");
    sdfile.print(now.hour(), DEC);
    sdfile.print(':');              
    sdfile.print(now.minute(), DEC);
    sdfile.print(':');
    sdfile.print(now.second(), DEC);
    sdfile.print(";");
    sdfile.println();
        sdfile.close(); // at first I did not close the file everytime but that didnt work either
}

*/

thanks

You open the file more times than you close it. That may cause issues?

I changed loop() to this

void loop(void){ 
    sdfile = SD.open("test3.txt", FILE_WRITE);
  if(sdfile){
    printTime();    
         Serial.println("sd card found ");
  } else {
    Serial.println("sd card not found");
  }
  delay(950); // changing delay does nothing, still 11 times
  Serial.println("void loop() completed");
sdfile.close();
} 

Now the last line has sdfile.close(), but still the same effect occurs

You don't need to open the file inside the loop at all (unless something else accesses the card and the file could be deleted between loops and you need to check it exists each time?)

Try this:

void loop(void){ 
  if(sdfile){
    printTime();    
         Serial.println("sd card found ");
  } else {
    Serial.println("sd card not found");
  }
  delay(950); // changing delay does nothing, still 11 times
  Serial.println("void loop() completed");
}

Removed the 2 lines, Serial outputs "sd card not found" everytime now. What the hell am i doing

Ah yeah, I didn't spot that the file had been closed again in setup so it won't be open at the point in the loop with the IF statement.

You'll need to either open it at the start of the loop and check it exists then write to it inside printTime without opening it again inside that function
or
move the check that the file exists into the printTime function to check it after the file is opened.

This would be the option with the file opened in the loop:

// dont question the chosen libraries
#include <OneWire.h> 
#include <DallasTemperature.h> 
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
File sdfile;
#include "RTClib.h"
RTC_DS1307 rtc;


void setup(void){
    while(!Serial);                                         
    Serial.begin(9600);
    Serial.println("starting sd card");
    if(!rtc.begin()){                                  
        Serial.println("rtc not found");
        while(1);
    } else {
        Serial.println("rtc found");      
    }
    if (!SD.begin(10)) {
        Serial.println("sd card did not start");
        while(1);
    }
    if(!rtc.isrunning()){                      
        Serial.println("rtc does not work.");
        rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 
    }
    Serial.println("sd card started.");
    sdfile = SD.open("test3.txt", FILE_WRITE);
    if(sdfile){
        Serial.println("opening file");
    } else {
      Serial.println("file not found");
    }
    
        sdfile.close(); //idk man
} 

void loop(void){ 
    sdfile = SD.open("test3.txt", FILE_WRITE);
  if(sdfile){
    printTime();    
         Serial.println("sd card found ");
  } else {
    Serial.println("sd card not found");
  }
sdfile.close();
  delay(950); // changing delay does nothing, still 11 times
  Serial.println("void loop() completed");
  
} 

void printTime(){ 
    DateTime now = rtc.now();                     
    sdfile.print(now.day(), DEC);   
    sdfile.print('-');                             
    sdfile.print(now.month(), DEC);
    sdfile.print('-');
    sdfile.print(now.year(), DEC);
    sdfile.print(";");
    sdfile.print(now.hour(), DEC);
    sdfile.print(':');              
    sdfile.print(now.minute(), DEC);
    sdfile.print(':');
    sdfile.print(now.second(), DEC);
    sdfile.print(";");
    sdfile.println();
}

That works, thank you.

I have two remarks though:

  1. Why the hell did it even stop after 11 times? And why did it even stop, so I can avoid it in the future?
  2. Why is the rtc off by 2.5 minutes now?

A similar thing would happen on your computer - if you keep opening the same file without closing it you'll eventually get an error. A computer is more powerful and would take longer before things break down though.
It could have run out of memory or it might be the SD card or SD card controller can't handle that many open connections to a file.

I'm not sure. Was it correct before? You might need to configure it with the right time when first using it?
Your code will append to the end of the file so the latest entries will be at the bottom.

thanks. It did work before, I fixed it by changing this

if(!rtc.isrunning()){                      
        Serial.println("rtc does not work.");
        rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 
}

to this

if(!rtc.isrunning()){                      
        Serial.println("rtc does not work.");    
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 

Works perfectly now. Now i'll have to integrate 5 code files into one.

That's surprising. Don't forget that DATE and time are compile time constants. Unless you're reloading the code every time you run, the clock will get further and further out of sync.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.