Maximum array size

Hi everybody,

I'm trying to run the following program; it compiles, but it never starts. :cry:
Reducing the array size makes it run but I need to use a pretty big array.

1000 short ints should not overflow ATmega168 memory, should they?
1000 * 16 bit = 1000 * 2 byte = 2000 byte
(compiling this program returns: " 2838 bytes (of a 14336 byte maximum)")

Why is it happening?
Thank you very much!

#define L  1000

short speed[L];
int gyroPin = 0;

void setup() {
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
}

void loop() {
  long int last = 0;
  long int now = 0;
  
  last = millis();
  
  for(int i=0; i<L; i++) {
    now = millis();
    while(now == last) {      // wait until 1 ms elapses
      delayMicroseconds(50);
      now = millis();
    }
    speed[i] = analogRead(gyroPin);
  }
  
  send();
}

void send() {
  Serial.begin(9600);
  Serial.println("Ang speed-----------------");
  for(int i=0; i<L; i++) {
    Serial.println(speed[i]);
  }
  while(1);
}

Variables are not stored in program memory (flash), they are stored in RAM, and the mega168 only has 1k of RAM. You realistically only have room for an array of maybe 200 to 300 2-byte integers since the Arduino serial library uses 128 bytes for its receive buffer and the mega168 uses stores its stack in RAM. If your array is constant you can store it in program space (which is 14k) by including <avr/pgmspace.h> and using the PROGMEM attribute in your array declaration, but it looks like in your case this wouldn't help you.

If you need to store a large array, maybe you should look into purchasing an external RAM module.

  • Ben

Variables are not stored in program memory (flash), they are stored in RAM, and the mega168 only has 1k of RAM.
[...] If your array is constant you can store it in program space (which is 14k) by including <avr/pgmspace.h> and using the PROGMEM attribute in your array declaration, but it looks like in your case this wouldn't help you.

If you need to store a large array, maybe you should look into purchasing an external RAM module.

Thanks for the info!
It's good to know that pgmspace.h exists.
Where can I find a RAM module? Is it fast as interna RAM?

Thank you again :slight_smile:

It looks like you are using the array to store readings from a sensor at 1ms intervals so something like this 24LC64 EEPROM 24LC64 8k x 8 Microchip Serial EEPROM Datasheet may be suitable. It should be more than fast enough for your two bytes per ms data rate, will hold the readings even with power off, uses just two (I2C) pins for interface and costs a dollar or two an 8k byte part.

you can find an example sketch interfacing with i2c memory here : Arduino Playground - I2CEEPROM

Thanks! :slight_smile: