Pages: [1]   Go Down
Author Topic: Several problems with large byte arrays  (Read 1181 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've encountered several problems using large arrays with Arduino
(V14,15 and 16 under Ubuntu).  I'm trying to generate arbitrary
waveforms from a DAC, the code works great if the waveforms are
256 bytes long, but when they are around 800 bytes long bad values
are produced.  Downloading similar code with a 3000 byte array
bricks the Arduino by overwriting the bootloader.
I tried putting line breaks within the data but that did not
change the behavior.  No errors show up when compiling any
of this code.

An example of the problem code is here:
http://www.solorb.com/elect/musiccirc/ArduinoLFO/
as DAC3.pde
« Last Edit: July 24, 2009, 04:27:30 pm by TubeGlow » Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are most likely running out of RAM. You've only got 2k bytes to play with so an 800 byte array takes up a fair chunk. Now you say "bytes" but if you declare your array as:

int array[800];

you're actually taking up 1600 bytes since an "int" is 2 bytes.

When you say the 3000-byte array overwrites the bootloader, that suggests you're actually putting the arrays in FLASH? That was going to be my recommendation, putting the arrays in FLASH (using the PROGMEM keyword). But if you're already doing that.....then I don't know.

So can you clarify exactly what you're doing code-wise and what other RAM/FLASH resources are taken up by your program?
Logged

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

That sounds like a plausible explanation for why the longer arrays
don't work.  However, when I take
working code with three 256 byte (not int) arrays and change
them to PROGMEM, the data becomes partially garbled.
I thought I'd start by trying PROGMEM on the working code before
trying the broken code that uses the longer arrays.

re: the response: The large array that overwrote the flash was NOT stored in
flash:
#define STEPS 3840
byte wavetable[STEPS] = {0,.....};
« Last Edit: July 24, 2009, 08:10:24 pm by TubeGlow » Logged

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

...So, to wrap this issue up, some details:
I'm using an Arduino Decimilia board with the ATmega 168 cpu,
that's 16K flash and 1K RAM according to the Atmel data sheet.
Apparently, creating an array that's longer than the
size of RAM can compile without an error and if the array is
much larger than RAM, the bootloader gets corrupted.  Fixing this
requires reflashing the CPU via a USBtinyISP board.

Putting the large arrays into PROGMEM seems like the logical
solution, but experiments produce corrupt data.  More experimentation will be required here...
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 267
dinosaur cork
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Make sure you use the PROGMEM datatypes when you use PROGMEM. I remember my sketches generated bizarre bugs with corrpt data at certain size limits when I used regular data types.

The PROGMEM docs continue to suffer banishment from management, so you have to dig for them.

http://arduino.cc/en/Reference/PROGMEM
Logged

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

Tubeglow wrote:
Quote
Apparently, creating an array that's longer than the
size of RAM can compile without an error

Can and usually will. The compiler is very trusting about such issues. PROGMEM does work reliably (on my duemilanove at least. I embedded a couple of images for a recent projects using it; the largest one was about 5.5KB and could be read (in lots of small chunks smiley-wink) without trouble.
Logged

Pages: [1]   Go Up
Jump to: