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