Go Down

Topic: Serial.print issues (Read 744 times) previous topic - next topic

irethedo

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:
Code: [Select]
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:
Quote

****  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

SurferTim

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

irethedo

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

SurferTim

Quote
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).
Code: [Select]
int bufferMatrix[50][50];

kanurys

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 :)



50 * 50 * 2 = 5000 bytes.


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

oh, 2 bytes for int. got it.

irethedo

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

SurferTim

I include this function code to any sketch I want to check the SRAM.
Code: [Select]
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.
Code: [Select]
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.

irethedo

Thanks Surfer Tim-

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

Does this mean I have 1248 bytes left?


SurferTim

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.
Code: [Select]
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() {
}


irethedo

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

Go Up