Global objects

Hi all,

the sketch below prints "Serial started" and nothing else, it seems like setup or loop are never reached, and I can't see why... any idea?

Obviously this is a very simplified version of a real sketch where I am initializing lots of global objects...

Franck

class Dummy {
  public:
    Dummy() {
      Serial.begin(9600);
      Serial.println("Serial started.");
      Serial.flush();
    };
};

Dummy dummy;

void setup()
{
  Serial.println("In Setup"); Serial.flush();
}

void loop()
{
  delay(2000);
  Serial.println("looping"); Serial.flush();
}

try getting rid of Serial.flush();

At the time the constructor is executed, not much of the Arduino environment is initialized. I guess your Serial.begin() is cleaned out.

arduino_new:
try getting rid of Serial.flush();

Well yes of course, but the real code was initializing a bunch of different objects so I added the Serial to debug that.

It turns out that an lcd.begin(...) in one of the constructors was the problem; I cleared all constructors and added a few begin() methods where needed and everything works.

I guess interacting with the hardware in global object constructors is not a good idea.

Thanks!
Franck

It turns out that an lcd.begin(...) in one of the constructors was the problem

Even though there was no LCD code in the program you posted...

I guess interacting with the hardware in global object constructors is not a good idea.

Which is why well written libraries have a init() or similar function that is called after the object has been created.

UKHeliBob:
Even though there was no LCD code in the program you posted...

Did you read the message?
"Obviously this is a very simplified version of a real sketch where I am initializing lots of global objects..."

Well written libraries? That's your point of view... it requires one more state for objects - constructed but not initialized - with its cohort of bugs and pitfalls.

It's more a characteristic of the platform that we have to accommodate, certainly not a feature.

Franck

Did you read the message?

Yes, but it was you that said

the sketch below prints "Serial started" and nothing else,

and later said

It turns out that an lcd.begin(...) in one of the constructors was the problem

Have you solved the problem with the sketch originally posted ?

I see, let me be more explicit...

The pb with my real sketch was an lcd.begin; I then added the Dummy debugging object to troubleshoot and the problem became Serial.xxx andlcd.begin. I removed both and the problem went away.

The pb with the sample sketch I posted was the Serial.begin().

Thanks for helping out - Franck

OK - understood