Pages: [1]   Go Down
Author Topic: Maximum array size  (Read 5645 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
Logged

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 24, 2008, 08:37:50 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!  smiley
Logged

Pages: [1]   Go Up
Jump to: