Display errors for 2 OLED I2Cs

Trying to get 2 oleds going on my Nano, and I'm running into primary expression errors.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SD.h>
#include <Bounce2.h>

// Constants
Adafruit_SSD1306 display1(-1,128, 64);
Adafruit_SSD1306 display2(-1,128, 64);


#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS1 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define SCREEN_ADDRESS2 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define LEFT_ENCODER_CLK 2
#define LEFT_ENCODER_DT 3
#define LEFT_ENCODER_BTN 4
#define RIGHT_ENCODER_CLK 5
#define RIGHT_ENCODER_DT 6
#define RIGHT_ENCODER_BTN 7
#define RESET_BUTTON 16
#define BUTTON_3D6 17
#define BUTTON_STATS 15
#define SD_CS 4
#define display1
#define display2


// Objects
Bounce resetButton = Bounce();
Bounce button3d6 = Bounce();
Bounce statsButton = Bounce();

// Variables
const char *diceTypes[] = { "D20", "D12", "D10", "D8", "D6", "D4", "D0" };
const char *statsList[] = { "Ht", "IQ", "Str", "Ht", "Will", "Dex", "Obs" };
int diceSelection = 0;
int numDice = 0;
int modifier = 0;
bool d6Only = false;
long lastActivity = 0;

void displayMainMenu() {
  display1.clearDisplay();
  display1.setCursor(0, 0);
  display1.print("D6 Only? Yes/No");
  display1.display();
}

void roll3d6() {
  int rolls[3];
  int total = 0;
  for (int i = 0; i < 3; i++) {
    rolls[i] = random(1, 7);
    total += rolls[i];
  }
  display2.clearDisplay2();
  display2.setCursor(0, 0);
  display2.print("3D6 Roll Total: ");
  display2.println(total);
  display2.display2();
}

void displayStats() {
  display1.clearDisplay1();
  display1.setCursor(0, 0);
  display1.print("Stats Menu");
  display1.display1();
}

void handleEncoders() {
  // Implement rotary encoder handling for dice selection and menu navigation
}

void handleButtons() {
  if (button3d6.fell()) {
    roll3d6();
  }
  if (statsButton.fell()) {
    displayStats();
  }
}

void setup() {
  pinMode(LEFT_ENCODER_CLK, INPUT);
  pinMode(LEFT_ENCODER_DT, INPUT);
  pinMode(LEFT_ENCODER_BTN, INPUT_PULLUP);
  pinMode(RIGHT_ENCODER_CLK, INPUT);
  pinMode(RIGHT_ENCODER_DT, INPUT);
  pinMode(RIGHT_ENCODER_BTN, INPUT_PULLUP);
  pinMode(RESET_BUTTON, INPUT_PULLUP);
  pinMode(BUTTON_3D6, INPUT_PULLUP);
  pinMode(BUTTON_STATS, INPUT_PULLUP);

  resetButton.attach(RESET_BUTTON);
  resetButton.interval(5);
  button3d6.attach(BUTTON_3D6);
  button3d6.interval(5);
  statsButton.attach(BUTTON_STATS);
  statsButton.interval(5);

  if (!display1.begin(0x3C, OLED_RESET) || !display2.begin(0x3D, OLED_RESET)) {
    while (true)
      ;  // Stop if displays aren't found
  }

  display1.clearDisplay1();
  display1.display1();
  display2.clearDisplay2();
  display2.display2();

  if (!SD.begin(SD_CS)) {
    d6Only = true;  // Disable certain functionality if SD card is absent
  }

  displayMainMenu();
}

void loop() {
  resetButton.update();
  button3d6.update();
  statsButton.update();

  // Handle inactivity timeout
  if (millis() - lastActivity > 30000) {
    displayMainMenu();
  }

  // Reset button
  if (resetButton.fell()) {
    displayMainMenu();
  }

  // Handle other buttons and encoders
  handleEncoders();
  handleButtons();
}

Both display1 and display2 will attempt to allocate 1K of RAM at runtime. Since a Nano uses an ATmega328P, which has only 2K of RAM in total, the second allocation attempt will always fail. You need to either use a board with a processor that has more than 2K of RAM, or use a different library that doesn't allocate a 1K buffer for each screen.

These two lines cause the major problem. Not sure what you are trying to do, but this effectively deletes the text "display1" and "display2" wherever it appears in the sketch.

#define display1
#define display2

The function names clearDisplay(), display(), etc, remain the same regardless of the name you give to the particular display instance.

  display1.clearDisplay1();
  display1.display1();
  display2.clearDisplay2();
  display2.display2();
1 Like

Thanks ,I'll modify this.

copy, thanks. I'll look at different libraries!

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