Arduino Due crashed because of using an int array?

Hello all. I have a question about saving arrays to the internal flash. My sketch was to save an array of 50,000 samples from the ADC as an integer array.
Using 12bit resolution, that gives me about 100Kb to be on the safe side.
The sketch itself is only about 25.3Kb.
That should leave about 400Kb untouched. However, when I upload it, it crashes.
I was guessing that part of the sketch got written over by the array. Is this possible? And if so, how do I avoid this?
Here is my code.

/*
 10-25-14
 
 Samples and plays back an incoming audio signal at 12 bit resolution
 
 Mode 1 - Sampled audio is immediately played back through the DAC. Sampled audio is simultaneously
           stored in a 20,000 sample array for playback later.
 
 Mode 2 - Saved data is played back through the DAC in a loop. 
 
 Selection between modes is determined by the ADC value of the incoming audio
 */

const int analogInPin = A1;  // Audio in
const int analogOutPin = DAC0; // Audio out

void setup()  {
pinMode (A1, INPUT);      
pinMode (53, OUTPUT);    // Lights an LED when in mode 2
}
void loop() {
    
    analogReadResolution(12);
    analogWriteResolution(12);  
    int sensorValue;        // value read from the ADC
    int savedData[20000];    //int array to save incoming samples
    int dataPointer = 0;    
    
    
  while (true)
  {
    dataPointer = 0;      //reset the pointer
    digitalWrite (53, LOW);  //turn off the LED
    sensorValue = analogRead(analogInPin);
    
    if (sensorValue < 125)
    {  
      
      for (dataPointer = 0; dataPointer< sizeof(savedData); dataPointer++)
      {
        sensorValue = analogRead(analogInPin);      //ADC input
        analogWrite (DAC0, sensorValue * 3);        //play on output amplify by 3
        savedData[dataPointer] = sensorValue;       //Also save in the array
      }                                            
    }
    
    else if (sensorValue > 125)
    {
     digitalWrite (53, HIGH);  //Turn on the LED
     
     for (dataPointer = 0; dataPointer< sizeof(savedData); dataPointer++)
      {
        analogWrite(DAC0, savedData[dataPointer] * 3);  
        delay (.5);  //can insert a delay here for a cool effect
    }
     
    }
  }
  }

Thanks in advance for any help. Cheers.
P.S. Sorry about the code being split up. Not sure what happened. It wont let me paste it as one code block.

int savedData[20000]; on a due is 20000 * 4 bytes = 80000 bytes

for (dataPointer = 0; dataPointer< sizeof(savedData); dataPointer++)

should be

for (dataPointer = 0; dataPointer< sizeof(savedData)/sizeof(int) ; dataPointer++)

sizeof returns size in bytes == 800000 => thus savedData[dataPointer] will go out of range....

Note: you should not name something a pointer which is in fact an index :wink:

quailquillz:
Hello all. I have a question about saving arrays to the internal flash. My sketch was to save an array of 50,000 samples from the ADC as an integer array.
Using 12bit resolution, that gives me about 100Kb to be on the safe side.
The sketch itself is only about 25.3Kb.
That should leave about 400Kb untouched. However, when I upload it, it crashes.
I was guessing that part of the sketch got written over by the array. Is this possible?

No. You need to understand that you are dealing with different types of memory in an Arduino (AVR or SAM/ARM based).

The sketch is in FlashROM, which is read-only once you have uploaded you sketch. It only (by default) contains code. Up to 512KB on an Arduino Due..

Your variables, which require writeable memory, as stored in SRAM, of which the Due has 96KB. Don't know off hand if you can actually use close to that amount as a single variable (minus the general overhead for libs, etc), but your array of 20,000 'int's will take up 80,000 bytes, as by default in a 32 bit processor, sizeof (int) is 4 bytes! That just doesn't fly!

One way how you possibly can get away with this, as you say you are using only 12bit samples, is to explicitly define the array as of type "int_16". That would make above mentioned 20,000 entries large array only 40,000 bytes in size, which would even fit into the 64KB bank of SRAM...

Ralf

That would make above mentioned 20,000 entries large array only 40,000 bytes in size, which would even fit into the 64KB bank of SRAM...

--- good thought.

To the OP, how would the compiler even know you want your samples to go into FLASH?
When you declare an array of type int, it will take that from RAM, aka Variable Space.

I don't know of any "Arduino" way of putting data in FLASH, I would think it takes longer to write to FLASH than RAM, so that delay consideration isn't in the IDE (that I know of).

rmetzner49:
--- good thought.

To the OP, how would the compiler even know you want your samples to go into FLASH?
When you declare an array of type int, it will take that from RAM, aka Variable Space.

I don't know of any "Arduino" way of putting data in FLASH, I would think it takes longer to write to FLASH than RAM, so that delay consideration isn't in the IDE (that I know of).

The point is that you can not "write to Flash". It requires a special write mode, which simply is not available during program/sketch execution. That's why it is called "FlashROM", contrary to "FlashRAM"...

Ralf

I don't know of any "Arduino" way of putting data in FLASH, I would think it takes longer to write to FLASH than RAM, so that delay consideration isn't in the IDE (that I know of).

On an ARM using "const" should place a variable in flash and not copy it to RAM, but as has been noted the problem here is that the array is too large for RAM.

uint16_t savedData[20000]

Should work.


Rob