available memory

i did some test regarding the available memory. here's my code below

#include <math.h>
void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  int b;

   
   b=availableMemory();
   Serial.println(b,DEC);
}

void loop()
{

} 
// this function will return the number of bytes currently free in RAM

int availableMemory() 
{
  int size = 1024;
  byte *buf;

  while ((buf = (byte *) malloc(--size)) == NULL)
    ;

  free(buf);

  return size;
}

output is 828

i was wondering what was used to wipe off so many bytes since there is no floating point calculation?

i have another question:let say i have a function (say it takes up 200bytes)with full of floating point calculations in it.And i need to call it quite frequently to update some other functions.will the function use up the 1k sram eventually as it was being called ?

The Serial buffer consumes 128 bytes, the rest is due to the startup code and the sketch.

Whatever dynamic RAM a function consumes will be released immediately after the function returns so It doesn't matter how quick you call it.

thanks for the replies ..i will update this thread again

i had successfully send 4 bytes of command packet to control the WCK servo and it's suppose to response by transmitting back 2 bytes back to arduino.

1)is it the right way to clear the 2 bytes by calling Serial.read() for 2 times?please refers to my code PosSend() function.

2)why i couldn't see the rx led blings or it's too fast for a naked eye?

3)how to optimize my code as the dynamic ram only left 50bytes?

here's my code

#include <math.h>
#define HEADER 0xff

void setup()
{  char SpeedLevel,ServoID,Position;
   SpeedLevel=0x01; // hex value to represent the speedlevel
   ServoID=0x00; // hex value to represent the servoID
   Position=0x80; // any value from 0-255(byte/hex)that represents position
   Serial.begin(115200);           // set up baudrate at 9600 bps
   char x,y;
   x=PosSend( ServoID,  SpeedLevel, Position);
   y=availableMemory();
   Serial.println(y,DEC);
}

void loop()
{
}

char PosSend(char ServoID, char SpeedLevel, char Position)
{
  //char Current;
  SendOperCommand((SpeedLevel<<5)|ServoID, Position);
  Serial.read();//read first byte
  Serial.read();//read second byte
  //Current =Serial.read();
  //return Current;
} 

void SendOperCommand(char Data1, char Data2)
{    
  char CheckSum;
  CheckSum = (Data1^Data2)&0x7f;
  Serial.print(HEADER,BYTE);
  Serial.print(Data1,BYTE);
  Serial.print(Data2,BYTE);
  Serial.print(CheckSum,BYTE);
}

int availableMemory()
{
  int size = 1024;
  byte *buf;
  while ((buf = (byte *) malloc(--size)) == NULL);
  free(buf);
  return size;
}

1)is it the right way to clear the 2 bytes by calling Serial.read() for 2 times?please refers to my code PosSend() function.

No. You call Serial.read() occurs well before the motor has had a chance to response to the call to SendOperCommand. Why do you need to clear this buffer anyway? You don't appear to using RX serial anywhere.

2)why i couldn't see the rx led blings or it's too fast for a naked eye?

115 thousand times per second = way too fast! :slight_smile:

3)how to optimize my code as the dynamic ram only left 50bytes?

You actually have 818 bytes free. The problem is that you are assigning the int return from availablememory() into a char variable.

Cheers,

Mikal