Can this be optimized to minimize memory?

Don't know much about memory/ram etc... I have this circular buffer code (yes its very basic!) but as is it takes 400% memory usage. Is there something I can do different to keep the array sizes I want but minimize memory usage?

#define MP_DIM_X 100 //Storage will be 1 less than this
float mp [MP_DIM_X][20];
unsigned int mp_head;
unsigned int mp_tail;

float storeSomething[6][20];
float readSomething[1][20];
float writeSomething[1][20];

void setup() {
  //-------------------------------------//
  //Serial
  //-------------------------------------//
  Serial.begin(9600);
  delay(200);
  Serial.println("\n\n\n\n\n\n\n\nStart");
  delay(200);

  mp_head = 0;
  mp_tail = 0;

  for (int i = 0; i < 7; i++) {
    writeSomething[0][0] = 0;
    writeSomething[0][1] = i * 2 + 1;
    bufferPush(writeSomething);
  }

  bufferLastMember(readSomething);


  bufferShift(readSomething);
  for (int i = 0; i < 2; i++) {
    storeSomething[0][i] = readSomething[0][i];
  }
  bufferShift(readSomething);
  for (int i = 0; i < 2; i++) {
    storeSomething[1][i] = readSomething[0][i];
  }
  bufferShift(readSomething);
  for (int i = 0; i < 2; i++) {
    storeSomething[2][i] = readSomething[0][i];
  }
  bufferShift(readSomething);
  for (int i = 0; i < 2; i++) {
    storeSomething[3][i] = readSomething[0][i];
  }
  bufferShift(readSomething);
  for (int i = 0; i < 2; i++) {
    storeSomething[4][i] = readSomething[0][i];
  }
  bufferShift(readSomething);
  for (int i = 0; i < 2; i++) {
    storeSomething[5][i] = readSomething[0][i];
  }

  bufferLastMember(readSomething);

  for (int i = 0; i < 8; i++) {
    writeSomething[0][0] = 0;
    writeSomething[0][1] = i * 2 + 101;
    bufferPush(writeSomething);
  }
}

void bufferPointerAdd(unsigned int &num) {
  if (num == MP_DIM_X - 1) {
    num = 0;
  }
  else {
    num++;
  }
}

void bufferPointerSub(unsigned int &num) {
  if (num == 0) {
    num = MP_DIM_X - 1;
  }
  else {
    num--;
  }
}

void bufferPush(float (&a)[1][20]) {
  for (int i = 0; i < 2; i++) {
    mp[mp_tail][i] = a[0][i];
  }
  bufferPointerAdd(mp_tail);
}

void bufferShift(float (&a)[1][20]) {
  for (int i = 0; i < 2; i++) {
    a[0][i] = mp[mp_head][i];
  }
  bufferPointerAdd(mp_head);
}

void bufferLastMember(float (&a)[1][20]) {
  for (int i = 0; i < 2; i++) {
    a[0][i] = mp[(mp_tail - 1)][i];
  }
}

int bufferSize() {
  if (mp_tail < mp_head) {
    return (MP_DIM_X + mp_tail - mp_head);
  }
  else {
    return (mp_tail - mp_head);
  }
}

boolean bufferIsFull() {
  if (bufferSize() == MP_DIM_X - 1) {
    return true;
  }
  else {
    return false;
  }
}

boolean bufferIsEmpty() {
  if (bufferSize() == 0) {
    return true;
  }
  else {
    return false;
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

For one thing, you are using "int" for everything. If the value never gets above 255, use "byte".

Paul

And you are using "float" but only, ever, putting integers into them.

#define MP_DIM_X 100 //Storage will be 1 less than this

Not the way you're using it. Storage size will be equal to that.

float mp [MP_DIM_X][20];

That one array consumes 8K of RAM....

Regards,
Ray L.

float storeSomething[6][20];
float readSomething[1][20];
float writeSomething[1][20];

Do these need to be floats ?

What is the point of a 2 dimensional array where one of the dimensions only has one element ?

Darn. Other than small tweaks (byte vs int, make it more complicated so no all elements are necessarily floats...), sounds like no major misses.

UKHeliBob: easier to match when putting in the buffer I guess. Does this take more memory though or is it just a non-standard approach?

Android does not offer swap space for memory, however it does use paging and memory-mapping. Any files or resources which are present on the disk, such as code, are kept in mmap’ed pages walgreenslistens.com
Android knows these pages can be recovered from the disk, so they can be paged out if the system needs memory somewhere else.

ghannamr:
Android does not offer swap space for memory, however it does use paging and memory-mapping. Any files or resources which are present on the disk, such as code, are kept in mmap’ed pages. Android knows these pages can be recovered from the disk, so they can be paged out if the system needs memory somewhere else.

It's unclear to me how this is at all applicable to Arduino.