Constructor not called and loop not runnin

I am working on a project where i created my own class to encapsulate u8glib to simulate a console output. However, i have run into problems with my constructor. To simplify the code I have recreated the same issue in a simpler project.

My problem is that i don't know why my constructor for MyClass is never called (led 13 is not blinking, and no serial output) and further why Loop is not run (led 13 is not blinking).

I bet the reason is very simple, but right now I'm stuck. Any help would be greatly appreciated!

Test_Class_Constructor.ino

#include "MyClass.h"

const int led = 13;

MyClass mc(led);

//SETUP
void setup(void)
{  
  mc.printMessage();  
}

void loop(void)
{
  // Do some slow blinking
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

MyClass.h

#ifndef MyClass_h

#include <Arduino.h>

class MyClass
{
  public:
    MyClass(int sPin);
    void printMessage(void);
  private:
    int state;
    int statusPin;
    
};

#endif

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass(int sPin)
{
  Serial.begin(9600);
  Serial.println("Initializing MyClass...");
  
  statusPin = sPin;
  state = 1;
  pinMode(statusPin, OUTPUT); 

  // Do some fast blinking
  digitalWrite(statusPin, HIGH);   delay(125); digitalWrite(statusPin, LOW);   delay(50); digitalWrite(statusPin, HIGH);   delay(125);   digitalWrite(statusPin, LOW);    delay(600);           
  digitalWrite(statusPin, HIGH);   delay(125); digitalWrite(statusPin, LOW);   delay(50); digitalWrite(statusPin, HIGH);   delay(125);   digitalWrite(statusPin, LOW);    delay(600); 
  digitalWrite(statusPin, HIGH);   delay(125); digitalWrite(statusPin, LOW);   delay(50); digitalWrite(statusPin, HIGH);   delay(125);   digitalWrite(statusPin, LOW);    delay(600); 
  digitalWrite(statusPin, HIGH);   delay(125); digitalWrite(statusPin, LOW);   delay(50); digitalWrite(statusPin, HIGH);   delay(125);   digitalWrite(statusPin, LOW);    delay(600); 
  digitalWrite(statusPin, HIGH);   delay(125); digitalWrite(statusPin, LOW);   delay(50); digitalWrite(statusPin, HIGH);   delay(125);   digitalWrite(statusPin, LOW);    delay(600); 
  digitalWrite(statusPin, HIGH);   delay(125); digitalWrite(statusPin, LOW);   delay(50); digitalWrite(statusPin, HIGH);   delay(125);   digitalWrite(statusPin, LOW);    delay(600); 
  

  Serial.println("MyClass initialized.");
}

void MyClass::printMessage()
{
  Serial.println("Message");
}
#ifndef MyClass_h

When you start your header file with this, you need to have a #define MyClass_h on the next line.

MyClass::MyClass(int sPin)
{
  Serial.begin(9600);
  Serial.println("Initializing MyClass...");

There's a good reason the things that "Serial.begin" does are not in a constructor.

Init needs to be called before your serial usage in the constructor,

As the instance is global, your constructor runs before the call to init().

You can move the constructor code to a function named start or begin ( begin isn't the best choice as it is a C++11 free function ).
Then call this function from setup.

Thanks for all replies, I will try to move the code to a start function.

What kind of code is generally placed in constructors then? Only variable initialization?

Moving the code out from the constructor did the trick. Thanks for the help!

Now it's back to debugging my console output to the OLED display :slight_smile:

If you want to use Arduino features in the constructor, just don't use a global object... However there is always a solution.

On an AVR you can control the start up, which I do in my library here: http://arduino.land/Code/SmallSetup/. Using it, your original code should work ( as it calls init() before global constructors are called. ).

The code is short so I'll post it here, you do not need to do anything apart from include it, or just dump this code into the start of your sketch.

//Written by Christopher Andrews, Provided under MIT & GPL licences.
#if defined( __AVR__ ) && !defined( HEADER_SMALLSETUP )
	#define HEADER_SMALLSETUP
	#include "wiring.c"

	/*** init runs before constructors ***/
	__attribute__ ((naked)) __attribute__ ((section (".init5"))) void init();
	
	/*** USBAttach runs after constructors ***/
	#if defined( USBCON )
		__attribute__ ((naked)) __attribute__ ((section (".init7"))) void USBAttach();
		void USBAttach(){ USBDevice.attach(); }
	#endif	
	
	/*** setup runs after USBAttach to ensure Serial is active ***/
	__attribute__ ((naked)) __attribute__ ((section (".init8")))  void setup();

	/*** main() runs as normal, only contains runtime code ***/
	__attribute__ ((naked)) int main();
	
	#ifdef __AVR_ATtiny25__
		int main(){ return (({while( loop(), true );}),0x00); }
	#else
		int main(){ return (({while( ( serialEventRun ? serialEventRun() : ( void ) 0 ), loop(), true );}),0x00); }
	#endif
#endif

Thanks pYro_65 , I've copied this in to my book of tricks. :slight_smile:

What kind of code is generally placed in constructors then? Only variable initialization?

Your code is probably "typical", but it doesn't work on Arduino (without pyro_65's fixes) because of the order in which the arduino peripherals are initialized compared to the contructor invocations...