In my current project I'm using an Arduino Nano, an RTC-Module, SD-Module and an OLED-Display to log time-values. I'm currently trying to implement following function:

    char *onString   = (char*)malloc(28 * sizeof(char)); // "ON:  DD/MM/YYYY - HH:MM:SS" = char[27] + '\0'
    char *offString  = (char*)malloc(28 * sizeof(char)); // "OFF: DD/MM/YYYY - HH:MM:SS" = char[27] + '\0'
    void createLog(){
      Serial.println("----- Creating Log -----\n");
      uint32_t temp_position = 0;
      uint32_t startTime = 0;
      uint32_t endTime = 0;
      bool breakLoop = false;

        tempfile = SD.open("tempfile.dat", FILE_READ);  
          tempfile.read((uint8_t *)&startTime, sizeof(startTime));
          tempfile.read((uint8_t *)&endTime, sizeof(endTime));
          temp_position = tempfile.position();
          Serial.print("Reading from tempfile... "); 
            breakLoop = true; // Check if last data set of tempfile
            Serial.println("Last Data set of tempfile!");
        } else {
          Serial.println("Error opening tempfile.dat!");
          breakLoop = true; // Exit while loop!

        DateTime startT((DateTime(startTime)));
        DateTime endT((DateTime(endTime)));
        TimeSpan diffT = endT - startT; 

        snprintf(onString,  28, "ON:  %02d.%02d.%04d / %02d:%02d:%02d", startT.day(), startT.month(), startT.year(), startT.hour(), startT.minute(), startT.second());
         * Uncomment the following line and communication won't work anymore...    
        // snprintf(offString, 28, "OFF: %02d.%02d.%04d / %02d:%02d:%02d", endT.day(),   endT.month(),   endT.year(),   endT.hour(),   endT.minute(),   endT.second());

        // Serial.print(offString);

        //* Opening Logfile (human readable) and writing the strings into it...
      Serial.println("\n----- Finished creating log -----");

The function is supposed to read (unix)time values from a file on the SD ("start" and "end"-time), and convert them into strings. This works perfectly well with the snprintf() function. But when I uncomment the second snprintf()-call (for the "offString"), the arduino fails to communicate with the SD Card. Even before calling the whole createLog()-function in the loop. (My first try to log something on the SD-Card is in the setup-block, and it fails to open the "tempfile.dat"-file).

So my guess is, that the problem lies somewhere in the memory-allocation or compiling process that I'm completely unfamiliar with. Eventhough I've read about some problems with the s(n)printf functions for Arduino, the problems were quite different to mine (too many arguments after the string etc.)

Any suggestions are appreciated!

First Suggestion from @Michel Keijzers: Changing lengths of strings to 64 unfortunately changed nothing

Second suggestion from @jsotola: Exchange commandlines. Now the "offString"-Line alone works poperly, but the two snprintf's together still do not work.

P.S. If needed I will provide my full code.

  • Did you try to change all 28's to something bigger e.g. 64's? I think the %02 and %04 should never be bigger than 2 resp 4 characters, but checking is better than assuming. – Michel Keijzers Oct 6 '19 at 19:48
  • what happens if you exchange the two snprintf command lines? – jsotola Oct 6 '19 at 20:53
  • Thanks to both of you!. I added the answers to your suggestions to the Questions. – Wulle Oct 7 '19 at 5:43
  • don't you run out of memory? – Juraj Oct 7 '19 at 5:45
  • 1
    It looks like a memory corruption problem. The problem with these is that the lines of code that cause the problem are not the ones that make it visible. Thus the bug could well be in some part of the program you are not showing. Ideally, you should try to write a minimal testable example that shows your issue. – Edgar Bonet Oct 7 '19 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.