Erratic failures in apps based on minor changes in code

Getting a bit frustrated with developing for Arduino. My programs seem to go defunct for seemengly unexplainable reasons.

Some examples:

  • There is a global float array. If this is initialized to size, say 200, Ethernet.begin with dhcp fails. When size adjusted to 50, dhcp works.
  • I have a function for checking for c-style string (char*) equality. I have a Serial.println("string_compare") in the beginning of the function for debugging purposes. dhcp fails. When comment Serial.println out, dhcp works. Even if I change the println argument from "string_compare" to "string", dhcp starts working.
  • Adding regular code (that compiles ok) to a function causes entire application fail/freeze before the function is even executed.

Board is Arduino uno R3, size of the binary scetch a bit over 15000.

I am the only one experiencing this kind of random behaviour? It really eats my motivation for starting anything more extensive on the platform.

Check the RAM requirements. You have got only 2kbytes handy…

I figured from the compiler message
"Binary sketch size: 15 350 (of a 32 256 byt maximum)"
that I am well within limits!

OK, so the data lives in RAM of 2k, and code in Flash of 32k? And presumably the stock libraries (Ethernet, SPI, ...) take their share of the SRAM for their buffers and variables... Welcome to the world of microcontrollers, I guess...

Thanks for your prompt reply!

I assume you are using the standard Arduino IDE. I might be worthe while to switch to one of the more extensive IDE's like ECLIPSE of Atmels IDE (v6.x). I use Eclipse and it shows exactly how much RAM is used and what is available based on type of board used.

mvuo:
It really eats my motivation for starting anything more extensive on the platform.

It's nothing to do with the platform. 200 x 4 bytes is 800 bytes. OK, that is within the available memory of 2048, but don't get too excited if you have other arrays.

If you buy a 4-seat car and try to carry 16 people it won't work. Not the car's fault. You can do a lot with the platform, but you need to be aware of the limitations.

I have a function for checking for c-style string (char*) equality. I have a Serial.println("string_compare") in the beginning of the function for debugging purposes. dhcp fails. When comment Serial.println out, dhcp works. Even if I change the println argument from "string_compare" to "string", dhcp starts working.

This is all pretty meaningless without posting code.

How to use this forum

My mistake, I forgot that on these microcontrollers variables are allocated in limited SRAM, whereas code is stored in flash.

Now I can happily continue my project by rethinking and optimizing memory usage by sharing buffer space and index variables between functions, shortening string constants, etc...

Life is good again and Arduino rocks! 8)

Thanks for your time!

I have a Serial.println("string_compare")

You would save more RAM by writing it Serial.println(F("string_compare"))

In "Arduino Land" forgetting about the limited SRAM (Due excepted) is a form of Alzheimer's...

A memory 'free' utility:

int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

Usage:

  Serial.print(F("  Free RAM = ")); Serial.println(freeRam()); Serial.println();

Ray