Go Down

Topic: Standalone ATmega328P-PU possible memory leak (Read 631 times) previous topic - next topic

batista742k2

Jun 12, 2020, 12:33 am Last Edit: Jun 12, 2020, 06:22 pm by CrossRoads
Hi.

I'm building a project that consists of a device that measures the gases in the air, as well as the temperature and humidity. Basically, this device shows all the data on the "OLED 128x64 I2C" and sends it via ESP8266-01, so we can consult the data in real-time on a mobile app. When hight level of gases is detected, it sends an SMS via module "SIM800L" to the user phone, which is registered in the app, and activate an alarm system too (buzzer + LED blinking).

As you can see, there are many components turned on the ATmega328P-PU, and it has some data treatment as well.

So, why am I posting this? Basically, my ATmega328P-PU is crashing, it's very rare but happens.

I suspect that, if not an electronic problem (i already uploaded a topic on that section today), it will probably be a problem related to the dynamic memory (maybe a memory leak). The sketch is using 25.702 bytes (79%) of the program storage space and 1048 bytes of the dynamic memory (51%). As I have constant treatment of data on the "void loop ()" function, maybe it's causing it to break.

The code will be attached, due to its size. I'm already saying, it has 382 lines of code, but I think that the only thing that matters is the void loop function.

Hope to hear any suggestion soon, as I'm a beginner and I'm excited with this "little project"  :D

Thanks in advance.

aarg

If you have another thread active on this subject, you really shouldn't start another one. At least, provide a link to the other one for reference. There may be information there that could save someone a lot of time.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

jremington

#2
Jun 12, 2020, 02:11 am Last Edit: Jun 12, 2020, 02:12 am by jremington
There are many potential problems that WILL cause crashes in your code.

(1) On AVR based Arduinos, use of Strings causes unpredictable memory problems and crashes.  Do not use Strings.

(2) code like this is suspect.  Use strncpy() and strncat() to avoid overflowing the output buffer.
Code: [Select]
  char phoneNumberForSend[30];
  strcpy(phoneNumberForSend, "!");
  strcat(phoneNumberForSend, phoneNumber.c_str());
  strcat(phoneNumberForSend, ";");


(3) What do you think this line does? The compiler, which produces PROGMEM constants, has no access to millis().
Code: [Select]
  const PROGMEM unsigned long currentTime = millis();

david_2018

(2) code like this is suspect.  Use strncpy() and strncat() to avoid overflowing the output buffer.
Code: [Select]
 char phoneNumberForSend[30];
  strcpy(phoneNumberForSend, "!");
  strcat(phoneNumberForSend, phoneNumber.c_str());
  strcat(phoneNumberForSend, ";");

Even worse, none of the strcpy() or strcat() statements are needed.  All phoneNumberForSend is used for is a Serial.print() two lines later, you can just as easily do this:

Code: [Select]

  Serial.print("!");
  Serial.print(phoneNumber);
  Serial.print(";");

TomGeorge

Hi,
Place 10uF  or 47uF capacitor on the 5V line and gnd on the protobaord.

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

batista742k2

If you have another thread active on this subject, you really shouldn't start another one. At least, provide a link to the other one for reference. There may be information there that could save someone a lot of time.
Even worse, none of the strcpy() or strcat() statements are needed.  All phoneNumberForSend is used for is a Serial.print() two lines later, you can just as easily do this:

Code: [Select]

  Serial.print("!");
  Serial.print(phoneNumber);
  Serial.print(";");


Hello David. I understand what you're saying, but this "Serial.prints" are not for appearing on the Serial monitor, they are being sent for an ESP8266-01, so I think I have to send it in only one String.

Thanks for your reply.

batista742k2

If you have another thread active on this subject, you really shouldn't start another one. At least, provide a link to the other one for reference. There may be information there that could save someone a lot of time.
Sure, sir, you're right. It's here: https://forum.arduino.cc/index.php?topic=689876.msg4638952#msg4638952


-----------------


There are many potential problems that WILL cause crashes in your code.

(1) On AVR based Arduinos, use of Strings causes unpredictable memory problems and crashes.  Do not use Strings.

(2) code like this is suspect.  Use strncpy() and strncat() to avoid overflowing the output buffer.
Code: [Select]
 char phoneNumberForSend[30];
  strcpy(phoneNumberForSend, "!");
  strcat(phoneNumberForSend, phoneNumber.c_str());
  strcat(phoneNumberForSend, ";");


(3) What do you think this line does? The compiler, which produces PROGMEM constants, has no access to millis().
Code: [Select]
 const PROGMEM unsigned long currentTime = millis();
I see your point. I will check that up right now.

Thanks for your reply :D

-----------------


Hi,
Place 10uF  or 47uF capacitor on the 5V line and gnd on the protobaord.

Tom.... :)
I will do it, thank you, Tom! :D

Robin2

#7
Jun 12, 2020, 11:03 am Last Edit: Jun 12, 2020, 11:04 am by Robin2
Does your Title "Standalone ATmega328P-PU possible memory leak" mean that the exact same program works properly on an Uno but fails on the standalone 328 ?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

batista742k2

Does your Title "Standalone ATmega328P-PU possible memory leak" mean that the exact same program works properly on an Uno but fails on the standalone 328 ?

...R
Indeed, yes. On the Arduino UNO board, the fails are less consistent. And as I'm using the ATmega328P-PU without the board, I will not state the Arduino board if not tested and used properly.

Robin2

Indeed, yes. On the Arduino UNO board, the fails are less consistent.
I reckon it would make the fault-finding much easier if you take the trouble to get the program working without any failures on the Uno before trying to apply it to the standalone 328.

There is no reason why a standalone 328 should behave differently from one that is attached to an Uno.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jremington

Quote
Indeed, yes. On the Arduino UNO board, the fails are less consistent.
You may have more than one problem.

CrossRoads

Have you added all the caps discussed in the other topic?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

TomGeorge

#12
Jun 13, 2020, 11:38 am Last Edit: Jun 13, 2020, 11:39 am by TomGeorge
Hi,
I think this is the OPs schematic.

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

david_2018

Hello David. I understand what you're saying, but this "Serial.prints" are not for appearing on the Serial monitor, they are being sent for an ESP8266-01, so I think I have to send it in only one String.

Thanks for your reply.
The ESP8266 will never know if you are sending as a single String or as multiple print() statements, all it sees is a stream of characters coming from a serial port, and even at 115200 baud all the print() statements will get executed before the first character is completely received, so there will be no perceptible delay in the serial data.

Your functions validateIP() and validatePN() both have a slight error, if you receive a character other than a '0' or '1' from the ESP8266, then the function will end without executing a return statement.

Would be helpful to see the code running on the ESP8266, to see the exact data format it is sending over serial.

TomGeorge

#14
Jun 13, 2020, 01:43 pm Last Edit: Jun 13, 2020, 01:55 pm by TomGeorge
Hi,
I think this is all your circuit;

You need 0.1uF capacitor across pins 7 and 8 as close to the 328.
Your gas sensors are per the data sheet.
MQ4, starting and running heater current 150mA.
MQ7, starting heater current of 150mA. running current 70mA.
How warm does your LM7805 get?
What is your DC input voltage?
C3 would be better as 0.1uF not 0.0002uF.
A 0.1uF cap between reset and gnd pin may also help.

Looking at the picture of  your project you have one 22pF cap going to gnd and the other to 5V, they both should be gnd.

Tom.... :) 
PS, If you disconnect the buzzer, does that improve performance?
Everything runs on smoke, let the smoke out, it stops running....

Go Up