reduce program size

Hi Folks,

I got a arduino nano the issue is that the sketch size is currently 30222 and Im not finished still need to implement a relay and a photo cell. Already did some size reduction in the libraries. but I appreciate any idea for sketch size reduction.

#include "U8glib.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc; //realtime cloc

const int chipSelect = 4; //SD card

#include <SdFat.h> //SD card
SdFat sd;  //SD card
SdFile myFile; //SD card

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);	// I2C display

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
String dataString;
 
void setup(void)
{
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  Serial.begin(9600);  
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  rtc.begin();

  sensors.begin();

}
 
 
void loop(void)
{

  dataString = "";
  DateTime now = rtc.now();
  sensors.requestTemperatures();
    if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    //sd.errorHalt("opening test.txt for write failed");
  }  
       
    dataString += (now.year(), DEC);
    dataString += "/";
    dataString += (now.month(), DEC);
    dataString += "/";
    dataString += (now.day(), DEC);
    dataString += "/";
    dataString += (now.hour(), DEC);
    dataString += " ";
    dataString += (now.minute(), DEC);
    dataString += ":";
    dataString +=(now.second(), DEC);
    dataString += " ";
    dataString += (sensors.getTempCByIndex(0));
    dataString += "-";
    dataString += (sensors.getTempCByIndex(1));
   
    draw();
          
    myFile.print(dataString);
    myFile.println("");
    myFile.close();
/*
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("failed opening test.txt");
  }
  Serial.println("test.txt:");

  // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) Serial.write(data);
  // close the file:
  myFile.close();
  */
  
  delay(15000);  
}


//int freeRam () {
//  extern int __heap_start, *__brkval;
//  int v;
//  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
//}

void draw(void) { 
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 25, 10, "COOP INFO");
  
  //u8g.setPrintPos(15, 30);
 // u8g.print(freeRam());
  u8g.setPrintPos(35, 50);
  u8g.print(dataString);
}

Which functionality do you want to get rid of?

You can save a few kilobytes by getting rid of all your String definitions and changing those to char arrays. Next, the Ug8 library is pretty large and you might try removing some of the fonts you are not using. I can't remember if there is a way to selectively call in just one font when you use it. You might search that topic to see if someone else has a solution.

You might try changing from

u8g.setFont(u8g_font_unifont); to u8g.setFont(u8g_font_unifontr);

That basically uses just the 'english' characters in the font and excludes all the accented characters (at least thats my understanding).

I would've tried it myself, but I dont run half the libraries you're using :slight_smile:

ps. using u8g_font_helvB12r instead of u8g_font_helvB12 saves me about 4k.