"Not enough memory" error for simply looping through my array.

Hi brothers.

This code works:

int setCycle[10][20][8] = 
  { 
    {
       {1, 180, 3, 1, 0, 5, 160, 180}, 
       {2, 180, 3, 1, 0, 10, 90, 0}, 
       {3, 180, 3, 1, 1, 30, 0, 0}, 
       {4, 360, 3, 1, 0, 10, -50, 100},
       {5, 180, 3, 1, 0, 5, 160, 180},
       {6, -1, -1, -1, -1, -1, -1, -1} 
    },
    {
       {1, 180, 3, 1, 0, 5, 160, 180}, 
       {2, 180, 3, 1, 0, 10, 90, 0}, 
       {3, 180, 3, 1, 1, 30, 0, 0}, 
       {4, 360, 3, 1, 0, 10, -50, 100},
       {5, 180, 3, 1, 0, 5, 160, 180},
       {6, -1, -1, -1, -1, -1, -1, -1} 
    }
 };

void setup()
{

Serial.println(setCycle[0][0][0]); 
Serial.println(setCycle[1][1][0]); 
Serial.println(setCycle[0][2][0]); 
Serial.println(setCycle[0][3][0]); 
Serial.println(setCycle[0][4][0]); 
Serial.println(setCycle[0][5][0]); 
Serial.println(setCycle[0][6][0]); 
Serial.println(setCycle[0][7][0]); 
Serial.println(setCycle[0][8][0]); 
Serial.println(setCycle[0][9][0]); 
Serial.println(setCycle[0][10][0]); 
Serial.println(setCycle[1][11][0]); 
Serial.println(setCycle[0][12][0]); 
Serial.println(setCycle[0][13][0]); 
Serial.println(setCycle[0][14][0]); 
Serial.println(setCycle[0][15][0]); 
Serial.println(setCycle[0][16][0]); 
Serial.println(setCycle[0][17][0]); 
Serial.println(setCycle[0][18][0]); 
Serial.println(setCycle[0][19][0]); 

}

The same code spits the “Not enough memory;” error when I try to use a FOR loop:

int setCycle[10][20][8] = 
  { 
    {
       {1, 180, 3, 1, 0, 5, 160, 180}, 
       {2, 180, 3, 1, 0, 10, 90, 0}, 
       {3, 180, 3, 1, 1, 30, 0, 0}, 
       {4, 360, 3, 1, 0, 10, -50, 100},
       {5, 180, 3, 1, 0, 5, 160, 180},
       {6, -1, -1, -1, -1, -1, -1, -1} 
    },
    {
       {1, 180, 3, 1, 0, 5, 160, 180}, 
       {2, 180, 3, 1, 0, 10, 90, 0}, 
       {3, 180, 3, 1, 1, 30, 0, 0}, 
       {4, 360, 3, 1, 0, 10, -50, 100},
       {5, 180, 3, 1, 0, 5, 160, 180},
       {6, -1, -1, -1, -1, -1, -1, -1} 
    }
 };

void setup()
{
for (int i=0; i < 20; i++) 
 { Serial.println(setCycle[0][i][0] ); }
}

Thank you.

Please tell us what Arduino you are using so we have some idea how much SRAM it has.

Please post the complete program.

…R

10208*2bytesPerInt is 3200, way more than the 2048 bytes available on an Uno.

Your first case works because the compiler does all the array lookups at compile time, changes the prints to just printing those specific constants, notices that there are no other users of the array, and deletes it from the final image.

Hi westrfw and Robin2. Thanks for your replies.

If I move the whole array from global into the setup() part it works

I have 2 issues here.

1st. I plan to fill more slots of the array which has to be of this [10][20][8] dimension. The user will choose only a part at a time

2nd What I am trying is to define the arrays outside the setup() part and particularly in other files in order to keep my home code clean !

The complete error after compiling is here:

Sketch uses 4948 bytes (15%) of program storage space. Maximum is 32256 bytes.
Global variables use 3388 bytes (165%) of dynamic memory, leaving -1340 bytes for local variables. Maximum is 2048 bytes.
Not enough memory; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing your footprint.
Error compiling for board Arduino/Genuino Uno.

The complete used code (copied pasted) is here:

int setCycle[10][20][8] =
  {
    {
       {1, 180, 3, 1, 0, 5, 160, 180},
       {2, 180, 3, 1, 0, 10, 90, 0},
       {3, 180, 3, 1, 1, 30, 0, 0},
       {4, 360, 3, 1, 0, 10, -50, 100},
       {5, 180, 3, 1, 0, 5, 160, 180},
       {6, -1, -1, -1, -1, -1, -1, -1}
    },
    {
       {1, 180, 3, 1, 0, 5, 160, 180},
       {2, 180, 3, 1, 0, 10, 90, 0},
       {3, 180, 3, 1, 1, 30, 0, 0},
       {4, 360, 3, 1, 0, 10, -50, 100},
       {5, 180, 3, 1, 0, 5, 160, 180},
       {6, -1, -1, -1, -1, -1, -1, -1}
    }
 };


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
for (int i=0; i < 20; i++)
 { Serial.println(setCycle[0][i][0] ); }
}

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

}

You don’t have 3200 bytes of RAM either on the stack or in globals.

Hi,
What model Arduino are you using, it looks like a UNO or Nano?

I have given you a karma for using code tags.

Thanks.. Tom.. :slight_smile:

If I move the whole array from global into the setup() part it works

No it doesn't. It merely becomes unable to figure out that you have exceeded the memory space available at compile time, so it can't warn you.
If the array is full of constants, you should look at PROGMEM.
If it's not, then you need a bigger chip.

westfw:
No it doesn't. It merely becomes unable to figure out that you have exceeded the memory space available at compile time, so it can't warn you.
If the array is full of constants, you should look at PROGMEM.
If it's not, then you need a bigger chip.

Thank you brother. I would use a combination of PROGMEM. The constants will go there and the array would fit in the 1024 address slots of the EEPROM memory so users will be able to modify them every now and then using an LCD and button, occasionally.

TomGeorge:
......
I have given you a karma for using code tags.

I would thank you for my first karma !!! Yes its an UNO!

TheMemberFormerlyKnownAsAWOL:
You don't have 3200 bytes of RAM either on the stack or in globals.

Thanks I am going to apply a different approach, a combination of the PROGMEM (constant flash) and EEPROM memory functions of the ATMEGA 328P chip.

Thank you all for your help.

The EEPROM holds only 512 "int"s.

TheMemberFormerlyKnownAsAWOL:
The EEPROM holds only 512 "int"s.

You are right.

The UNO ATMEGA 328P holds 1024 bytes. Every int holds 2 bytes.

But a byte holds a number from 0-255 which is enough for my modification. Therefore I consider I have 1024 slots available for my Settings Data.

reference: Arduino - EEPROM

1024 bytes on the ATmega328P, 512 bytes on the ATmega168 and ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560. The Arduino and Genuino 101 boards have an emulated EEPROM space of 1024 bytes.

Careful of abusing the umber of EEPROM write cycles