[C-Struct] Too complex for Uno and 32x32 LED Matrix ? [Solved]

Hello guys !

Until now I always found the answers of my questions on Google, but not today.
So I come here to explain my problem to you.

First my project:
I'm developping a little game (space invader) based on Arduino Uno and a 32x32 LED Matrix.

I've developped a nice first version quickly but not exactly what I expect and bad wrote in C.

So I want to re-write it using the C structure's.

But something curious appear.
It seems the Arduino can't do the job if the code is too "evolved".
I don't have nothing on my matrix screen.

My code below is compiled and uploaded without error but not working.

#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library

#define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
#define OE  9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

void setup() {
  pinMode(11,INPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);
  matrix.begin(); 
}

struct Missile{
 bool ON; 
 int PosX = 0;
 int PosY = 0;
 int Couleur = matrix.Color333(0, 7, 7);
};
struct Missile missileVaisseau, missileMonstre;

struct Vaisseau{
 int PosX = 0;
 int Couleur1 = matrix.Color333(7, 7, 7);
 int Couleur2 = matrix.Color333(7, 0, 0);
 Missile missileVaisseau;
};
struct Vaisseau vaisseau;

struct Monstre{
  int PosX = 0;
  int PosY = 0;
  Missile missileMonstre;
  };
struct Monstre monstre1, monstre2, monstre3, monstre4, monstre5, monstre6;

void loop() {
  spaceship_print(vaisseau.PosX,vaisseau.Couleur1,vaisseau.Couleur2);
  delay(500);
}

void spaceship_print(int x, int couleur1, int couleur2){
  matrix.drawPixel( 13 + x, 31, couleur1);
  matrix.drawPixel( 15 + x, 31, couleur1); 
  matrix.drawPixel( 17 + x, 31, couleur1); 
  matrix.drawPixel( 13 + x, 30, couleur1); 
  matrix.drawPixel( 14 + x, 30, couleur1); 
  matrix.drawPixel( 15 + x, 30, couleur1); 
  matrix.drawPixel( 16 + x, 30, couleur1); 
  matrix.drawPixel( 17 + x, 30, couleur1); 
  matrix.drawPixel( 14 + x, 29, couleur1); 
  matrix.drawPixel( 16 + x, 29, couleur1); 
  matrix.drawPixel( 15 + x, 28, couleur1); 

  matrix.drawPixel( 15 + x, 29, couleur2); 
  matrix.drawPixel( 14 + x, 31, couleur2); 
  matrix.drawPixel( 16 + x, 31, couleur2);
  }

That's curious because when I comment this part of the code it's working well, I print a spaceship on my screen, just like I expected.

struct Monstre{
  int PosX = 0;
  int PosY = 0;
  Missile missileMonstre;
  };
struct Monstre monstre1, monstre2, monstre3, monstre4, monstre5, monstre6;

Does anybody have already had the same problem ? It's really curious...

It seems the Arduino can't do the job if the code is too "evolved".

Nonsense.

I don't have nothing on my matrix screen.

Not having nothing on the screen is a good thing, isn't it?

Just how much memory do you have? Just how much are you using?

I'm on an Uno, so I've 32ko of memory and I'm only using 8ko...

And how much RAM are you using?

I use under of 2ko RAM, only hundreeds octets. If I believe the IDE their is nothing excedded or in error. Everythings are good for the IDE but their is a problem in the execution by the chip.

How did you get it to compile? Here, for example:

struct Missile{
 bool ON; 
 int PosX = 0;
 int PosY = 0;
 int Couleur = matrix.Color333(0, 7, 7);
};

Errors:

