Sketch size to big?


I've got some trouble with my arduino... My sketch size was about 13KB when my arduino (deumillanove/atmega168) started acting 'weird'.. I think it's something like a stack-overflow, causing it to return to wrong addresses... So i changed it to another deumillanove, which is atmega368 based, and everything worked fine again... But after my sketch has grown a little bit -it is now about 16KB- the 'crazyness' started again! The first part of the sketch worked just fine, but some lines of code later it's going crazy again.. so I added some 'Serial.println(...)''s to see how the code is flowing.. but since then it didn't even boot!.. The led at pin 13 is only flashing once, then.. nothing.. When i remove some code with a lot of hardcoded strings, it's all working again, (that part of the code is not executed right after the boot or something.. it's a function that will be called on a press of a button)...

This doesn't sound like a stack overflow, right? (Because changing 'non-boot code' affects the boot)

Can anyone tell me what's happening? :-/

Thanks in advance, -Maurice-

It does sound like a stack overflow. It’s The lack of RAM, not the amount of program memory that is causing the problem. So you need to reduce RAM usage rather then lines of code. Strings can consume a lot of RAM so read up on progmem in the Arduino reference to see how you can use program memory to store these instead of RAM.


Everything is working again!

In case you’re interested, i added this code to the top of my sketch, since all my functions were designed to work with ‘RAM-strings’:

#include <avr/pgmspace.h> 
#define str(x) pstrtoram(PSTR(x))

#define ramstr_buffer_size 5
#define ramstr_buffer_length 21

char ramstr_buffer[ramstr_buffer_size][ramstr_buffer_length];

char * pstrtoram(const char * ps){
  static int current_buffer = 0;
  int i = 0;
  char * buffer = ramstr_buffer[current_buffer];
  while (buffer[i] = pgm_read_byte(&ps[i])) i++;
  current_buffer %= ramstr_buffer_size;
  return buffer;

so whenever i use str("…") in my code it will use the PSTR macro to store it like ‘PROGMEM’ does, and calls pstrtoram to load it (temporarily) into RAM.

Again, thanks for your help!