OLED interfearing with Rtc_Pcf8563

Good evening everyone. I am asking guidance for a problem that rose in my last project, an automated greenhouse on my arduino nano.
The problem itself is quite stupid as i had a functioning code, changed my OLED screen, from a 128x32 to a 128x64 (half the screen is yellow on black, half cyan on black), and got stuck.

If i include both the OLED and the RTC clock module (each with the correct i2c adresses) the code gets stuck everytime i try to write on the screen (if i put it in loop it gets stuck as soon as it gets there working to the last line before the display.print).
If i comment out every display.print or the whole RTC part, the program works fine on the serial. I though about a i2c interference or voltage problem, but when the same setup is limited to the OLED and RTC alone (printing date and hour on the screen and serial) it works perfectly fine, with the same adresses and wiring. Strangely enough the rest of the code is identhical to the one working with the smaller OLED.
For anyone that needs it the full functionality of the greenhouse i simply have a nano connected to a DHT11 for temp and hum sensing and a water sensor to sense water outside the enclosure. I have an external PSU powering a small water pump and a led array made of 5 led circuits (one per color) with two small cooling fans. Each of these lines is controlled by the nano through transistors. To control the led array and the pump i have a 4x4 button pad connected to a single analog pin and a "safety measure" in the water sensor which can turn off the pump if it detects water

The new OLED

The old OLED
https://www.amazon.it/GeeekPi-Display-Pollici-SSD1306-Raspberry/dp/B0832QD4WR/ref=sr_1_19?__mk_it_IT=ÅMÅŽÕÑ&dchild=1&keywords=oled+64x128&qid=1585761775&refinements=p_76%3A490210031&rnid=490209031&rps=1&sr=8-19

The RTC module
https://it.aliexpress.com/i/4000779628690.html?spm=a2g0y.search0305.3.16.32984ca64Q6fI8&ws_ab_test=searchweb0_0,searchweb201602_0,searchweb201603_0,ppcSwitch_0&algo_pvid=a999c195-2511-472c-9009-dc5c043d9d15&algo_expid=a999c195-2511-472c-9009-dc5c043d9d15-2
I hope someone can help me find the issue t-t

This is the code

#include <Rtc_Pcf8563.h>
#include <SimpleDHT.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //(pin A5 - SCK / A4 - SDA)
Rtc_Pcf8563 rtc; //the wiring only require 5v, GND and two analog channels, A4 for SDA and A5 for SCK
#define PCF8563address 0x51
const byte pinDHT11 = 8;
SimpleDHT11 dht11(pinDHT11);
const byte red = 5;
const byte white = 6;
const byte fr = 9;
const byte green = 10;
const byte blue = 11;
const byte Fans = 3;  
const byte pinPompa = 4;
const byte pinAllarm= A1;
const int z = 600; //Limit value for the water sensor
byte temperature = 0; //set the initial T & H of the DHT and write on the serial if it worked
byte humidity = 0;
int err = SimpleDHTErrSuccess;

void setup() {

  pinMode(Fans, OUTPUT); 
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fr, OUTPUT);
  pinMode(white, OUTPUT);
  pinMode(pinPompa, OUTPUT);
  digitalWrite(Fans, LOW);
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  digitalWrite(fr, LOW);
  digitalWrite(white, LOW);
  digitalWrite(pinPompa, LOW);
    
  Serial.begin(9600); //inizio seriale
  
  rtc.initClock(); //start the RTC
  rtc.setDate(29, 1, 3, 0, 20); //day, weekday, month, century (0-1900,1-2000), year (0-99)
  rtc.setTime(02, 00, 00); //hr, min, sec
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //start the screen
  display.clearDisplay(); 
  display.setTextSize(1);  
  display.setTextColor(WHITE, BLACK); //set the screen so that spaces turn black and overwrite the values (needed for cleaning rows separately
  display.setCursor(0,16);
  display.print("LED OFF");
  display.display(); 
  delay(500);
}

