Moving OLED code into class = it stops working. Serial output stops + no compiler warnings

Hello everyone,
I could really use some help. I've got a simplified script that I can not get working :

File : Bug.ino

#include "Screen.h"
Screen screen;

void loop(){
  screen.output();
  Serial.print("Loop");
  Serial.println();
}

void setup(){
  Serial.begin(115200);
}

Screen.h

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

class Screen
{
  private:
    Adafruit_SSD1306 display;
    unsigned long startTime = 0;

  public :
    Screen();
    void init();
    void output();
};

Screen.cpp

#include "Screen.h"

Screen::Screen() {
  init();
}
void Screen::init() {
  this->display = Adafruit_SSD1306(128, 64, &Wire, -1);
  this->display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
  this->display.setTextSize(1);
  this->display.setTextColor(SSD1306_WHITE);
}
void Screen::output(){
  this->display.setCursor(1, 1);
  this->display.print("TEST");
  this->display.display();
}

As the subject says, this compiles fine, no warnings/errors but it kills my serial output and doesn't display anything on the screen.

Below is a similar script which is very similar single page script which works fine for me. Makes me think it is a dependency issue or something about how I split into into multiple pages but I tried all different configurations of including files/dependencies above. Does anyone have any suggestions on how to get the top script working? I would appreciate it so much.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

class Screen
{
  private:
    Adafruit_SSD1306 display;
    unsigned long startTime = 0;

  public :
    Screen(){
       this->display = Adafruit_SSD1306(128, 64, &Wire, -1);
    }
    void init(){
        //Initialize the screen
        this->display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
        this->display.setTextSize(1);
        this->display.setTextColor(SSD1306_WHITE);
        this->display.clearDisplay();
        this->output(60,30,":)))))");
        this->display.display();
    }

    void output(int x, int y, String S){
      this->display.setCursor(x, y);
      this->display.print(S);
      this->display.display();
    }
};


Screen screen;

void setup(){
  screen.init();
}

void loop(){}

I'd try taking the init() method out of the constructor and invoke it directly in setup() in your .ino sketch.

All those this-> are redundant.

For the constructor you should use an initializer list.

https://www.cplusplus.com/reference/initializer_list/initializer_list/

    Screen() : display(128, 64, &Wire, -1), startTime(0) {  }

So that actually fixed the issue.

Any idea why?

Because it's the proper way to initialize objects, constants and references?

Some elments can not be assigned to, but they can be initialized.

It is also to do with the constructor being invoked before the Arduino environment (via its own init() function) is set up. Anything you do in this phase (such as configuring timers etc.) could be undone later.

Thanks both of you for your help. I really appreciate it. I will be moving to an initializer list as well.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.