max size of "long" array?

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?

motomoto:
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 :slight_smile:

Use the Enhanced Arduino IDE, it will tell you when you exceed available memory, and will prevent the broken program to be uploaded.

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

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:

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]);
      
    }
  }
}

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 :slight_smile:

=(

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

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

Is it feasible to create arrays in Flash memory and store values received from serial to flash?

Is it feasible to create arrays in Flash memory and store values received from serial to flash?
PROGMEM - Arduino Reference

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)