Check memory FLASH/ PROGMEM

how to check size memory FLASH or PROGMEM on my arduino mega ? because my program always stuck after my program saved into memory Flash

Measuring memory usage: Measuring Memory Usage | Memories of an Arduino | Adafruit Learning System

If it turns out that it is not a memory problem post your code, maybe we can find why it is not working properly and help you to fix the code.

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

sorry I mean memory flash or progmem

The compiler will tell that at the end. It is okay to fill the Flash up to 100%.

The Arduino Mega 2560 has something special with PROGMEM, it is the only Arduino board that can have multiple PROGMEM segments and the total can be more than 64kbyte. However, most functions can not deal with that.

Please show your sketch.

You did not even take the time to look at the page that I linked. That page tells you how to find out. There is a lot of good information in that page.

Mega has 256 KB of Flash Memory of which 8 KB used by bootloader.

If you want to know how much Flash memory is used by your program, that is shown by the IDE after compiling.


this is my sketch .. my program always stuck when running after 1or 3 hours ..

this is my sketch .. my program always stuck when running after 1or 3 hours .. Is there a possibility that it is caused by a full flash memory?

It is unlikely that it is full flash.

It is more likely that you are running out of SRAM* due to using the String class or you have an array bounds violation. That is the best that I can guess without seeing the code. Post the code.

//* actually, running out of heap memory.

My program saved in flash memory RAM only used 23%

Let's calculate the size of PROGMEM.

29 * 18 bytes and that 8 times = 4176 bytes
That is only 4kbytes and you can go up to 64kbytes before the trouble begins.

The resetFunc() is dangerous. Every advanced Arduino programmer tries to avoid that. So if you see that in an example, then you know that it is not written by a good Arduino programmer.

I have not investigated the rest of the sketch.

thankyou ^^

@ginasoniah, why did you delete the post with the code? You no longer need help?

If you no longer need help because your problem is solved, please mark the topic as such by clicking the solution button under the most useful post (Koepel's one)

actually my program still cracks or stuck but I can try solution from @Koepel about resetFunc

Thanks for restoring the code.

Two comments

  1. A Mega has four hardware serial ports; one used for the communication with the PC and 3 spare ones. Why do you use SoftwareSerial?
  2. Your problem can very well be caused by the extensive (and sometimes incorrect) use of String (capital S).

Some pointers below regarding the use of String objects.

A waste e.g. in printwaktu. You get a number (e.g. ts.date), convert it to a String object and next use the underlaying text buffer.
You can just as well print the number directly.

void printwaktu()
{
  Time ts = rtc.getTime();
  Serial.print(ts.date);
  Serial.write("/");
  Serial.print(ts.mon);
  Serial.write("/");
  Serial.print(ts.year);
  Serial.write(" ");
  Serial.print(ts.hour);
  Serial.write(":");
  Serial.print(ts.min);
  Serial.write(":");
  Serial.print(ts.sec);
  Serial.write(" # ");
}

There are moe places where you convert an integer to a String object before printing it; just print the number directly.

Every time that you pass a String object to a function (e.g. to parseCommand(String com)), you place a copy of the String on the stack which wastes memory. Change the functions to use a reference and it will only use a pointer to the object.

void parseCommand(String &com)
{

Use of + (plus) to concatenate creates new Strings under the hood which wastes memory. Use either concat or += (as you sometimes already do).

Those are just some hints to reduce the risks related to String objects. Also check the first link in post #2 by @groundFungus. Use it liberaly all over the show to see if you're loosing RAM.

Other comments
Don't use single letter variables as global variables; find better names. It can be a mission to find them. E.g.

//Var RTC
DS3231 rtc(SDA, SCL);  // inisialisasi penggunaan i2c
Time t;
//END Var RTC

Change t to something else, e.g. waktu or theTime. Same applies to the global variable i.

thank you so much I try reduce String what you explain above.

but if I just print the number directly this is the result

my program not read right time, so I must convert integer to String

is it possible because I ran out of memory on "get" and "put" when saving EEPROM causing my program suddenly stuck ?

OK, in that case I might be mistaken about the types that are in ts. Can't check it out now, will try later.

You can write an extremely simple sketch (for testing purposes) and use printwaktu in there and see what it displays. If that displays correctly, you have some serious problems at hand.

I did run the below simple test sketch

#include <DS3231.h>

//Var RTC
DS3231 rtc(SDA, SCL);  // inisialisasi penggunaan i2c


void setup()
{
  Serial.begin(115200);
  rtc.begin();


  Serial.println(F("Hello"));

  printwaktu();
}

void printwaktu()
{
  Time ts = rtc.getTime();
  Serial.print(ts.date);
  Serial.write("/");
  Serial.print(ts.mon);
  Serial.write("/");
  Serial.print(ts.year);
  Serial.write(" ");
  Serial.print(ts.hour);
  Serial.write(":");
  Serial.print(ts.min);
  Serial.write(":");
  Serial.print(ts.sec);
  Serial.write(" # ");
}

void loop()
{
  // put your main code here, to run repeatedly:
}

The result

Hello
27/9/2022 14:45:56 # 

So the modification that I suggested works. Test it on your setup, it should behave the same.

After a quick glance, I doubt it. There does not seem to be anything obviously wrong with it.

when I try to reduce String my program not stuck again thank you guys