Serial.print issues

I am seeing something peculiar with Serial.print and am wondering if it is some sort
of buffer overrun' framing error or what...

Here is a simplified version of my code that exhibits the same symptom:

int bufferMatrix[50][50];
int xpos, ypos;

void setup() 
{
  Serial.begin(9600);
  Serial.println("****  Setup is done *****  ");
}

void loop() 
{
  Serial.println("****  loop has started *****  ");

   for (ypos = 0; ypos < 50; ypos++)
   {  
      for (xpos = 0; xpos < 50; xpos++)
      {  
//         bufferMatrix[xpos][ypos] = 0;
      }
   }
}

Here is the output from the code above:

**** Setup is done *****
**** loop has started *****
**** loop has started *****
**** loop has started *****
**** loop has started *****
**** loop has started *****
**** loop has started *****
**** loop has started *****
**** loop has started *****

If I remove the comment from the line:

// bufferMatrix[xpos][ypos] = 0;

then I get no output from my Serial.print statements.
Why is that and how can I fix this?

Thanks

Is that an Uno? If so you are out of SRAM.
50 * 50 * 2 = 5000 bytes.
An Uno has 2000 available.

Thanks - Yes it is a uno...

I slapped this together to show what I was seeing with the code that I am using as
it may be to lengthy to post.

In that code I am using a 8 x 22 buffer but find that it is doing the same thing (running out of Sram)

What else uses Sram, is it constants, variable, and the code too? Also, any tips on things to do to compact
the Sram use? I would like to consider this before upgrading the hardware if possible.

thanks again

In that code I am using a 8 x 22 buffer but find that it is doing the same thing (running out of Sram)

Not in the code you posted. This is a 50 x 50 array of integers (2 bytes each).

int bufferMatrix[50][50];

I've run into the same problem when using a matrix for a few hundred samples with floats. Running out of SRAM makes the Uno do funny things, sometimes completely freezing, sometimes running half a loop, etc... Time to get creative :slight_smile:

SurferTim:
50 * 50 * 2 = 5000 bytes.

Tim, why the "*2" ? I get the 50x50 matrix part.

oh, 2 bytes for int. got it.

Is there a way to tell how much sram is being used when code is uploaded to an arduino?

I include this function code to any sketch I want to check the SRAM.

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

Then call it something like this. I call it where I think the most memory is allocated, like in subroutines that have arrays, etc.

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

It does not show 0 when you run out of memory. It will show an unrealistic amount of SRAM remaining. If I run my Mega out of memory, it will return more free SRAM than my Mega has.

Thanks Surfer Tim-

I tried this on my Uno code and it reads
Free SRAM = -1248

Does this mean I have 1248 bytes left?

That number should be positive, probably somewhere between about 1000 to 100, depending on the remaining SRAM. That would normally indicate you are out of SRAM.

Write a test sketch. I used something like this to get an idea of how much SRAM memory the device has when it starts with an empty sketch.

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

void setup() {
  Serial.begin(9600);
  Serial.print(F("Free SRAM = "));
  Serial.println(freeRam());
}

void loop() {
}

I tweaked a few things and got it up to 735 at one point in the code
but then at other points of the code it doesn't work (must be running out of memory at those areas I assume..)

I notice no difference removing constants but arrays seem to eat up some sram.

What types of commands/ data structures tend to chew this sram up so fast and what
should I look to change to free up some of this sram?

thanks