Weird corruption of data, trying to find the cause.

I am working on an Arduino project and after making some updates yesterday, I noticed that I have a serious bug when part of my data gets corrupted and this corruption also affects the output to the serial monitor. Based on my previous experience, I made an assumption that this is probably happening because I am running out of SRAM, but after wasting nearly 12 hours, I am yet to fix the issue. I am using Arduino Uno and when uploading the sketch, SRAM usage seems to be about 24%, but I think this does not include all the memory that I use in runtime. Just to demonstrate the issue, take a look at this serial output.(Note that the very first 3 lines are just produced by the Visual Studio IDE that I am using and are not part of my project)

Opening port
Port open
Received message = 1
--------------------- Second:1
--------------------- Third:1
turning ON the system
Reeived mf-
--------------------- Second:he s-
--------------------- dirTh0
turning OFF the system

As you can see, After receiving the message, the 2nd and 3rd lines display the received value correctly. But after that, I get some corruption happening and it not only affects my variables but the serial output as well.

Here is the snippet of code that is responsible for producing this output.

if (I2CInterrupt == true)
 I2CInterrupt = false;

 String m_I2CMessage = "";
 while (Wire.available())
 m_I2CMessage += (char);

 Serial.Print(F("Received message = "));

 if (m_I2CMessage.length() == 9)
 Serial.print(F("--------------------- First:"));
 int *rgbValues = m_MessageProcessor->ProcessI2CMessage_CustomLights(m_LightsMode, m_I2CMessage);

 Serial.print(F("--------------------- Second:"));

 m_MessageProcessor->ProcessI2CMessage(m_SystemPowerStatus, m_LightsMode, m_I2CMessage.toInt());


 if (m_SystemPowerStatus == true)
 if (m_LightsMode == FADE_LIGHTS)
 else if (m_LightsMode == FADE_SPLIT_LIGHTS)

I understand this is not the full program but I really have no way of showing all the accompanying functions, since it is too long and I am sure none of you wants to spend an hour reading my whole sketch :slight_smile:

So my question is, do you see any mistakes in this code that can cause this issue? Also, if you agree with my deduction that this indeed might be a memory related problem, could you please provide a way for me to actually profile Arduino memory and to see if I am running out of SRAM?

You've left out far too much to be sure what is going on other than you are using the String class. Unless you are very careful, it can corrupt ram in just the way you are seeing.
"I2CInterrupt" suggests that this code might be in an interrupt routine, in which case that is very bad. "all the memory that I use in runtime" suggests that the code you didn't post might contain array declarations or something else that consumes a lot of ram. But that is just guesswork based on far too little information.
Post your code.


I am using interrupts to register when the I2C message is received from the main Arduino. But all that interrupt function does is setting the boolean to true. Just out of curiosity, why are you saying that the interrupt routine is bad? By the way, when testing this code, I made sure that the interrupt does not arrive in the middle of anything important. The only reason I did not want to post my code is that it is broken down into numerous files, but I am attaching the project to my comment right now in case if anyone wants to look through the entire code. This sketch is responsible for driving 183 WS2812B LEDs, and if my calculations are correct, the LEDs themselves should only take close to 500 bytes of memory. (48.1 KB)

I didn't say the interrupt routine is bad. For all I knew, all of that snippet you posted was part of your interrupt routine, in which case that is bad.
But now that you've posted the code, I see that it isn't, in which case all is OK - at least on that score.
At the moment, my only suggestion is to get rid of the String class. You seem to use it only in one place and I'm not sure that in this case it is causing a problem. But it might eventually bite you anyway, so get rid of it now.


I see, by the way, I never have seen before corruption caused by String. Could you provide any links that I can read to learn more about this issue?

There are plenty; this one gives a reasonable explanation (in my opinion): The Evils of Arduino Strings

You can use Available memory to get an idea about memory usage at run time.

And post your full code. Dynamic memory allocation (also used by the String class), writing outside the boundaries of an array and recursive function calls are some things that cause problems.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.


sterretje I did provide my full code as an attached zip file in one of my comments. Also, thank you, everyone, for informing me about the problems that Strings can cause. I spent the last 6 months working on a c++ project and was not expecting to have any String related problems in Arduino environment as well. I will eliminate the Strings and will switch to c strings then. Thanks.

Sorry, I did not see that.

Be aware that the IDE only calculates used RAM based on global and static variables. The Neopixel library makes use of dynamic memory allocation and hence that does not reflect in the usage calculation by the IDE.

Ok, so I eliminated all the String usage in this sketch and replaced it with c strings and it works like a charm. Thank you, everyone. I am still boggled by the errors that I was getting tho. I have a lot of Strings in my other sketch that sends I2C messages to this one and I have been working on that sketch for a few months now without any problems. Anyway, I also manually calculated the ram usage (at least as accurate as it is possible to do by hand :slight_smile: ) and it seems like I utilize only about 65% or so of SRAM so now I am convinced that the issues that I was having were indeed caused by the usage of Strings.