arduino freezes after a few hours


I have a problem with my project, arduino mega 2560 based sprinkler system.
My project uses:
-Arduino Mega 2560
-RTC DS1307
-SIM800L v2
-humidity sensors connected to analog pins
-Impulse power supply 5V 2A

Everything worked fine until I've installed it in my summer house.
After a few hours it freezes and doesn't react to anything, the LCD screen doesn't refresh parameters. I have already excluded ram problems, first of all, it is able to work few days in my workshop, otherwise I implemended freeRam function and there is always 5447 bytes free. That's why I'm not including code now.

Rather, it seems to me there is something wrong with power, or same noises. I know, there are power fluctuations where my summer house is, but my power supply adapter works in a range 110-230VAC, even if the power would by lower arduino should turn off, shouldn't it?
I have additional capacitors near the arduino VCC pin (100uF and ceramic 100nF).
Is it possible that the problem is a megnetic field from others electical devices like fridge, TV or commercial GSM alarm system? (I've tried moving an arduino with no results). It happens also with disconnected sensors (I thought a noise from ground)
Any ideas what can cause such problems?

Don't think anyone will be much help unless you show a circuit of what you have and your code.

My wild guess is the use of the String class.

But you need to post the program.


I didn't want to paste my code because it's awful...

Please remember, the problem occurs in one place (my summer house), in my workshop is fine.

How can I post my code? I have an error:

The message has the following error or errors that must be corrected before continuing:
The message exceeds the maximum allowed length (9000 characters).

That's true I use String class but I also use string.reserve(); function and it works, as I said I have 5kB free.

Check out this topic: freezing issue. The TLDR version is that a fridge on the same power circuit as the arduino was causing problems, likely when the compressor kicked in. A decoupling capacitor took care of it. Your symptoms sound rather similar.

Get rid of the Strings though. It may indeed be that they're not biting you now, but unless this is your last Arduino endeavor ever, they will eventually.

If the code is too long to include in your Reply then add the .ino file as an attachment.

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. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).


Is your summer house a lot warmer than your workshop?
If it works okay in the workshop but not in the summer house then the problem could be heat and/or power problems.