Weird thing happening using char array

Hi,

My project uses an Arduino mega paired with an ethernet shield as a data-logger. The code I've written was working just fine, however after about 1 month it stopped logging new data. I've done some tests and here is what I found.

In the beginning of the code there is

char testChar[60]; sprintf(testChar, serverRoot);

which is working just fine. Somewhere later in the code I have a very similar

char filePath[60]; sprintf(filePath, serverRoot);

which gives me jibberish, the Serial.print

Serial.print("filePath: "); Serial.println(filePath); Serial.print(filePath); Serial.println(" doesn't exist.);

output looks like this:

filePath : Ø ¿ doesn't eø

After this sometimes the board just freezes, other times it resets itself. I've checked ram usage using adafruit's freeRam(), this gives 2925 before the freeze.

Could my mega's ram be corrupt? How can I check this? Or could this be another problem?

You must have something wrong in your code. What it could be is anyone's guess, you didn't want to bore us with the code so right now you're the only one with any hope of spotting the problem.

char testChar[60]; sprintf(testChar, serverRoot);

sprintf()'s second argument is a format string. What is in serverRoot? Is it really a format string? What the purpose of having a format string with nothing to format?

serverRoot is a constant char array, it serves a config variable.

Originally the line is sprintf(filePath, serverRoot, '/',currentFileName);

After this I read filePath from an SD card.

serverRoot is a constant char array, it serves a config variable.

Meaningless statement.

Please read up on sprintf().

Originally the line is sprintf(filePath, serverRoot, '/',currentFileName);

and now what is it? because

char filePath[60];
sprintf(filePath, serverRoot);

won't do much good to you...

can we know what you have in the serverRoot format string?

Sorry for being incomplete,

Originally the line is sprintf(filePath, serverRoot, '/',currentFileName);

I shortened it to sprintf(filePath, serverRoot) for testing.

I have tried

strcpy(filePath, serverRoot); strcat(filePath, "/"); strcat(filePath, currentFileName);

this gives me jibberish as well.

The file can be found on my opening post. I hope the problem can be found.

What is the content of serverRoot? Why do you ignore that question?

Post the whole code.

const char serverRoot[] = "arduino";

The second argument is a format string. "arduino" is NOT a format string.

Trying various random bits of code might eventually lead to something interesting, but, trust me, it does help to have a clearly stated goal and a well understood path toward that goal.

You might try reading the responses to your post.

Darn this 5 minute rule, I will do my best answer it clearly.

I substituted sprintf with a strcpy and strcat as I stated in my previous post, but it didn't help.

As you might see in the commented code, I tried doing

char morechar[3]; morechar[0] = 'h'; morechar[1] = 'o'; morechar[2] = '\0'

Serialprinting also gives jibberish. So I was suspecting maybe there is a problem with my board's memory? (Since freeRam stated there is still free ram available).

I hope my goal is clear.

Garbage In = Garbage Out. What do you expect?

So I was suspecting maybe there is a problem with my board's memory?

Very unlikely.

More likely is that the baud rate that the Arduino is using to send data does not match the baud rate of whatever is receiving the data.

jremington: Garbage In = Garbage Out. What do you expect?

Could you elaborate?

PaulS: Very unlikely.

More likely is that the baud rate that the Arduino is using to send data does not match the baud rate of whatever is receiving the data.

Is it possible for the baud rate to change suddenly?

Here is a full log up until the freeze

Opening port Port open þSerial Started Initializing Ehternet Initializing Server Server is at ...192.168.0.16 Initializing SD card...initialization done. Connected to Cloud-Y Touched server. GET request took (ms): 923 testChar: arduino Ntp answer ok! Connected to Cloud-Y HTTP/1.1 200 OK Date: Mon, 10 Oct 2016 16:23:19 GMT Server: Apache/2 X-Powered-By: PHP/5.6.20 Vary: User-Agent Content-Length: 21 Connection: close Content-Type: text/html; charset=UTF-8

{2016-09-30 23:04:36}Pulled last remote dateTime: 2016-09-30 23:04:36 Comparing dates: 2016-09-30 2016-10-10 1475193600 1475280000 46-10-1 Added one day, now currentFileDate: 2016-10-01 Comparing dates: 2016-10-01 2016-10-10 Error opening arduino/20161001.txt 1475280000 1475366400 46-10-2 Added one day, now currentFileDate: 2016-10-02 Comparing dates: 2016-10-02 2016-10-10 Error opening arduino/20161002.txt 1475366400 1475452800 46-10-3 Added one day, now currentFileDate: 2016-10-03 Comparing dates: 2016-10-03 2016-10-10 Error opening arduino/20161003.txt 1475452800 1475539200 46-10-4 Added one day, now currentFileDate: 2016-10-04 Comparing dates: 2016-10-04 2016-10-10 Error opening arduino/20161004.txt 1475539200 1475625600 46-10-5 Added one day, now currentFileDate: 2016-10-05 Comparing dates: 2016-10-05 2016-10-10 Error opening arduino/20161005.txt 1475625600 1475712000 46-10-6 Added one day, now currentFileDate: 2016-10-06 Comparing dates: 2016-10-06 2016-10-10 Error opening arduino/20161006.txt 1475712000 1475798400 46-10-7 Added one day, now currentFileDate: 2016-10-07 Comparing dates: 2016-10-07 2016-10-10 Error opening arduino/20161007.txt 1475798400 1475884800 46-10-8 Added one day, now currentFileDate: 2016-10-08 Comparing dates: 2016-10-08 2016-10-10 Error opening arduino/20161008.txt 1475884800 1475971200 46-10-9 Added one day, now currentFileDate: 2016-10-09 Comparing dates: 2016-10-09 2016-10-10 Error opening arduino/20161009.txt 1475971200 1476057600 46-10-10 Added one day, now currentFileDate: 2016-10-10 Comparing dates: 2016-10-10 2016-10-10 Error opening arduino/20161010.txt 1476057600 1476144000 46-10-11 Added one day, now currentFileDate: 2016-10-11 Sent messages to Cloud Y. This took (ms): 2526 Got: 3 1 Y Free ram: 2925 Free ram: 2925 Here is serverRoot + / + currentFileName: arduino/20161010.txt filePath (should be same as above): Ø ¿ doesn't eø

Here is serverRoot + / + currentFileName: arduino/20161010.txt filePath (should be same as above): Ø

The part in the parentheses is WRONG. Can't you understand that?

PaulS: The part in the parentheses is WRONG. Can't you understand that?

Yes of course, I wrote that line because filePath SHOULD be "arduino/20161010.txt", so I can read it from the SD, however it seems like no matter what I store in filePath (or any other char array), at this point of code, I only end up with jibberish.

I would not trust this in your original code

void setFileName( time_t date ) {
  ...
  ...
  char month_temp [2];
  itoa(month(date), month_temp, 10);
  ...
  ...

The month is the october, so 10. You try to dump the C-string "10" in a 2 character array, so the terminating nul character will overwrite something somewhere. Disaster waiting to happen ;)

Same for the day.

sterretje: I would not trust this in your original code

void setFileName( time_t date ) {
  ...
  ...
  char month_temp [2];
  itoa(month(date), month_temp, 10);
  ...
  ...

The month is the october, so 10. You try to dump the C-string "10" in a 2 character array, so the terminating nul character will overwrite something somewhere. Disaster waiting to happen ;)

Same for the day.

Thank you so much for taking the time to look at my code, and it seems to have fixed the problem!!