1

Consider the following code:

    #include <Arduino.h>
    
    unsigned char testimage [] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    ... many more rows ...
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };
    
    void setup()
    {
        Serial.begin(115200);
        while(!Serial);
    
        Serial.println("start");
    
        for (int i = 0; i < 10; i++) {
            Serial.println((int)(testimage[i]));
        }
    
        Serial.println("stop");
    
    }
    void loop()
    {}

I found out that then the array gets a certain size, there is nothing being sent to the serial monitor. It looks like the entire program freezes. I do not even see the start message appear.

I haven't tried to find the exact size, but it is somewhere between 4500 and 8000. (When sized around 4500, it works, when around 8000 it doesn't).

When I do not address the array though, I do see the start and stop messages appear.

I am using an Arduino ATMega2560.

2
  • 2
    does not work is a vague description of a problem ... what happens exactly? ... what were you expecting to happen? ... please add the info to the question ... do not write a comment
    – jsotola
    Nov 18, 2023 at 17:04
  • Mega has 8000 bytes large SRAM
    – Juraj
    Nov 18, 2023 at 18:06

2 Answers 2

4

It isn't clear whether you meant to put your array in ROM as your question implies, but the code you presented assigns it to RAM. That would surely restrict to your array to less than the 8K of of RAM minus whatever is needed by (globals + your libraries' statics + your code's statics + the stack).

Update:

I assumed the data section of the code would end up in ROM.

In fact, an image of the initialized data section does become part of the ROM image. The initial values need to be saved somewhere so RAM can be initialized each time the program is(re-)started but it is not accessed from ROM during run-time. (Note we're not talking about PROGMEM data here, which is kept in ROM and accessed from there even at run-time.)

1
  • Thanks. I assumed the data section of the code would end up in ROM. I guess the example code seems to be completely wrong ;). Nov 19, 2023 at 14:02
3

In this case the RAM size is the limit. And you'd need some space for stack/heap,..

If you use non-const array, it'll be copied from flash to RAM at the startup

On the mega2560 even using const it still be copied into the RAM as each FLASH/RAM/EEPROM has different address space

Only way would be using PROGMEM and it can't be used directly (again different address spaces for flash/ram)

You can guess the mega2560 has 8KB RAM and you should keep some RAM for runtime. If there isn't enough of space, the stack and heap will be smashing each other and it would behave completely unpredictably (overwritten return addresses, runaway code and so on)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.