How can i squeeze my code into a ATTiny 2313

Hi

I am a newbee in programing, i am trying to make a very simple countdown timer, that starts on a push of a button and runs for 2 min. when it finishes the display turnes off, and starts again when the button is pushed.

I am using a small OLED as a display.
This a the code i am using ( i found it on the internet and modified it )

#include <U8g2lib.h>

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 

unsigned long Watch, _micro, time = micros();
unsigned int Clock = 0, R_clock;
boolean Reset = false, Stop = false, Paused = false;
volatile boolean timeFlag = false;
unsigned long clickTime = 0;
unsigned long Timer = 0;
unsigned long Timer_sec = 0;
unsigned long Timer_min = 0;

 void setup(void) {
  SetTimer(0,2,0);
  StartTimer();
  u8g2.begin();
}

void loop()
{
   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_fub30_tf);
   
  CountDownTimer(); 

  if (TimeHasChanged() ) 
  {
    u8g2.setCursor(0, 32);
    u8g2.print(u8x8_u8toa(ShowMinutes(),2));
    u8g2.sendBuffer();
    u8g2.setCursor(44, 28);
    u8g2.print(":");
    u8g2.sendBuffer();
    u8g2.setCursor(62, 32);
    u8g2.print(u8x8_u8toa(ShowSeconds(),2));
    u8g2.sendBuffer();

    delay(1000);
  }
}

boolean CountDownTimer()
{
  static unsigned long duration = 1000000;
  timeFlag = false;

  if (!Stop && !Paused)
  {
    if ((_micro = micros()) - time > duration ) 
    {
      Clock--;
      timeFlag = true;

      if (Clock == 0)
        Stop = true;

      _micro < time ? time = _micro : time += duration; 
    }
  }
  return !Stop;
}


void ResetTimer()
{
  SetTimer(R_clock);
  Stop = false;
}

void StartTimer()
{
  Watch = micros();
  time = micros();
  Stop = false;
  Paused = false;
}

void StopTimer()
{
  Stop = true;
}

void StopTimerAt(unsigned int minutes, unsigned int seconds)
{
  if (TimeCheck(minutes, seconds) )
    Stop = true;
}


void SetTimer(unsigned int hours, unsigned int minutes, unsigned int seconds)
{
  unsigned int _S = (seconds / 60), _M = (minutes / 60);
  if(_S) minutes += _S;
  if(_M) hours += _M;

  Clock = (hours * 3600) + (minutes * 60) + (seconds % 60);
  R_clock = Clock;
  Stop = false;
}

void SetTimer(unsigned int seconds)
{
 Clock = seconds;
 R_clock = Clock;
 Stop = false;
}

int ShowMinutes()
{
  return (Clock / 60) % 60;
}

int ShowSeconds()
{
  return Clock % 60;
}

boolean TimeHasChanged()
{
  return timeFlag;
}

boolean TimeCheck(unsigned int minutes, unsigned int seconds) 
{
  return (minutes == ShowMinutes() && seconds == ShowSeconds());
}

My problem is that i am trying to upload it to an ATTiny 2313 8K ( as i need multiple timers ). but i am getting an error : "egion `text' overflowed by 8142 bytes" which i understand happens because my code is too big for the Attiny 2313.

Please is there a smart way to achieve what i want with a smaller/smarter code ? or should i buy a bigger Attiny ? .. if so please any suggestion which one i need to buy ?

Thanks.

Please is there a smart way to achieve what i want with a smaller/smarter code ?

Maybe by not using the U8g2 library but manually optimizing each routine but I doubt that you can save half of the size.

or should i buy a bigger Attiny ? .. if so please any suggestion which one i need to buy ?

Yes, even better: an ATmega328p. They are almost equally priced as the bigger ATtinys.

An ATtiny2313 has 2k of flash. There is no way that you're going to fit a sketch that is 8k over-sized.
Driving any sort of bitmap display is "expensive." Why do you want to use an ATtiny at all? As pylon points out, there is little cost difference between an ATtiny and an ATmega...

Thank you Guys ... i understand. Now i did a bit of research and i changed the librady, and edited the code. my code is now down to "7486 bytes" which is under the 8K

but i am still getting an error message that my code is too big, and for a reason i cannot understand it says that the maximum storage us 2048 ?! befor it was 8k

Build options changed, rebuilding all
Archiving built core (caching) in: /var/folders/yr/9bj60pxs3213q3_tjy7vg32h0000gn/T/arduino_cache_455735/core/core_ATTinyCore_avr_attinyx313_LTO_disable,chip_2313,clock_1internal,eesave_aenable,bod_disable,INITIALIZE_SECONDARY_TIMERS_0_94922930dbc0cffd80c57507a7795642.a

Sketch uses 7486 bytes (365%) of program storage space. Maximum is 2048 bytes.
Global variables use 279 bytes (217%) of dynamic memory, leaving -151 bytes for local variables. Maximum is 128 bytes.
Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.
Error compiling for board ATtiny2313/4313.

please what i am doing wrong ?

Check the datasheet for the 2313:

https://cdn.solarbotics.com/products/datasheets/doc2543.pdf

2K flash, 128 bytes of RAM. You simply have far more program than microcontroller here.

The ATTiny2313 is not an appropriate device for driving that sort of display - there is no way you will fit the code to drive that into 2k of flash. A larger ATTiny (one with 8k of flash, like the 84, or maybe 85 if that has enough pins; the tiny1634 could easily fit it). ATTiny devices with less than 8k of flash get really restrictive, and OLED displays require a lot of flash and RAM. You're also way over the limit on RAM too.

I understand now, Thanks a lot.