String array loses value after function is called

In my sketch I have a function named createFileName, to do just that - come up with a filename derived from a datestamp coming from the RTC on my SIM900 shield. The filename is stored in a String array named "filename." I have been testing this sketch for a few days with no errors in this area whatsoever, but I just incorporated the code below to streamline my AT commands and parsing AT responses. Since adding this new code, "filename" no longer keeps its value when I use it in the next function, named TagtoFile.

Where and how should I properly declare the array in order for it to keep its value like I intend?

Below is what I added, and whenever I call up an instance of sendATcommand, "filename" loses its value. Sketch to follow.

 int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
    {
      uint8_t x = 0, answer = 0;
      char response[100];
      unsigned long previous;
      memset(response, '\0', 100);            // initalize string
      delay(100);
      while (Sim900.available() > 0) 
      {
        Sim900.read();                        // clears the buffer
      }
      Sim900.println(ATcommand);
      x = 0;
      previous = millis();
      do 
      {
        if (Sim900.available() != 0)
        {
          response[x] = Sim900.read();
          x++;
          if (strstr(response, expected_answer) != NULL)
          {
            answer = 1;
          }
        }
      } 
      while ((answer == 0) && ((millis() - previous) < timeout));
      Serial.println(response);
      return answer;
    }

The response for that section looks like this. Note the change in the value of String filename from creatFileName to TagtoFile.

<<< SETUP COMPLETE - WAITING TO READ TAGS... >>>
Creating First File after System Init...
createFileName: Filename For Count Upload = 05062318.csv
TagtoFile: Create File - Error opening

Including the whole sketch would be a good idea.

Update - after cleaning up my code somewhat the error has changed. No longer garbage in "filename", but I come up with an SD card error. I'm beginning to think this may have something to do with memory getting full.

So when I comment out lines 233 to 238, the error is not there. When I uncomment those lines one for one and test, the error doesn't appear immediately, but only when I have three or four of those lines uncommented, does the error apprear again. Which makes me think memory issues.

Output when running normal:

<<< SETUP COMPLETE - WAITING TO READ TAGS... >>>
Creating First File after System Init...
createFileName: Filename For Count Upload = 05070003.csv
606,579,614
333,333,337
337,338,341
336,337,341
336,338,341
335,336,340

Output when too many while (sendAT...) lines are running:

<<< SETUP COMPLETE - WAITING TO READ TAGS... >>>
Creating First File after System Init...
createFileName: Filename For Count Upload = 05070008.csv
TagtoFile: Create File - Error opening 05070008.csv

When I check on the SD card the empty file has actually been created.

Sketch following.

Sketch attached.

sendat.txt (10.6 KB)

Which Arduino are you using?
What does the compiler print for sketch and global variables sizes?

Pete

I'm using an UNO.

Compiler says:

Sketch uses 24450 bytes (75%) of program storage space. Maximum is 32256 bytes.
Global variables use 1575 bytes (76%) of dynamic memory, leaving 473 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

Hint:

Low memory available, stability problems may occur.

That in combination with String (capital S) is a recipe for disaster :wink:

I see that you already have a topic about the sendATcommand and the use of the F macro so use that first. Next you can work further on getting of the String class.

E.g. filenames need to adhere to the 8.3 convention, so can easily be placed in 13 character array and you can get rid of that String variable.

Sterretjie, you're a star!

Yes I'm having trouble with the Strings. I don't know enough yet to get the sketch to work with char. I need to read up more on this. Very chuffed with the awesome solution given by pert at Conserving RAM by using F(""); - Programming Questions - Arduino Forum.