void loop() {

   int value = analogRead(A0); //Read the values of the button pad
   const int valuemap = map (value, 0, 440, 0, 30); /* values for a 5v input
   0     40    79   113
  164   193   221   247
  284   306   327   347
  375   392   408   423
  mapped to
   0   2   5   7
  11  13  15  16
  19  20  22  23
  25  26  27  28  
  */
 //each map value gets a case. pwm range between 0 and 255//
      switch (valuemap) {
  case 0:
  analogWrite(red, 0);
  analogWrite(green, 0);
  analogWrite(blue, 0);
  analogWrite(fr, 0);
  analogWrite(white, 0);
  analogWrite(Fans, 0);
    break;
  case 2:
  analogWrite(red, 60);
  analogWrite(green, 60);
  analogWrite(blue, 60);
  analogWrite(fr, 60);
  analogWrite(white, 60);
  analogWrite(Fans, 120);
    break;
  case 5:
  analogWrite(red, 120);
  analogWrite(green, 120);
  analogWrite(blue, 120);
  analogWrite(fr, 120);
  analogWrite(white, 120);
  analogWrite(Fans, 190);
    break;
  case 7:
  analogWrite(red, 240);
  analogWrite(green, 240);
  analogWrite(blue, 240);
  analogWrite(fr, 240);
  analogWrite(white, 240);
  analogWrite(Fans, 240);
    break;
  case 11:
  analogWrite(red, 60);
  analogWrite(green, 0);
  analogWrite(blue, 0);
  analogWrite(fr, 60);
  analogWrite(white, 60);
  analogWrite(Fans, 60);
    break;
  case 13:
  analogWrite(red, 0);
  analogWrite(green, 60);
  analogWrite(blue, 60);
  analogWrite(fr, 0);
  analogWrite(white, 60);
  analogWrite(Fans, 60);
    break;
  case 15:
 digitalWrite(pinPompa, HIGH);
    break;
  case 16:
 digitalWrite(pinPompa, LOW);
    break;
}


  //write on the serial if DHT11 worked
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("DHT11 Failed Read"); Serial.println(err);delay(1000);
    return;
    }
//serial print all values
  Serial.print("Data: ");
  Serial.println(rtc.formatDate());
  Serial.print("Ora: ");
  Serial.println(rtc.formatTime());
  Serial.println("Valori: ");
  Serial.print("Temperatura=");
  Serial.print((int)temperature); Serial.println("*C"); //dht library already maps the values
  Serial.print("Umidità Aria=");
  Serial.print((int)humidity); Serial.println("%H");
 


//Display printing. 2 color oled display from 0 to 15 yellow, divided into 2 full rows at 0 and at 8, while after 16 its cyan, with rows at  16, 24, 32, 40, 48 & 56 without spaces
//print RTC values
  display.setCursor(0,0); 
  display.print("Data: "); //print the date and hour to the screen
  display.setCursor(50,0); 
  display.println(rtc.formatDate());
  display.setCursor(0,8); 
  display.print("Ora: ");
  display.println(rtc.formatTime());
  display.setCursor(50,8); 
  display.display(); 
  //print LED state
        switch (valuemap) {
  /*default:

    break;*/
 case 0:
  Serial.println("LED 0");
  display.setCursor(0,16); 
  display.print("                                                                   "); //these are to clean the led state row only when a new state has been set
  display.setCursor(0,16);
  display.println("LED 0");
    break;
  case 2: 
  Serial.println("LED +");
  display.setCursor(0,16); 
  display.print("                                                                   ");
  display.setCursor(0,16);
  display.println("LED +");
    break;
  case 5: 
  Serial.println("LED ++");
  display.setCursor(0,16); 
  display.print("                                                                   ");
  display.setCursor(0,16);
  display.println("LED ++");
    break;
  case 7:
  Serial.println("LED +++");
  display.setCursor(0,16); 
  display.print("                                                                   ");
  display.setCursor(0,16);
  display.println("LED +++");
    break;
  case 11: 
  Serial.println("LED R-FR-W");
  display.setCursor(0,16); 
  display.print("                                                                   "); 
  display.setCursor(0,16);
  display.println("LED R-FR-W");
    break;
  case 13:
  Serial.println("LED G-B-W");
  display.setCursor(0,16); 
  display.print("                                                                   ");
  display.setCursor(0,16);
  display.println("LED G-B-W");
    break;
  }

//print DHT11 values
  display.setCursor(0,26); 
  display.println("T(C):"); 
  display.setCursor(50,26);
  display.println((int)temperature);//Stringa da visualizzare
  display.setCursor(0,36);  //Sposta il cursore di lato per non sovrapporre le stringhe
  display.println("H(%):"); 
  display.setCursor(50,36);
  display.println((int)humidity); 

 //print pump & allarm values
  int allarm = analogRead(pinAllarm);
    if (allarm>z) {
  Serial.println("Acqua Rilevata");
  display.setCursor(0,46); 
  display.println("Allarme:");
  display.setCursor(50,46);
  display.println("ON");
  digitalWrite(pinPompa, LOW); //if the water sensor sense water outside the enclosure, turn the pump off
 }
 else if (allarm<z) {
  Serial.println("Acqua Non Rilevata");
  display.setCursor(50,46); 
  display.println("Allarme:");
  display.setCursor(50,46);
  display.println("OFF");
 }
  int motore=digitalRead(pinPompa);
