Is it possible to Instantiate more than one object of the same hardware? (OLED SSD1306)

I want to put two animations on one OLED SSD1306 display, one on default frequency, another on 1Mhz via I2C bus. In order to do that, i called two constructors from Adafruit_SSD1306.h:

Adafruit_SSD1306 disp1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 disp2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET, **clkDuring**);

In order to use these object, I have to call .begin() function in void setup():
.
.

disp1.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
disp2.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

.
When I want to show disp1 animation everything is fine, but when I want to show disp2 it doesn't work. What could be the broblem? Here is the full code:

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

#include "myLib.h"

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define clkDuring 500000UL
#define clkAfter 100000UL
Adafruit_SSD1306 disp1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 disp2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET, clkDuring);

#define SCREEN_ADDRESS 0x3C

Animation Anim;


int logo_width = Anim.frame_w();
int logo_height = Anim.frame_h();

int xx = (SCREEN_WIDTH - logo_width) / 2;
int yy = (SCREEN_HEIGHT - logo_height) / 2;

void setup() {
  Wire.begin();
  disp1.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  disp2.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  delay(2);
}

void loop() {
  for (int i = 0; i < A.num_of_frames(); i++) {
    disp1.clearDisplay();
    disp1.drawBitmap(xx, yy, A.frame(i), logo_width, logo_height, 1);
    disp1.display();
  }
}

I doubt that both displays will work on the same I2C address.
I think some have the ability to modify the default address.
If not possible, a I2C multiplexer could be used to run more than one device of the same address,
each on its own bus.

1 Like

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

Hello Whandall! It is ONE display with TWO objects, disp1 and disp2. Objects are created by calling constructors:

Adafruit_SSD1306 disp1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 disp2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET, clkDuring);

As you can see, main difference is parameter clkDuring, which tells the Arduino on which frequency I2C bus should work.

Thank you UKHeliBob!

Ok, so I misunderstood what you want to do.
Why on earth don't you want to run your display at the fastest possible frequency?
Why use two objects to manage one hardware?

This is my school project, and I am stuck here. Next would be to add a button which would choose between these two frequencies, but that would be peace of cake if I could solve this

Why don't you reconfigure the only one display you have?
There's probably an end() function, or calling begin again does not hurt.

If the reconfiguration can only be done via the constructor,
you could create the display dynamically, so you can destroy it and make a new one.

I don't think your two object approach will work.

And I still see no sense in using two different methods of access in the same sketch.

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