Program stops

Hi all, for my Arduino with atmega328 I have a program with compilation size of about 18kB and it runs OK. When I add about 20x Serial.print lines into the code (a new function), the code has a size about 19,5KB of 32kB, it is uploaded into Ardunio and the program stops inside the setup() even before the new function is called.

What problem kind I am facing up? is there a problem in variable maximum number or memory use? I don't use progmem.

Thank you for ideas or experience.

In theory, there is no limit on the number of variables you can have. On the other hand, you are probably running out of memory.

The Arduino has 3 kinds of memory - flash, SRAM, and EEPROM. Flash memory is where your executable code goes (the 19.5K of 32K (minus that required for the bootloader)). SRAM is where constants are stored (the strings in print/println statements, for instance). EEPROM is available for the Arduino to read from/write to, but is typically not used. It is where an application could store data that needs to be persisted even when the Arduino is reset.

You are probably running out of SRAM. Using progmem, some constants can be moved into program space, reducing the amount of SRAM required.

Thank you for your reply! Your explanation looks logical and helpful. I'll test it.

In One post, the following is described: "Remember that anything you store in your arrays will be static, it can not be dynamically updated while your program is running."

It means the only the values that doesn't change during the program can be stored in PROGMEM...?

It means the only the values that doesn't change during the program can be stored in PROGMEM...?

Yes. But, if your code has:

Serial.print("The temperature is: ");

The string "The temperature is: " is a non-changeable constant. That string could be placed in progmem, and read from there for printing.

So does the following helps?

PROGMEM  prog_uchar printMessage[] = {"The temperature is: "};

and then:

Serial.print(printMessage); Serial.println(somevalue);

The first part is correct. The second part is no, though. The Serial.print() method has no overloads (unfortunately) that deal with progmem. You need to get the data from progmem in order to be able to print it. See for examples of how to do that.

Thank you, its up to me to learn now :-)

One more question: Is there a possibility to look at the memory size used by my programm to see separately Flash, SRAM and EEPROM?

The flash size used is the value that is reported when you compile/upload.

The compiler has no idea whether or not you are using EEPROM, so it can't tell you how much of that you are using, or where.

SRAM changes as the code executes. As functions are called, data is added to SRAM. When they end, data is removed. A static, at compile time, value is not going to be too useful, so it is not reported.

There are ways to determine, at run time, at some point in the code, how much SRAM is being used. Search the forum for "free memory".