if (motore == HIGH) {
  Serial.println("Pompa ON");
  display.setCursor(0,56);
  display.println("Pompa:"); 
  display.setCursor(50,56);
  display.println("ON"); 
}
else if (motore==LOW){
  Serial.println("Pompa OFF");
  display.setCursor(0,56); 
  display.println("Pompa:"); 
  display.setCursor(50,56);
  display.println("OFF"); 
}

  delay (1000); //minimum delay for DHT11 to work properly
  display.display(); 
  display.setCursor(0,0); 
  display.print("                      "); //to clean the display, without cleaning the LED state row
  display.setCursor(0,8); 
  display.print("                     ");
  display.setCursor(0,26); 
  display.print("                                                           ");
  display.setCursor(0,36); 
  display.print("                                                           ");
  display.setCursor(0,46); 
  display.print("                                                           ");
  display.setCursor(0,56); 
  display.print("                                                           ");

}

You have lots of anonymous strings e.g.

Serial.print("DHT11 Failed Read");

replace all of these literal strings with F() e.g.

Serial.print(F("DHT11 Failed Read"));

This will save you lots of SRAM that is wasted when anonymous data is copied to SRAM.

David.

It works! i have included all the literal strings in F() and also fused the two cases together (dont know why i didn't it sooner) and saved some more ram

The new code is the following:

#include <Rtc_Pcf8563.h>
#include <SimpleDHT.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //(pin A5 - SCK / A4 - SDA)
Rtc_Pcf8563 rtc; //the wiring only require 5v, GND and two analog channels, A4 for SDA and A5 for SCK
#define PCF8563address 0x51
const byte pinDHT11 = 8;
SimpleDHT11 dht11(pinDHT11);
const byte red = 5;
const byte white = 6;
const byte fr = 9;
const byte green = 10;
const byte blue = 11;
const byte Fans = 3;  
const byte pinPompa = 4;
const byte pinAllarm= A1;
const int z = 600; //Limit value for the water sensor
byte temperature = 0; //set the initial T & H of the DHT and write on the serial if it worked
byte humidity = 0;
int err = SimpleDHTErrSuccess;

void setup() {

  pinMode(Fans, OUTPUT); 
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fr, OUTPUT);
  pinMode(white, OUTPUT);
  pinMode(pinPompa, OUTPUT);
  digitalWrite(Fans, LOW);
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  digitalWrite(fr, LOW);
  digitalWrite(white, LOW);
  digitalWrite(pinPompa, LOW);
    
  Serial.begin(9600); //inizio seriale
  
  rtc.initClock(); //start the RTC
  rtc.setDate(29, 1, 3, 0, 20); //day, weekday, month, century (0-1900,1-2000), year (0-99)
  rtc.setTime(02, 00, 00); //hr, min, sec
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //start the screen
  display.clearDisplay(); 
  display.setTextSize(2);  
  display.setTextColor(WHITE, BLACK); //set the screen so that spaces turn black and overwrite the values (needed for cleaning rows separately
  display.setCursor(0,16);
  display.print("SERRUINO");
  display.display(); 
  delay(2000);
  display.clearDisplay(); 
  display.setTextSize(1);  
  display.setTextColor(WHITE, BLACK); //set the screen so that spaces turn black and overwrite the values (needed for cleaning rows separately
  display.setCursor(0,16);
  display.print("LED OFF");
  display.display(); 
}

void loop() {
  //write on the serial if DHT11 worked
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print(F("DHT11 Failed Read")); Serial.println(err);delay(1000);
    return;
    }
//serial print all values
  Serial.println("===============================");
  Serial.print(F("Data: "));
  Serial.println(rtc.formatDate());
  Serial.print(F("Ora: "));
  Serial.println(rtc.formatTime());
  Serial.println(F("Valori: "));
  Serial.print(F("Temperatura="));
  Serial.print((int)temperature); Serial.println("*C"); //dht library already maps the values
  Serial.print(F("Umidità Aria="));
  Serial.print((int)humidity); Serial.println("%H");

//Display printing. 2 color oled display from 0 to 15 yellow, divided into 2 full rows at 0 and at 8, while after 16 its cyan, with rows at  16, 24, 32, 40, 48 & 56 without spaces
//print RTC values
  display.setCursor(0,0); 
  display.print("Data: "); //print the date and hour to the screen
  display.setCursor(50,0); 
  display.println(rtc.formatDate());
  display.setCursor(0,8); 
  display.print("Ora: ");
  display.setCursor(50,8);  
  display.println(rtc.formatTime());


//print DHT11 values
  display.setCursor(0,26); 
  display.println(F("T(C):")); 
  display.setCursor(50,26);
  display.println((int)temperature);//Stringa da visualizzare
  display.setCursor(0,36);  //Sposta il cursore di lato per non sovrapporre le stringhe
  display.println(F("H(%):")); 
  display.setCursor(50,36);
  display.println((int)humidity); 
  
   int value = analogRead(A0); //Read the values of the button pad
   const int valuemap = map (value, 0, 440, 0, 30); /* values for a 5v input
   0     40    79   113
  164   193   221   247
  284   306   327   347
  375   392   408   423
  mapped to
   0   2   5   7
  11  13  15  16
  19  20  22  23
  25  26  27  28  
  */
 //each map value gets a case. pwm range between 0 and 255. set the led state and print LED state
      switch (valuemap) {
  case 0:
  analogWrite(red, 0);
  analogWrite(green, 0);
  analogWrite(blue, 0);
  analogWrite(fr, 0);
  analogWrite(white, 0);
  analogWrite(Fans, 0);
  Serial.println(F("LED 0"));
  display.setCursor(0,16); 
  display.print("                "); //these are to clean the led state row only when a new state has been set
  display.setCursor(0,16);
  display.println(F("LED 0"));
    break;
  case 2:
  analogWrite(red, 60);
  analogWrite(green, 60);
  analogWrite(blue, 60);
  analogWrite(fr, 60);
  analogWrite(white, 60);
  analogWrite(Fans, 120);
  Serial.println(F("LED +"));
  display.setCursor(0,16); 
  display.print("                ");
  display.setCursor(0,16);
  display.println(F("LED +"));
    break;
  case 5:
  analogWrite(red, 120);
  analogWrite(green, 120);
  analogWrite(blue, 120);
  analogWrite(fr, 120);
  analogWrite(white, 120);
  analogWrite(Fans, 190);
  Serial.println(F("LED ++"));
  display.setCursor(0,16); 
  display.print("                ");
  display.setCursor(0,16);
  display.println(F("LED ++"));
    break;
  case 7:
  analogWrite(red, 240);
  analogWrite(green, 240);
  analogWrite(blue, 240);
  analogWrite(fr, 240);
  analogWrite(white, 240);
  analogWrite(Fans, 240);
  Serial.println(F("LED +++"));
  display.setCursor(0,16); 
  display.print("                ");
  display.setCursor(0,16);
  display.println(F("LED +++"));
    break;
  case 11:
  analogWrite(red, 60);
  analogWrite(green, 0);
  analogWrite(blue, 0);
  analogWrite(fr, 60);
  analogWrite(white, 60);
  analogWrite(Fans, 60);
  Serial.println(F("LED R-FR-W"));
  display.setCursor(0,16); 
  display.print("                ");
  display.setCursor(0,16);
  display.println(F("LED R-FR-W"));
    break;
  case 13:
  analogWrite(red, 0);
  analogWrite(green, 60);
  analogWrite(blue, 60);
  analogWrite(fr, 0);
  analogWrite(white, 60);
  analogWrite(Fans, 60);
  Serial.println(F("LED G-B-W"));
  display.setCursor(0,16); 
  display.print("                ");
  display.setCursor(0,16);
  display.println(F("LED G-B-W"));
    break;
  case 15:
 digitalWrite(pinPompa, HIGH);
    break;
  case 16:
 digitalWrite(pinPompa, LOW);
    break;
}
 //print pump & allarm values
  int allarm = analogRead(pinAllarm);
    if (allarm>z) {
  Serial.println(F("Acqua Rilevata"));
  display.setCursor(0,46); 
  display.println(F("Allarme:"));
  display.setCursor(50,46);
  display.println(F("ON"));
  digitalWrite(pinPompa, LOW); //if the water sensor sense water outside the enclosure, turn the pump off
 }
 else if (allarm<z) {
  Serial.println(F("Acqua Non Rilevata"));
  display.setCursor(0,46); 
  display.println(F("Allarme:"));
  display.setCursor(50,46);
  display.println(F("OFF"));
 }
  int motore=digitalRead(pinPompa);
if (motore == HIGH) {
  Serial.println(F("Pompa ON"));
  display.setCursor(0,56);
  display.println(F("Pompa:")); 
  display.setCursor(50,56);
  display.println(F("ON")); 
}
else if (motore==LOW){
  Serial.println(F("Pompa OFF"));
  display.setCursor(0,56); 
  display.println(F("Pompa:")); 
  display.setCursor(50,56);
  display.println(F("OFF")); 
}

  delay (1000); //minimum delay for DHT11 to work properly
  display.display(); 
  display.setCursor(0,0); 
  display.print("                      "); //to clean the display, without cleaning the LED state row
  display.setCursor(0,8); 
  display.print("                     ");
  display.setCursor(0,26); 
  display.print("                                                           ");
  display.setCursor(0,36); 
  display.print("                                                           ");
  display.setCursor(0,46); 
  display.print("                                                           ");
  display.setCursor(0,56); 
  display.print("                                                           ");

}

I would kiss you but i'm quarantined, so have my digital thanks <3