Too big program to fit in arduino nanon flash memory

Hello....
I have an arduino nano and I'm trying to build a project with two DHT22 sensors , one RTC module (DS3231) , a microSD module and an 0,96'' OLED 128x32 monitor.
I wrote a program to read the temps and humidities from the two sensors and store them in a microsd card.The terminal output shows the date,time,temp1,hum1,temp2,hum2 and stores the values just fine.
The problem is that when I want to see this values in the OLED display , the program reaches 97% of the total flash memory of arduino nano and I can't upload it.
If I skip the last lines that stores the data in microsd card , the program is about 87% and I can upload it just fine.
Is there any way to make the program more compact to fit to memory?
I asume that the library of the OLED display reserves some amount of memory for buffer purposes so I can't fit my program in the flash memory.
If there is no way to make the program more compact to fit , should I go to a ESP8266 chip?
Will the code run without changes or I have to learn to program in this new chip?
The code is below

#include "RTClib.h"

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

File myFile;

RTC_DS3231 rtc;

char cl[32];

#include "DHT.h"

#define DHTPIN1 4     // Digital pin connected to the DHT sensor
#define DHTPIN2 5     // Digital pin connected to the DHT sensor
#define DHTTYPE1 DHT22   // DHT 22  (AM2302), AM2321
#define DHTTYPE2 DHT22   // DHT 22  (AM2302), AM2321
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);

void setup(){
Serial.begin(9600);

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();
 
   if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }

  dht1.begin();
  dht2.begin();
  Wire.begin();

  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
 
if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
}

if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    //rtc.adjust(DateTime(2019, 9, 11, 12, 40, 0));
  }

// Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = dht1.readHumidity();
  float h2 = dht2.readHumidity();
  // Read temperature as Celsius (the default)
  float t1 = dht1.readTemperature();
  float t2 = dht2.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f1 = dht1.readTemperature(true);
  float f2 = dht2.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h1) || isnan(t1) || isnan(f1)) {
    Serial.println(F("Failed to read from DHT1 sensor!"));
    return;
  }
if (isnan(h2) || isnan(t2) || isnan(f2)) {
    Serial.println(F("Failed to read from DHT2 sensor!"));
    return;
  }
DateTime now = rtc.now();

   sprintf(cl, "%02d/%02d/%02d %02d:%02d:%02d",  now.day(), now.month(), now.year(),now.hour(), now.minute(), now.second()); 
 
  //Serial.print(F("Date/Time: "));
  //Serial.print(cl);
 
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(cl); 
  float hif1 = dht1.computeHeatIndex(f1, h1); // Compute heat index in Fahrenheit (the default)
  float hif2 = dht2.computeHeatIndex(f2, h2); // Compute heat index in Fahrenheit (the default)
  display.print(F("H1: "));
  display.print(h1);
  display.print(F("% "));
  display.print(F("T1: "));
  display.print(t1);
  display.println(F("C"));
  display.print(F("H2: "));
  display.print(h2);
  display.print(F("% "));
  display.print(F("T2: "));
  display.print(t2);
  //display.print(F("C"));
  //display.print("  Temp2(RTC) : ");
  //display.print(rtc.getTemperature());
  //display.print("C");
  display.display();
 
  Serial.print(F(" Humidity: "));
  //Serial.print(h);
  Serial.print(F("%  Temp1: "));
  //Serial.print(t);
  Serial.print(F("C "));
  Serial.print("  Temp2(RTC) : ");
  Serial.print(rtc.getTemperature());
  Serial.print("C");
   
    if (now.second() == 00 ) {
 
  myFile = SD.open("test.txt", FILE_WRITE);
  myFile.print(cl);
  myFile.print(" , ");
  myFile.print(h1);
  myFile.print(" , ");
  myFile.print(t1);
  myFile.print(" , ");
  myFile.print(h2);
  myFile.print(" , ");
  myFile.print(t2);
  myFile.println(rtc.getTemperature());
  myFile.close();
   
    Serial.print("  MicroSD REC ");
  }
  Serial.println();
      delay(1000);
}

OLED is very large because it works with graphics, try compiling using lcd hd44780 to see if OLED is worth keeping. If you need more capacity, you also have the STM32 line, can search BluePill to start.

Or you need to make (or find) a smaller OLED library

Hey Stanley, I had the same problem. There is a library called SSD1306ASCII on github that should help you. I took that library and trimmed it a bit more and added character scaling so I could also output larger characters (up to 8 times the 5x7 single size). Using the original graphics library I created a simple "hello world" sketch that used 10.5K and 1366 bytes of ram. Replacing that with my modified ASCII library, the sketch shrunk to 5.6K and only 48 bytes of ram.

[Reducing memory usage of SSD1306 0.96" OLED SPI on UNO/atmega328 - Displays - Arduino Forum](http://Reducing memory usage of SSD1306 0.96" OLED SPI on UNO/atmega328)

Thanks a lot.I will try the smaller library first to see if that helps.
As about blupill I checked that is similar to nano with greater flash memory.Its interesting although I think I should learn more about ESP8266 to deside if it is more worth for future projects.

Yes, ESP8266 is interesting, but it's the first version, see ESP32, which is more likely to expand in the future.

What many manufacturers do is separate the display controller from another additional microcontroller.

Thanks again for your help.I apreciate it

Do you really need that graphic library?
Getting something useful from the SSD1306 OLED - Displays - Arduino Forum uses text libraries. I imagine these apply to your display.

Nick_Pyner:
Do you really need that graphic library?
Getting something useful from the SSD1306 OLED - Displays - Arduino Forum uses text libraries. I imagine these apply to your display.

No actually I don't neet it.Only plain text I want to display.I will check the library from the link you provide.
Thanks man!