Go Down

Topic: Maximum array size (Read 8775 times) previous topic - next topic

arduino.poet

Hi everybody,

I'm trying to run the following program; it compiles, but it never starts.  :'(
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!

Code: [Select]

#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

arduino.poet

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

mem

#3
Jul 24, 2008, 03:37 pm Last Edit: Jul 24, 2008, 03:37 pm by mem Reason: 1
It looks like you are using the array to store readings from a sensor at 1ms intervals so something like this 24LC64 EEPROM  http://www.futurlec.com/Memory/24LC64.shtml  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 : http://www.arduino.cc/playground/Code/I2CEEPROM


Go Up