Datalogger code size issue

Hello There,

My project is to build a arduino nano based data logger with :

  • arduino nano
  • SPI sd card module
  • I2C bme280 module
  • little I2C oled screen module

For now i managed to get :

  • DATA from BME280 on oled screen
  • DATA from BME280 on the SD card file

but not both fuctions together due to the size apparently.

I have been struggeling for over 2 hours now, trying to reduce the size of my code.
You will see in the code I tried to comment out some of the strings i was creating.
I keep getting the same error message on IDE :

"
text section exceeds available space in boardSketch uses 31016 bytes (100%) of program storage space. Maximum is 30720 bytes.

Global variables use 1487 bytes (72%) of dynamic memory, leaving 561 bytes for local variables. Maximum is 2048 bytes.
Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.
Error compiling for board Arduino Nano.

"

Any help will be very much appreciated!
thank you,

This is my code:

#include <BME280I2C.h>
#include <string.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#include <SPI.h>
#include <SD.h>
int CS_PIN = 7;
Adafruit_SSD1306 display(OLED_RESET);
float sea_press = 101325;
BME280I2C bme;    // Default : forced mode, standby time = 1000 ms
void setup()
{
  Serial.begin(9600);
  Serial.println("STARTED");
  Wire.begin();
  ///////////////OLED DISPLAY INITIALISATION
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  Serial.println("welcome");
    display.clearDisplay();
    display.setTextSize(1.4);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println("welcome");
    display.display();
    delay(1000);
  ///////////////////BME280 initialisation
  while(!Serial) {} 
  while(!bme.begin())
  {
    Serial.println("Could not find BME280 sensor!");
    delay(1000);
  }
  // bme.chipID(); // Deprecated. See chipModel().
  switch(bme.chipModel())
  {
     case BME280::ChipModel_BME280:
       Serial.println("Found BME280 sensor! Success.");
       break;
     case BME280::ChipModel_BMP280:
       Serial.println("Found BMP280 sensor! No Humidity available.");
       break;
     default:
       Serial.println("Found UNKNOWN sensor! Error!");
  }
  //////////////////////SD CARD init
  Serial.print("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(CS_PIN)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
  }
//////////////////////////////////////////////////////////////////
void loop()
{
  //mesure des valeurs et calcul alt
  float temp(NAN), hum(NAN), pres(NAN);
  int timer = millis()/1000;
  bme.read(pres, temp, hum);
  float alt = ((pow((sea_press / pres), 1/5.257) - 1.0) * (temp + 273.15)) / 0.0065;
  float preshPa = pres/100;
  //conversion en caracteres affichables
  char temp_buff[5]; 
  char hum_buff[5];
  char pres_buff[5];
  char alt_buff[5];
  //char temp_disp_buff[20] = "Temp degC:";
  //char hum_disp_buff[17] = "HR:";
  //char pres_disp_buff[20] = "pressure hPa:";
  //char alt_disp_buff[20] = "altitude m:";
  // appending temp/hum to buffers
    dtostrf(temp,4,1,temp_buff);
    //strcat(temp_disp_buff,temp_buff);
    dtostrf(hum,2,0,hum_buff);
    //strcat(hum_disp_buff,hum_buff);
    dtostrf(preshPa,6,2,pres_buff);
    //strcat(pres_disp_buff,pres_buff);
    dtostrf(alt,4,0,alt_buff);
    //strcat(alt_disp_buff,alt_buff);
  // routine for displaying text for temp/hum readout
    display.clearDisplay();
    display.setTextSize(1.4);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println(temp_buff);
    display.println(hum_buff);
    display.println(pres_buff);
    display.println(alt_buff);
    display.display();
    delay(2000);
//writing on the SD card  
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println("time");
    dataFile.println(timer);
    dataFile.println("temp");
    dataFile.println(temp);
    dataFile.println("HR");
    dataFile.println(hum);
    dataFile.println("pres");
    dataFile.println(pres);
    dataFile.println("alt");
    dataFile.println(alt);
    dataFile.close();
    delay(1000);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}