sketch_apr26a:25: error: ISO C++ forbids initialization of member ‘PosX’
sketch_apr26a:25: error: making ‘PosX’ static
sketch_apr26a:25: error: ISO C++ forbids in-class initialization of non-const static member ‘PosX’
sketch_apr26a:26: error: ISO C++ forbids initialization of member ‘PosY’
sketch_apr26a:26: error: making ‘PosY’ static
sketch_apr26a:26: error: ISO C++ forbids in-class initialization of non-const static member ‘PosY’
sketch_apr26a:27: error: ‘matrix’ cannot appear in a constant-expression
sketch_apr26a:27: error: `.' cannot appear in a constant-expression
sketch_apr26a:27: error: a function call cannot appear in a constant-expression
sketch_apr26a:27: error: ISO C++ forbids initialization of member ‘Couleur’
sketch_apr26a:27: error: making ‘Couleur’ static
sketch_apr26a:27: error: ISO C++ forbids in-class initialization of non-const static member ‘Couleur’

I'm using Arduino IDE 1.6.3, and don't have any error.

You can find in attachement the log of my two files and the files.

Here the result of the execution where I've a Ok result.

Sketch uses 8,544 bytes (26%) of program storage space. Maximum is 32,256 bytes.
Global variables use 329 bytes (16%) of dynamic memory, leaving 1,719 bytes for local variables. Maximum is 2,048 bytes.

And here the result of the execution without the comment and the result is not correct.

Sketch uses 8,688 bytes (26%) of program storage space. Maximum is 32,256 bytes.
Global variables use 395 bytes (19%) of dynamic memory, leaving 1,653 bytes for local variables. Maximum is 2,048 bytes.

Both uploaded without error and without ROM or RAM exceed.

Result_OK.txt (25.9 KB)

Result_OK.c (1.71 KB)

Result_NOK.txt (26 KB)

Result_NOK.c (1.69 KB)

DSCF5037.JPG

DSCF5038.JPG

So what is your actual problem , then ?

Are you getting an error saying that your program is "too evolved" ??

struct Monstre{
  int PosX = 0;
  int PosY = 0;
  Missile missileMonstre;
  };
struct Monstre monstre1, monstre2, monstre3, monstre4, monstre5, monstre6;

The one that doesnt work, contains this code.

It seems to me, that the sub-struct in this structure should be declared as

  struct Missile missileMonstre ;

unless you have defined a typedef synonym for "struct Missile".

But maybe, newer version of C++ allow this.

I think you need to check how much ram your program is using, when it is actually running.

There are several things I don't like about your code, but if they were wrong, then your OK version would also fail. So the things I don't like, must actually be OK.

Adding the Monstre code to the second version, adds 6 instances of the struct with 11 bytes of data each, and the amount of bytes that the global variables in your program increases by the same amount, 66 bytes.

It is possible, however, that the matrix object might be using say 1700 bytes of Ram, so when you increase the variables in your program from 325 bytes to 391 bytes, you actually cause the total to exceed 2k bytes.

The three things I don't like are:

(1) Nick Gannon's issue with whether that colour initialization is valid. I don't like it either, but it works in your working version.

(2) The missing keyword struct before the word Missile, in the declaration of both the Vaisseau and Monstre structs. But if you can "get away with this" in Vaisseau, then why not in Monstre ?

(3) The third thing I am wary about, is the use of the same names missileVaisseau and missileMonstre, for the internal components of the second and third struct, as well as for the two declared instances of the first struct.

This should not be a problem, but it may cause a problem in relation to (2). Since the attempt to merge the functionality of objects and structs, it may be attempting to use the declared instances of the first struct, somehow when defining the fields of the second and third struct with the same names. And there is a difference, here. You only declare ONE instance of the second struct, but you declare SIX instances of the third struct. The interaction of this issue, with issue (2) might be what is causing your problem in some obscure way.

I would try changing the name of the Missile element in the second and third struct, so that it is not the same as the two declared instances of the first struct. See what happens. If the compiler is treating these as object and not plain structs, that might reveal a problem.

I would also try adding the keyword struct before the Missile, in the second and third struct. See if that makes any difference.

I thought too it was a RAM problem. I've tried with a Arduino Mega 2560 with 8Ko of RAM... and it's running well ! So I'll play now with my Mega card.

Thanks for your help guys :wink: