Connection problem between Arduino Nano and OLED I2C display

Hello,
I'm currently trying to get a 0.92" OLED screen to work in one of my projects.
My main program is quite large, so I use another program to test the functionality individually and modify it as needed.
I have a problem with the screen. I can't connect to it from the main program. However, I have no problem to make it work individually when I connect to it with the test program.
I don't understand what's going on. Both programs are the same and yet it doesn't work.

Here is my main program :

// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Program      : Arduino IDE 2.0.3
// Device       : Arduino Nano
// Title        : Control of the Kin-Ball electronic scoreboard
// Author       : Yannick Blaser
// Created on   : 09.01.2023 (DD.MM.YYY)
// Last update  : 
// Description  : This program is used to control the Kin-Ball electronic scoreboard developped by Yannick Blaser.
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


// π—Ÿπ—œπ—•π—₯𝗔π—₯𝗬𝗦
  #include <Adafruit_NeoPixel.h>     //   <β€’ LED strips
  #include <Wire.h>                  //   <┐
  #include <Adafruit_GFX.h>          //   <β”Ό Screen on control panel
  #include <Adafruit_SSD1306.h>      //   <β”˜
  #include <EEPROM.h>                //   <β€’ Points recording


// π—šπ—Ÿπ—’π—•π—”π—Ÿ 𝗩𝗔π—₯π—œπ—”π—•π—Ÿπ—˜π—¦
  // Internal variables
  #define RED                      0       // Value of RED for the LED strips (0-255)
  #define GREEN                    0       // Value of green for the LED strips (0-255)
  #define BLUE                     0       // Value of blue for the LED strips (0-255)
  #define WHITE                    255     // Value of white for the LED strips (0-255)
  #define Brightness               255     // Value of brightness for the LED strips (0-255)
  #define LED_COUNT_match          21      // Number of LEDs per match display
  #define LED_COUNT_period         14      // Number of LEDs per period display
  
  // Inputs
  #define PIN_Button_Left          0       // Digital I/O number of left button         <┐
  #define PIN_Button_Middle        1       // Digital I/O number of middle button       <─
  #define PIN_Button_Right         2       // Digital I/O number of right button        <β”Ό CONTROL PANEL
  #define PIN_Button_Back          3       // Digital I/O number of "Cancel" button     <─
  #define PIN_Button_Reset         14      // Digital I/O number of "Reset" button      <─
  #define PIN_Switch_Match_Period  4       // Digital I/O number of switch              <β”˜
  
  // Outputs
  #define PIN_Led_Tens_Left        5       // Digital I/O number of match tens points display      <┐
  #define PIN_Led_Unit_Left        6       // Digital I/O number of match units points display     <β”Ό LEFT DISPLAY
  #define PIN_Led_Period_Left      11      // Digital I/O number of period points display          <β”˜
  #define PIN_Led_Tens_Middle      7       // Digital I/O number of match tens points display      <┐
  #define PIN_Led_Unit_Middle      8       // Digital I/O number of match units points display     <β”Ό MIDDLE DISPLAY
  #define PIN_Led_Period_Middle    12      // Digital I/O number of period points display          <β”˜
  #define PIN_Led_Tens_Right       9       // Digital I/O number of match tens points display      <┐
  #define PIN_Led_Unit_Right       10      // Digital I/O number of match units points display     <β”Ό RIGHT DISPLAY
  #define PIN_Led_Period_Right     13      // Digital I/O number of period points display          <β”˜

  // Screen size
  #define Screen_Width             128     // OLED display width, in pixels
  #define Screen_Height            64      // OLED display height, in pixels

  // Variables for points recording
  #define Adress_Match_Left        1       // Adress on EEPROM of match points      <┬ LEFT DISPLAY
  #define Adress_Period_Left       4       // Adress on EEPROM of period points     <β”˜
  #define Adress_Match_Middle      2       // Adress on EEPROM of match points      <┬ MIDDLE DISPLAY
  #define Adress_Period_Middle     5       // Adress on EEPROM of period points     <β”˜
  #define Adress_Match_Right       3       // Adress on EEPROM of match points      <┬ RIGHT DISPLAY
  #define Adress_Period_Right      6       // Adress on EEPROM of period points     <β”˜




// 𝗩𝗔π—₯π—œπ—”π—•π—Ÿπ—˜π—¦ 𝗒𝗙 𝗔𝗗𝗗π—₯π—˜π—¦π—¦π—”π—•π—Ÿπ—˜ π—Ÿπ—˜π——π—¦
  // Declaration of LED strips
  Adafruit_NeoPixel DISPLAY_Match_Tens_Left    (LED_COUNT_match,  PIN_Led_Tens_Left,     NEO_GRBW + NEO_KHZ800);     // Declaration of match tens points LED strip      <┐
  Adafruit_NeoPixel DISPLAY_Match_Units_Left   (LED_COUNT_match,  PIN_Led_Unit_Left,     NEO_GRBW + NEO_KHZ800);     // Declaration of match units points LED strip     <β”Ό LEFT DISPLAY
  Adafruit_NeoPixel DISPLAY_Period_Left        (LED_COUNT_period, PIN_Led_Period_Left,   NEO_GRBW + NEO_KHZ800);     // Declaration of period points LED strip          <β”˜
  Adafruit_NeoPixel DISPLAY_Match_Tens_Middle  (LED_COUNT_match,  PIN_Led_Tens_Middle,   NEO_GRBW + NEO_KHZ800);     // Declaration of match tens points LED strip      <┐
  Adafruit_NeoPixel DISPLAY_Match_Units_Middle (LED_COUNT_match,  PIN_Led_Unit_Middle,   NEO_GRBW + NEO_KHZ800);     // Declaration of match units points LED strip     <β”Ό MIDDLE DISPLAY
  Adafruit_NeoPixel DISPLAY_Period_Middle      (LED_COUNT_period, PIN_Led_Period_Middle, NEO_GRBW + NEO_KHZ800);     // Declaration of period points LED strip          <β”˜
  Adafruit_NeoPixel DISPLAY_Match_Tens_Right   (LED_COUNT_match,  PIN_Led_Tens_Right,    NEO_GRBW + NEO_KHZ800);     // Declaration of match tens points LED strip      <┐
  Adafruit_NeoPixel DISPLAY_Match_Units_Right  (LED_COUNT_match,  PIN_Led_Unit_Right,    NEO_GRBW + NEO_KHZ800);     // Declaration of match units points LED strip     <β”Ό RIGHT DISPLAY
  Adafruit_NeoPixel DISPLAY_Period_Right       (LED_COUNT_period, PIN_Led_Period_Right,  NEO_GRBW + NEO_KHZ800);     // Declaration of period points LED strip          <β”˜

  // Color variables for the LED strips
  uint32_t COLOR_Match_Tens_Left     = DISPLAY_Match_Tens_Left.Color    (RED, GREEN, BLUE, WHITE);     // Color of match tens points      <┐
  uint32_t COLOR_Match_Units_Left    = DISPLAY_Match_Units_Left.Color   (RED, GREEN, BLUE, WHITE);     // Color of match units points     <β”Ό LEFT DISPLAY
  uint32_t COLOR_Period_Left         = DISPLAY_Period_Left.Color        (RED, GREEN, BLUE, WHITE);     // Color of period points          <β”˜
  uint32_t COLOR_Match_Tens_Middle   = DISPLAY_Match_Tens_Middle.Color  (RED, GREEN, BLUE, WHITE);     // Color of match tens points      <┐
  uint32_t COLOR_Match_Units_Middle  = DISPLAY_Match_Units_Middle.Color (RED, GREEN, BLUE, WHITE);     // Color of match units points     <β”Ό MIDDLE DISPLAY
  uint32_t COLOR_Period_Middle       = DISPLAY_Period_Middle.Color      (RED, GREEN, BLUE, WHITE);     // Color of period points          <β”˜
  uint32_t COLOR_Match_Tens_Right    = DISPLAY_Match_Tens_Right.Color   (RED, GREEN, BLUE, WHITE);     // Color of match tens points      <┐
  uint32_t COLOR_Match_Units_Right   = DISPLAY_Match_Units_Right.Color  (RED, GREEN, BLUE, WHITE);     // Color of match units points     <β”Ό RIGHT DISPLAY
  uint32_t COLOR_Period_Right        = DISPLAY_Period_Right.Color       (RED, GREEN, BLUE, WHITE);     // Color of period points          <β”˜
  

// 𝗩𝗔π—₯π—œπ—”π—•π—Ÿπ—˜π—¦ 𝗒𝗙 𝗦𝗖π—₯π—˜π—˜π—‘
  // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  Adafruit_SSD1306 display(Screen_Width, Screen_Height, &Wire, -1);


// 𝗣π—₯π—’π—šπ—₯𝗔𝗠 𝗩𝗔π—₯π—œπ—”π—•π—Ÿπ—˜π—¦
  // Counters variables
  int Counter_Match_Left          = 0;     // General counter for match points     <┐
  int Counter_Match_Tens_Left     = 0;     // Counter for match tens points        <β”Ό LEFT DISPLAY COUNTERS
  int Counter_Match_Units_Left    = 0;     // Counter for match units points       <─
  int Counter_Period_Left         = 0;     // Counter for period points            <β”˜
  int Counter_Match_Middle        = 0;     // General counter for match points     <┐
  int Counter_Match_Tens_Middle   = 0;     // Counter for match tens points        <β”Ό MIDDLE DISPLAY COUNTERS
  int Counter_Match_Units_Middle  = 0;     // Counter for match units points       <─
  int Counter_Period_Middle       = 0;     // Counter for period points            <β”˜
  int Counter_Match_Right         = 0;     // General counter for match points     <┐
  int Counter_Match_Tens_Right    = 0;     // Counter for match tens points        <β”Ό RIGHT DISPLAY COUNTERS
  int Counter_Match_Units_Right   = 0;     // Counter for match units points       <─
  int Counter_Period_Right        = 0;     // Counter for period points            <β”˜

  // Variables for rising edges detection
  bool ButtonState_Left;           //   <┬ LEFT BUTTON
  bool LastButtonState_Left;       //   <β”˜
  bool ButtonState_Middle;         //   <┬ MIDDLE BUTTON
  bool LastButtonState_Middle;     //   <β”˜
  bool ButtonState_Right;          //   <┬ RIGHT BUTTON
  bool LastButtonState_Right;      //   <β”˜
  bool ButtonState_Back;           //   <┬ CANCEL BUTTON
  bool LastButtonState_Back;       //   <β”˜
  bool ButtonState_Reset;          //   <┬ RESET BUTTON
  bool LastButtonState_Reset;      //   <β”˜
  bool SwitchState;                //   <β€’ SWITCH

  // Variables for blinking
  bool Blink_Left    = 0;     //   <β€’ LEFT DISPLAY
  bool Blink_Middle  = 0;     //   <β€’ MIDDLE DISPLAY
  bool Blink_Right   = 0;     //   <β€’ RIGHT DISPLAY



//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {

  //Initialisation of communication with the Arduino
  Serial.begin(9600);

  // Declaration of pins for inputs and outputs
  pinMode(PIN_Button_Left,         INPUT);
  pinMode(PIN_Button_Middle,       INPUT);
  pinMode(PIN_Button_Right,        INPUT);
  pinMode(PIN_Button_Back,         INPUT);
  pinMode(PIN_Button_Reset,        INPUT);
  pinMode(PIN_Switch_Match_Period, INPUT);


// 𝗦𝗖π—₯π—˜π—˜π—‘
  Serial.println("Tentative de connexion avec l'Γ©cran...");

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){  // Address 0x3C for 128x64
    Serial.println(F("Erreur de communication avec le chipset SSD1306... Arret du programme."));
    for(;;);
  }
  else{
    Serial.println(F("Initialisation du controleur SSD1306 reussi."));
  }

  display.clearDisplay();          // Clearing the screen
  display.setTextSize(3);          // Setting screen text size
  display.setTextColor(WHITE);     // Setting screen color
  

// π—Ÿπ—˜π—— 𝗦𝗧π—₯π—œπ—£π—¦
  // Starting communication with LED strips
  DISPLAY_Match_Tens_Left.begin();
  DISPLAY_Match_Units_Left.begin();
  DISPLAY_Period_Left.begin();
  DISPLAY_Match_Tens_Middle.begin();
  DISPLAY_Match_Units_Middle.begin();
  DISPLAY_Period_Middle.begin();
  DISPLAY_Match_Tens_Right.begin();
  DISPLAY_Match_Units_Right.begin();
  DISPLAY_Period_Right.begin();

  // Setting LED strips brightness
  DISPLAY_Match_Tens_Left.setBrightness(Brightness);
  DISPLAY_Match_Units_Left.setBrightness(Brightness);
  DISPLAY_Period_Left.setBrightness(Brightness);
  DISPLAY_Match_Tens_Middle.setBrightness(Brightness);
  DISPLAY_Match_Units_Middle.setBrightness(Brightness);
  DISPLAY_Period_Middle.setBrightness(Brightness);
  DISPLAY_Match_Tens_Right.setBrightness(Brightness);
  DISPLAY_Match_Units_Right.setBrightness(Brightness);
  DISPLAY_Period_Right.setBrightness(Brightness);

  // Display current settings on all LED strips
  DISPLAY_Match_Tens_Left.show();
  DISPLAY_Match_Units_Left.show();
  DISPLAY_Period_Left.show();
  DISPLAY_Match_Tens_Middle.show();
  DISPLAY_Match_Units_Middle.show();
  DISPLAY_Period_Middle.show();
  DISPLAY_Match_Tens_Right.show();
  DISPLAY_Match_Units_Right.show();
  DISPLAY_Period_Right.show();


// π—£π—’π—œπ—‘π—§π—¦ π—₯π—˜π—–π—’π—₯π——π—œπ—‘π—š
  // Clear memory
  for (int i = 0; i < EEPROM.length(); i++){
    EEPROM.update(i, 0);
  }

  Serial.println("Memory cleared");
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop() {

  Serial.println("Starting loop");

  // Reading of inputs
  ButtonState_Left    = digitalRead(PIN_Button_Left);
  ButtonState_Middle  = digitalRead(PIN_Button_Middle);
  ButtonState_Right   = digitalRead(PIN_Button_Right);
  ButtonState_Back    = digitalRead(PIN_Button_Back);
  ButtonState_Reset   = digitalRead(PIN_Button_Reset);
  SwitchState         = digitalRead(PIN_Switch_Match_Period);


// π—–π—›π—”π—‘π—šπ—˜ 𝗒𝗙 𝗠𝗔𝗧𝗖𝗛 π—£π—’π—œπ—‘π—§π—¦

  Serial.println("Match points :");

  if (SwitchState == false){

    // Increase points for LEFT DISPLAY
    if ( (ButtonState_Left != LastButtonState_Left) && (ButtonState_Left == HIGH) && (Counter_Match_Left < 99) ) {
      if ( (Blink_Middle == false) && (Blink_Right == false) ){      // If LED strips are currently not blinking, they'll start blinking
        Blink_Middle = !Blink_Middle;
        Blink_Right  = !Blink_Right;
      }
      else if ( (Blink_Middle == true) && (Blink_Right == true) ){   // If LED strips are currently blinking, they'll stop blinking, the current points will be stored in the memory and then the counter will increase by 1
        Blink_Middle = !Blink_Middle;
        Blink_Right  = !Blink_Right;
        EEPROM.update(Adress_Match_Middle, Counter_Match_Middle);
        EEPROM.update(Adress_Match_Right,  Counter_Match_Right);
        Counter_Match_Middle++;
        Counter_Match_Right++;
      }
    }

    Serial.println("Left OK,");

    // Increase points for MIDDLE DISPLAY
    if ( (ButtonState_Middle != LastButtonState_Middle) && (ButtonState_Middle == HIGH) && (Counter_Match_Middle < 99) ) {
      if ( (Blink_Right == false) && (Blink_Left == false) ){        // If LED strips are currently not blinking, they'll start blinking
        Blink_Right = !Blink_Right;
        Blink_Left  = !Blink_Left;
      }
      else if ( (Blink_Right == true) && (Blink_Left == true) ){     // If LED strips are currently blinking, they'll stop blinking, the current points will be stored in the memory and then the counter will increase by 1
        Blink_Right = !Blink_Right;
        Blink_Left  = !Blink_Left;
        EEPROM.update(Adress_Match_Right, Counter_Match_Right);
        EEPROM.update(Adress_Match_Left,  Counter_Match_Left);
        Counter_Match_Right++;
        Counter_Match_Left++;
      }
    }

    Serial.println("Middle OK,");

    // Increase points for RIGHT DISPLAY
    if ( (ButtonState_Right != LastButtonState_Right) && (ButtonState_Right == HIGH) && (Counter_Match_Right < 99) ) {
      if ( (Blink_Left == false) && (Blink_Middle == false) ){       // If LED strips are currently not blinking, they'll start blinking
        Blink_Left   = !Blink_Left;
        Blink_Middle = !Blink_Middle;
      }
      else if ( (Blink_Left == true) && (Blink_Middle == true) ){    // If LED strips are currently blinking, they'll stop blinking, the current points will be stored in the memory and then the counter will increase by 1
        Blink_Left   = !Blink_Left;
        Blink_Middle = !Blink_Middle;
        EEPROM.update(Adress_Match_Left,   Counter_Match_Left);
        EEPROM.update(Adress_Match_Middle, Counter_Match_Middle);
        Counter_Match_Left++;
        Counter_Match_Middle++;
      }
    }

    Serial.println("Right OK,");

    // Decrease points
    if ( (ButtonState_Back != LastButtonState_Back) && (ButtonState_Back == HIGH) ) {
      if ( (Blink_Left == true) || (Blink_Middle == true) || (Blink_Right == true) ){             // If any LED strip is currently blinking, they'll all stop blinking
        Blink_Left   = !Blink_Left;
        Blink_Middle = !Blink_Middle;
        Blink_Right  = !Blink_Right;
      }
      else if ( (Blink_Left == false) && (Blink_Middle == false) && (Blink_Right == false) ){     // If all LED strips are currently not blinking, the previous points will be restored form the memory
        Counter_Match_Left   = EEPROM.read(Adress_Match_Left);
        Counter_Match_Middle = EEPROM.read(Adress_Match_Middle);
        Counter_Match_Right  = EEPROM.read(Adress_Match_Right);
      }
    }

    // Reset points
    if ( (ButtonState_Reset != LastButtonState_Reset) && (ButtonState_Reset == HIGH) ) {
      Counter_Match_Left   = 0;
      Counter_Match_Middle = 0;
      Counter_Match_Right  = 0;
      EEPROM.update(Adress_Match_Left,   Counter_Match_Left);
      EEPROM.update(Adress_Match_Middle, Counter_Match_Middle);
      EEPROM.update(Adress_Match_Right,  Counter_Match_Right);
    }
  }  


// π—–π—›π—”π—‘π—šπ—˜ 𝗒𝗙 π—£π—˜π—₯π—œπ—’π—— π—£π—’π—œπ—‘π—§π—¦

  Serial.println("Period points:");

  if (SwitchState == true){

    // Increasing points for LEFT DISPLAY
    if ( (ButtonState_Left != LastButtonState_Left) && (ButtonState_Left == HIGH) && (Counter_Match_Left < 9) ) {           // The current points are stored in the memory and then the counter increase by 1
      EEPROM.update(Adress_Period_Left, Counter_Period_Left);
      Counter_Period_Left++;
    }

    Serial.println("Left OK,");

    // Increasing points for MIDDLE DISPLAY
    if ( (ButtonState_Middle != LastButtonState_Middle) && (ButtonState_Middle == HIGH) && (Counter_Match_Middle < 9) ) {   // The current points are stored in the memory and then the counter increase by 1
      EEPROM.update(Adress_Period_Middle, Counter_Period_Middle);
      Counter_Period_Middle++;
    }

    Serial.println("Middle OK,");

    // Increasing points for RIGHT DISPLAY
    if ( (ButtonState_Right != LastButtonState_Right) && (ButtonState_Right == HIGH) && (Counter_Match_Right < 99) ) {      // The current points are stored in the memory and then the counter increase by 1
      EEPROM.update(Adress_Period_Right, Counter_Period_Right);
      Counter_Period_Right++;
    }

    Serial.println("Right OK,");

    // Decrease points
    if ( (ButtonState_Back != LastButtonState_Back) && (ButtonState_Back == HIGH) ) {
      Counter_Period_Left   = EEPROM.read(Adress_Period_Left);
      Counter_Period_Middle = EEPROM.read(Adress_Period_Middle);
      Counter_Period_Right  = EEPROM.read(Adress_Period_Right);
    }

    // Reset points
    if ( (ButtonState_Reset != LastButtonState_Reset) && (ButtonState_Reset == HIGH) ) {
      Counter_Period_Left   = 0;
      Counter_Period_Middle = 0;
      Counter_Period_Right  = 0;
      EEPROM.update(Adress_Period_Left,   Counter_Period_Left);
      EEPROM.update(Adress_Period_Middle, Counter_Period_Middle);
      EEPROM.update(Adress_Period_Right,  Counter_Period_Right);
    }
  }


//π——π—œπ—¦π—£π—Ÿπ—”π—¬ 𝗒𝗙 𝗗𝗔𝗧𝗔 & π—’π—§π—›π—˜π—₯ π—”π—–π—§π—œπ—’π—‘π—¦
  //Tave the current buttons state as the last state, for next time through the loop
  LastButtonState_Left    = ButtonState_Left;
  LastButtonState_Middle  = ButtonState_Middle;
  LastButtonState_Right   = ButtonState_Right;
  LastButtonState_Back    = ButtonState_Back;
  LastButtonState_Reset   = ButtonState_Reset;

  // Calculation of the value of each counter
  Counter_Match_Tens_Left    = Counter_Match_Left/10;
  Counter_Match_Units_Left   = Counter_Match_Left-(Counter_Match_Tens_Left*10);
  Counter_Match_Tens_Middle  = Counter_Match_Middle/10;
  Counter_Match_Units_Middle = Counter_Match_Middle-(Counter_Match_Tens_Middle*10);
  Counter_Match_Tens_Right   = Counter_Match_Right/10;
  Counter_Match_Units_Right  = Counter_Match_Right-(Counter_Match_Tens_Right*10);

  // Display of each value on the screen
  display.clearDisplay();               // Clearing the screen

  display.setTextSize(2);
  display.setCursor(0,0);
  display.println("N");
  display.setCursor(58,0);
  display.println("G");
  display.setCursor(116,0);
  display.println("B");
  display.setTextSize(3);
  
    // Left number
    display.setCursor(0, 25);
    display.println(Counter_Match_Left);
    
    // Middle number                    // Adjusting the position of the number so that it is centered on the screen
    if (Counter_Match_Middle < 10){
      display.setCursor(56, 25);
    }
    else if (Counter_Match_Middle >= 10){
      display.setCursor(47, 25);
    }
    display.println(Counter_Match_Middle);

    // Right number                     // Adjusting the position of the number so that it is aligned to the right of the screen
    if (Counter_Match_Right < 10){
      display.setCursor(110, 25);
    }
    else if (Counter_Match_Right >= 10){
      display.setCursor(92, 25);
    }
    display.println(Counter_Match_Right);

  display.display();

  // Display of each value on the corresponding LED strips
  Display_Tens  (DISPLAY_Match_Tens_Left,    Counter_Match_Tens_Left,    COLOR_Match_Tens_Left,    Blink_Left);
  Display_Units (DISPLAY_Match_Units_Left,   Counter_Match_Units_Left,   COLOR_Match_Units_Left,   Blink_Left);
  Display_Units (DISPLAY_Period_Left,        Counter_Period_Left,        COLOR_Period_Left,        false);
  Display_Tens  (DISPLAY_Match_Tens_Middle,  Counter_Match_Tens_Middle,  COLOR_Match_Tens_Middle,  Blink_Middle);
  Display_Units (DISPLAY_Match_Units_Middle, Counter_Match_Units_Middle, COLOR_Match_Units_Middle, Blink_Middle);
  Display_Units (DISPLAY_Period_Middle,      Counter_Period_Middle,      COLOR_Period_Middle,      false);
  Display_Tens  (DISPLAY_Match_Tens_Right,   Counter_Match_Tens_Right,   COLOR_Match_Tens_Right,   Blink_Right);
  Display_Units (DISPLAY_Match_Units_Right,  Counter_Match_Units_Right,  COLOR_Match_Units_Right,  false);
  Display_Units (DISPLAY_Period_Right,       Counter_Period_Right,       COLOR_Period_Right,       Blink_Right);

  DISPLAY_Match_Tens_Left.show();
  DISPLAY_Match_Units_Left.show();
  DISPLAY_Period_Left.show();
  DISPLAY_Match_Tens_Middle.show();
  DISPLAY_Match_Units_Middle.show();
  DISPLAY_Period_Middle.show();
  DISPLAY_Match_Tens_Right.show();
  DISPLAY_Match_Units_Right.show();
  DISPLAY_Period_Right.show();


  Serial.print("Blink state : ");
  Serial.print(Blink_Right);
  Serial.print("       ");
  Serial.print("Counter : ");
  Serial.print(Counter_Match_Right);
  Serial.print("       ");
  Serial.print("Button + state : ");
  Serial.print(ButtonState_Right);
  Serial.print("       ");
  Serial.print("Button - state : ");
  Serial.println(ButtonState_Back);

  delay(50);
}

And here is my test programm :

  #include <Adafruit_NeoPixel.h>     //   <β€’ LED strips
  #include <Wire.h>                  //   <┐
  #include <Adafruit_GFX.h>          //   <β”Ό Screen on control panel
  #include <Adafruit_SSD1306.h>      //   <β”˜
  #include <EEPROM.h>                //   <β€’ Points recording



  #define PIN_Button_Left          16       // Digital I/O number of left button         <┐
  #define PIN_Button_Middle        15       // Digital I/O number of middle button       <─
  #define PIN_Button_Right         2       // Digital I/O number of right button        <β”Ό CONTROL PANEL
  #define PIN_Button_Back          3       // Digital I/O number of "Cancel" button     <─
  #define PIN_Button_Reset         14      // Digital I/O number of "Reset" button      <─
  #define PIN_Switch_Match_Period  4       // Digital I/O number of switch              <β”˜

  #define Screen_Width             128     // OLED display width, in pixels
  #define Screen_Height            64      // OLED display height, in pixels

  Adafruit_SSD1306 display(Screen_Width, Screen_Height, &Wire, -1);

  bool ButtonState_Left = false;           //   <┬ LEFT BUTTON
  bool ButtonState_Middle = false;         //   <┬ MIDDLE BUTTON
  bool ButtonState_Right = false;          //   <┬ RIGHT BUTTON
  bool ButtonState_Back = false;           //   <┬ CANCEL BUTTON
  bool ButtonState_Reset = false;          //   <┬ RESET BUTTON
  bool SwitchState;                //   <β€’ SWITCH

void setup() {
  Serial.begin(9600);
  pinMode(PIN_Button_Left,         INPUT);
  pinMode(PIN_Button_Middle,       INPUT);
  pinMode(PIN_Button_Right,        INPUT);
  pinMode(PIN_Button_Back,         INPUT);
  pinMode(PIN_Button_Reset,        INPUT);
  pinMode(PIN_Switch_Match_Period, INPUT);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){  // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  display.clearDisplay();          // Clearing the screen
  display.setTextSize(3);          // Setting screen text size
  display.setTextColor(WHITE);     // Setting screen color

}

void loop() {
  
  ButtonState_Left    = digitalRead(PIN_Button_Left);
  ButtonState_Middle  = digitalRead(PIN_Button_Middle);
  ButtonState_Right   = digitalRead(PIN_Button_Right);
  ButtonState_Back    = digitalRead(PIN_Button_Back);
  ButtonState_Reset   = digitalRead(PIN_Button_Reset);
  SwitchState         = digitalRead(PIN_Switch_Match_Period);

  display.clearDisplay();
  
  display.setTextSize(2);
  display.setCursor(0,0);
  display.println("N");
  display.setCursor(58,0);
  display.println("G");
  display.setCursor(116,0);
  display.println("B");
  display.setTextSize(3);

  
  display.setCursor(0,16);
  display.println(ButtonState_Right);

  display.setCursor(56,16);
  display.println(ButtonState_Middle);
  
  display.setCursor(110,16);
  display.println(ButtonState_Left);

  display.setCursor(0,40);
  display.println(SwitchState);

  display.setCursor(56,40);
  display.println(ButtonState_Back);

  display.setCursor(110,40);
  display.println(ButtonState_Reset);

  display.display();


}

Does anyone have any idea what might be causing this problem?

Can you be more specific?

Are you saying it fails here?
I use a Nano with my 1306's; the driver is quite memory-hungry. What do you get for compiler results for memory usage when your big code compiles?

Yeah, it fails here.

Here is what it get for compiler results :
"The sketch uses 20106 bytes (65%) of the programs storage space. The maximum is 30720 bytes.
Global variables use 1003 bytes (48%) of dynamic memory, leaving 1045 bytes for local variables. The maximum is 2048 bytes."

The Arduino can connect to the OLED screen if I use the test programm, but it fail to connect when I use my main programm.

Are you using different I/O pins in the two sketches?

There is your problem, the Adafruit library will allocate 1024 bytes of dynamic ram at run-time (for a 128 x 64 display), leaving only 21 bytes, which is completely insufficient.

Oh gosh, I guess that's not good. Is there a way to allocate more memory or am I really in trouble?

@blaserya

You need to have a library that uses less memory. I use ss_oled(see the library manager), and I tweaked the file to remove more of the fonts. The result is a much improved usage footprint, at the cost of having only two fonts, the small and normal sizes. Since I'm using it for text, it gives me 21cx8r with the small font, or 16cx4r with the normal font.

Ok thanks, I'll try that.
Would it be possible for you to provide me with a code in which you use this library ? It would help me a lot to understand how to use it.

I'm not sure to know how to do that, but I'll probably figure it out.

I'll respond in a couple of hours, I'm not at that computer right now. I started with the example code that installs with that library, it's pretty self-explanatory. More on the library reduction later.

There is effectively a difference, but it's because I forgot to modify them in the main code.

In my main code, this :

  #define PIN_Button_Left          0       // Digital I/O number of left button
  #define PIN_Button_Middle        1       // Digital I/O number of middle button

...Should be this :

  #define PIN_Button_Left          16      // Digital I/O number of left button
  #define PIN_Button_Middle        15      // Digital I/O number of middle button

Ok no problem, thank you so much for your help !

You may already be aware of this, if so I apologise. In general, on a Nano, don't use pins 0 and 1 (RX and TX) for anything. Reserve them for the debugging use of Serial Monitor. There are few enough debugging options in a Nano, you don't need to cripple the only decent one. I've gone so far in one case as to build a timer into setup that allows me to release those two pins for other uses if I don't send a character from Serial monitor in the first couple of seconds after reset, but that's tricky, and only really worthwile if you're really desperate. If you do use them for something, study the Nano schematic to verify that you're not overloading, or otherwise degrading the performance of the two pins.

I wasn't aware of this when I first created my code. I discovered it afterward and that's why I changed it to 15 and 16 in my test code, but I forgot to change it in my main code.

1 Like

Okay. Here's my test code. Bare bones. The original code is Larry Banks' demo for avr, comes with the library. I haven't visited the .cpp file to see if there's any streamlining that can be done; I'm sure there are gains, but I doubt one can save more than a percent or two, as the compiler/linker optimizes away any unused functions, so the only gain is if one streamlines a particular function to reduce it's overall size, and some of that may happen during the compile anyway. The primary trick is to stick to using one font size, but as can be seen, the gains are small. I tested by commenting out the various defines at the top. The library creates the stretched font by manipulating one of the other two.

These results are different from my earlier quoted results, as I streamlined the test and removed some errors. Results for me were:
only small font, 19% (6014 bytes) program, 14% (301 bytes) Ram,
only normal font, 20% (6222 bytes) program, 14% (301 bytes) Ram,
only stretched font, 21% (6454 bytes) program, 14% (301 bytes) Ram,
small and normal fonts, 22% (6992 bytes) program, 14% (301 bytes) Ram,
small, stretched, and normal fonts, 23% (7342 bytes) program, 14% (301 bytes) Ram
Hope this helps.

//
// Small Simple OLED library test for AVR platform, for font usage impact
// without line drawing function (saving of 1K RAM)
//
#include <ss_oled.h>

#define SS  //enables small font use
#define NN  //enables normal font use
#define TT  //enables stretched font use

SSOLED ssoled;
#define SDA_PIN -1
#define SCL_PIN -1
// no reset pin needed
#define RESET_PIN -1
// let ss_oled find the address of our display
#define OLED_ADDR 0x3c
#define FLIP180 1
#define INVERT 0
// Use the default Wire library
#define USE_HW_I2C 1
const char tstring[] = "12345678";

void setup()
{
  int rc;
  rc = oledInit(&ssoled, OLED_128x64, OLED_ADDR, FLIP180, INVERT, USE_HW_I2C, SDA_PIN, SCL_PIN, RESET_PIN, 400000L);       // Standard HW I2C bus at 400Khz
//all messaging for missing OLED and type stripped out
}

void loop()
{
#ifdef SS
oledWriteString(&ssoled, 0, 0, 0, tstring, FONT_SMALL, 0, 1);
#endif
#ifdef NN
    oledWriteString(&ssoled, 0, 0, 2, tstring, FONT_NORMAL, 0, 1);
#endif
#ifdef TT
    oledWriteString(&ssoled, 0, 0, 4, tstring, FONT_STRETCHED, 0, 1);
#endif
}

I now use the ss_oled library instead of the Adafruit_SSD1306 library and everything works fine now.
Thank you so much for all your help !

my side, I did not know know ss_oled but instead of Adafruit_SSD1306 + wire + GFX, I ONLY use SD1306AsciiAvrI2c. It's awesome

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