Pages: [1]   Go Down
Author Topic: max size of "long" array?  (Read 582 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to create a type long array to store time values in milliseconds received over serial. I can't use int since some of the values I'm dealing with exceeds the maximum int value (>65,000).

When I create a long array of size 1000, the sketch runs fine. However if I bump it up to size 2000 then I get junk on the serial monitor.. I'm using an Arduino Mega 2560.

I can also create multiple long arrays of size 1000 and it still works fine.

What's the maximum size for a long array?

Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can also create multiple long arrays of size 1000 and it still works fine.

I doubt it!

An array of 2000 (or 2 arrays of 1000) long values = 8000 bytes, Arduino mega have 8192 bytes available, and the program need some place too smiley

Use the Enhanced Arduino IDE, it will tell you when you exceed available memory, and will prevent the broken program to be uploaded.
« Last Edit: November 27, 2012, 11:14:43 pm by guix » Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your code by using # and output of serial

long equal to 4 bytes and it's mean 2000 *4 = 8KB     SRAM   8 KB

this mean your SRAM saturate
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That makes sense. I will try the enhanced IDE.

I can create multiple long arrays of size 1000 but I have not tried to fill them up. That's probably why they seem fine to me.

Here's my code:

Code:
void loop() {

    long t1[1000];  //type long to store big numbers (time in ms)
    long t2[1000];
    long t3[1000];
    int n[2000];
 
  // if there's any serial available, read it:
  if (Serial.available() > 0) { 
    if(start == 0){
      // look for the next valid integer in the incoming serial stream:
      long c1 = Serial.parseInt();
      // do it again:
      int c2 = Serial.parseInt();
 
      // look for newline
      if (Serial.read() == '\n') {
       
        Serial.println(c1, DEC);
        Serial.println(c2, DEC);
 
        t1[index] = c1;
        n[index] = c2;
        ++index;
      }
    }
   
    else if(start == 1){
      //output to pins
      int i;
      for (i = 1; i < index; i=i+1){
        delay(t1[i]-t1[i-1]);
        digitalWrite(n[i-1]*2+20, LOW);
        digitalWrite(n[i]*2+20, HIGH);
      }
    }
   
    //print what's currently in the arrays
    Serial.println("Sequence");
    int i;
    for (i = 0; i < index; i = i + 1) {
    Serial.println(t1[i]);
    //Serial.println(n[i]);
     
    }
  }
}
Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, if you don't use a variable (in you case, the t2 and t3 arrays) then it is simply deleted from the program when compiled, that's why it worked smiley
« Last Edit: November 27, 2012, 11:48:22 pm by guix » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-cry

Is there a more efficient method that I could use to store 2000+ time values?
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a more efficient method that I could use to store 2000+ time values?

Efficient method depends on your application what you want to do with those values.
EEPROM and SD card is way to store values
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it feasible to create arrays in Flash memory and store values received from serial to flash?
http://arduino.cc/en/Reference/PROGMEM
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it feasible to create arrays in Flash memory and store values received from serial to flash?
http://arduino.cc/en/Reference/PROGMEM

Flash (PROGMEM) memory can only be populated at program burn time. You can’t change the values in the flash after the program has started running.(REF: arduino.cc)
Logged

From Idea To Invention

Pages: [1]   Go Up
Jump to: