Weird matrix behaviour for my arduino game

I have:

int screen[SCREEN_HEIGHT][SCREEN_WIDTH];

And I initializate it like this:

for (int i = 0; i < SCREEN_HEIGHT; ++i) {
    for (int j = 0; j < SCREEN_WIDTH; ++j) screen[i][j] = -1;
}

But when I print the matrix values I get:

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
22292 -19701 22292 -19701 30722 7195 8193 256 0 -23773 2816 483 454 454 3013 2098

PD: It seems to be related to the dsiplay and the library I’m using.
I use a ST7920 128x64 display and u8g2 as the screen library.

U8G2_ST7920_128X64_F_HW_SPI u8g2(U8G2_R0, /* CS=*/ 10, /* reset=*/ 8);

The reason I think it is related is because when I add more thing to the buffer that will display the numbers of error I get in the screen matrix increases.

Example when displaying also some custom sprites (numbers from 0 to 7) and a text “Score:”:

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 
2 -1 2 -1 2 -1 2 -1 2 -1 2 -1 2 -1 2 -1 
3 -1 3 -1 3 -1 3 -1 3 -1 3 -1 3 -1 3 -1 
3 -1 3 -1 3 -1 3 -1 3 -1 3 -1 3 -1 3 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 32258 437 5376 4790 5430 
4790 1846 1847 2192 150 0 23058 -27384 30732 2311 3335 1907 -30674 4632 -18923 17166 
32258 3597 14088 1280 0 -23773 3584 2166 2277 2277 3553 2242 2244 225 0 0

Any idea of why this happens??

If you need any extra context let me know.

It’s difficult to be sure. What board are you using and when you compile, how much of the SRAM is being used up? I’m wondering if your screen buffer is somehow too small for what you are trying to do and it’s walking all over the array storage?

Sorry, not much help I’m afraid.

Cheers,
Norm.

How do you print the matrix? This seems right actually.

I use an Arduino Uno.
How do I check the SRAM used?
I use a full buffer (1024 bytes) to send to the screen.

Here is how I print the values:

for (int i = 0; i < SCREEN_HEIGHT; ++i) {
        for (int j = 0; j < SCREEN_WIDTH; ++j) {
            Serial.print(screen[i][j]);
            Serial.print(" ");
        }
        Serial.println("");
    }
    Serial.println("");

And here how I display the values in the screen:

u8g2.clearBuffer();

    for (int i = 0; i < SCREEN_HEIGHT; ++i) {
        for (int j = 0; j < SCREEN_WIDTH; ++j)
            drawSprite(spaceInvadersSprites, screen[i][j], j*SPRITE_WIDTH, i*SPRITE_HEIGHT, SPRITE_HEIGHT, SPRITE_WIDTH);
    }

    u8g2.drawStr(2, 6, "Score:");
    char buff[32];
    itoa(score, buff, strlen(buff));
    u8g2.drawStr(34, 6, buff);

    u8g2.sendBuffer();

Here is the drawSprite function too:

void drawSprite(const unsigned char *spriteSheet, int sprite, int x, int y, int h, int w) {
    if (spriteSheet == NULL or sprite < 0) return;
    if (h > 8) h = 8;

    for (int i = 0; i < w; ++i) {
        char c = spriteSheet[sprite*w+i];
        for (int j = 0; j < h; ++j) {
            if (c & 0x1) u8g2.drawPixel(x+i, y+j);
            c = c >> 1;
        }
    }
}

Okey, I don’t know why, but when I change from an int matrix to a char matrix it just works perfectly. I would assume something had to see with the memory. If anyone know why let me know xd

TY

Here you’re definitely treating the screen object as a char* not as an int*, so that would match.

Char takes up a single byte for each entry in the array. On an Arduino Uno, int take up two bytes.

The Uno only has a maximum on 2048 bytes of SRAM and you were using half of that for your buffer, from what you said above. The Arduino itself uses a few more, at least 9 bytes even in the Blink sketch, as it holds variables for the millis() counter and such like. Any libraries used in the sketch might use additional bytes too.

Also, if you use the String data type on an Uno, you are asking for trouble. It needs to do a lot of dynamic memory allocations “under the hood” and this can lead to SRAM fragmentation and “interesting” bugs to track down if the sketch runs out of SRAM during some of these hidden operations. The advice online, is to avoid these in Arduino sketches, or to use them at your own risk.

You check the output when you compile a sketch. It’s at the end:

...
Checking size .pio/build/uno/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  13.0% (used 266 bytes from 2048 bytes)
Flash: [=         ]   8.3% (used 2690 bytes from 32256 bytes)

Your SRAM is detailed on the first line where it says “RAM”. In the example given, I’ve used 266 bytes of SRAM out of a total of 2048 available.

HTH

Cheers,
Norm.

So it was a memory problem xd

TY for the explanation I will now be more carefull with my memory usage :slight_smile:

1 Like