Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more. Accepting essential orders - here's how.
0

Big array causing problems, I think--looking for more info
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Big array causing problems, I think--looking for more info

by Tcepsa on Thu Jan 29, 2009 12:05 am

I have the following chunk of code as a sketch:
Code: Select all | TOGGLE FULL SIZE
char bigArray[5120];

int ledPin = 13;

void setup(){
  Serial.begin( 9600 );
  Serial.println("Starting!");
  pinMode( ledPin, OUTPUT );
}

void loop(){
  digitalWrite( ledPin, HIGH );
  Serial.println("On");
  delay( 1000 );
  digitalWrite( ledPin, LOW );
  Serial.println("Off");
  delay( 1000 );
}


When the first line is commented out (//char bigArray[5120];) or removed, it works as expected: the LED flashes, and the serial monitor sees "Starting!" followed by a tireless stream of "On" and "Off", alternating.

However, when the first line is present, everything seems to break horribly. Nothing happens. The LED doesn't blink or light, nothing comes across on the serial connection (even if I hit the reset button and wait the 7-10 seconds for it to restart).

I suspect I've hit some sort of internal memory restriction. The only thing that I can think of off the top of my head is that it's allocating the ~5k for the array and then copying the actual program code into the memory after that, and it can't make the JMP to such a high memory address to actually start the program. This seems like a long shot, though (to say nothing of being a conundrum; one of the reasons I went with an ATmega168 was for the 16k of Flash, and it seems like I should still be able to use it since I would only be using about 8k between the array, the code, and the Serial library).

I just had a flash of inspiration and tried making it "static char bigArray[5120];" and it seems to have solved the problem (at least, the LED is flashing and I'm getting serial communication again)

So my two (and a half) big questions are:
1) What's going on/why didn't it work when I put that array in there?
2) Did making it static fix it? Why?
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Re: Big array causing problems, I think--looking for more info

by westfw on Thu Jan 29, 2009 1:00 am

The ATmega168 processor used on the Arduino only has 1K of memory (1024 bytes), part of that is taken up by the stack and the runtime environment. An array of 5000+ bytes is not possible.

SOME large data structures can be moved from RAM to FLASH using the PROGMEM directive, but if you really need 5000 byte data arrays, then it's time to look into a different processor...

westfw
 
Posts: 1609
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Big array causing problems, I think--looking for more info

by Tcepsa on Thu Jan 29, 2009 1:30 am

Thank you for responding, but that doesn't match up with my experience... When I programmed the chip with the sketch posted above (using the Arduino programming environment and with the first line commented out so it actually worked), it said
Binary sketch size: 2666 bytes (of a 14336 byte maximum)

If, as you say, it only has 1k (1024 bytes) of space in which to store a sketch, how is it possible for it to store and run this sketch successfully?

(I'm sure there's some subtlety going on here that I am not aware of, but I have no idea what it is ^_^)
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Re: Big array causing problems, I think--looking for more info

by unwiredben on Thu Jan 29, 2009 1:49 am

The sketch size reported is how large the program is in read-only memory (ROM). The 168 has 16K of Flash ROM that can be written to by special code like the bootloader, but can't be changed while your program is running. The array is allocated in read-write memory, and that's far more limited on the ATMega 168.

As for why it works when you declare it as static, that tells the compiler that the array is only accessed in the one source code file. Since you never actually use it in this code, the compiler doesn't have to allocate it, so it gets optimized away. If you actually used it in the code, I expect it would break no matter if it's static or not.

unwiredben
 
Posts: 75
Joined: Fri Dec 19, 2008 2:02 pm
Location: Brooklyn, NY

Re: Big array causing problems, I think--looking for more info

by Tcepsa on Thu Jan 29, 2009 2:12 am

Ahhh, okay, that makes a bit more sense. Thanks!

So, if I were to "program" this big, static table into the upper reaches of the Flash memory somewhere (or wherever would be liable to be free--is there a way to find that out?) through another program solely existing for that purpose, would I be able to read it from Flash during execution?
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Re: Big array causing problems, I think--looking for more info

by mtbf0 on Thu Jan 29, 2009 9:01 am

it'll land in the lower regions, actually. right after the interrupt vectors.

http://www.arduino.cc/en/Reference/PROGMEM
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Big array causing problems, I think--looking for more info

by Tcepsa on Sat Jan 31, 2009 1:18 pm

Great, thank you! I'll play around with that and see whether I can get it working for what I am trying to do. (I didn't realize that I could just ask it to store it there and it would handle finding space for it, etc. ^_^)
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Please be positive and constructive with your questions and comments.