Help: Binary sketch size too big! (OLED Display code)

Hi!

I want to use this code to control an OLED display with an Arduino Uno, but it shows me the following errors:

Binary sketch size: 35832 bytes (of a 30720 byte maximum)
processing.app.debug.RunnerException: Sketch too big; see
http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it
at processing.app.Sketch.size(Unknown Source)
at processing.app.Sketch.build(Unknown Source)
at processing.app.Sketch.build(Unknown Source)
at processing.app.Editor$DefaultRunHandler.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)

This is the code

//----------------------------------------//
//-----------1.5inch 262k OLED -----------//
//-For Arduino Mega / 328 / Gadgeteer-----//
//---------128x128 pixel SPI Mode---------//
//----------------------------------------//
//---------------www.wide.hk--------------//

#include <SPI.h>
#include "image.h"

//Arduino Mega1280 : Gadgeteer
//SDA=51 //Gadgeteer PIN7
//SCK=52 //Gadgeteer PIN9
int RES = 50; //Gadgeteer PIN4
int DC = 49; //Gadgeteer PIN5
int CS = 53; //Gadgeteer PIN6

// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

//SPI Initialization
void spi_init(void)
{
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV2);//SCK frequency
SPI.setDataMode(SPI_MODE3);
}

void Write_Command(unsigned char COM)
{
digitalWrite(CS,LOW);
digitalWrite(DC,LOW);
SPI.transfer(COM);
digitalWrite(DC,HIGH);
digitalWrite(CS,HIGH);
}

void Write_Data(unsigned char DATA)
{
digitalWrite(CS,LOW);
digitalWrite(DC,HIGH);
SPI.transfer(DATA);
digitalWrite(DC,HIGH);
digitalWrite(CS,HIGH);
}

void Set_Address(unsigned char a,unsigned char b,unsigned char c,unsigned char d)
{
Write_Command(0x15);//set column address
Write_Data(a);//start address
Write_Data(b);//end address

Write_Command(0x75);//set row address
Write_Data(c);//start address
Write_Data(d);//end address
}

void SSD13xx(void)
{
Write_Command(0xfd);//set command lock
Write_Data(0x12);
Write_Command(0xfd);//set command lock
Write_Data(0xb1);
Write_Command(0xae);//display off
Write_Command(0x15);//set column address
Write_Data(0x00);
Write_Data(0x7f);
Write_Command(0x75);//set row address
Write_Data(0x00);
Write_Data(0x7f);
Write_Command(0x5c);//set write ram
Write_Command(0x5d);//set read ram
Write_Command(0xa0);//set re-map&color depth
Write_Data(0x74);
Write_Command(0xa1);//set display start line
Write_Data(0x00);
Write_Command(0xa2);//set display offset
Write_Data(0x00);
Write_Command(0xa6);//set display mode
Write_Command(0xab);//funcion selection
Write_Data(0x01);
Write_Command(0xaf);//set display on
Write_Command(0xb1);//set phase length
Write_Data(0x32);
Write_Command(0xb3); //display clock divider / oscillator frequency
Write_Data(0xf1);
Write_Command(0xb4);//set segment low voltage
Write_Data(0xa0);
Write_Data(0xb5);
Write_Data(0x55);
Write_Command(0xb5);//set gpio
Write_Data(0x0a);
Write_Command(0xb6);//set second pre-charge period
Write_Data(0x01);
Write_Command(0xb8);//gamma look up table
Write_Data(0x02);
Write_Data(0x03);
Write_Data(0x04);
Write_Data(0x05);
Write_Data(0x06);
Write_Data(0x07);
Write_Data(0x08);
Write_Data(0x09);
Write_Data(0x0a);
Write_Data(0x0b);
Write_Data(0x0c);
Write_Data(0x0d);
Write_Data(0x0e);
Write_Data(0x0f);
Write_Data(0x10);
Write_Data(0x11);
Write_Data(0x12);
Write_Data(0x13);
Write_Data(0x15);
Write_Data(0x17);
Write_Data(0x19);
Write_Data(0x1b);
Write_Data(0x1d);
Write_Data(0x1f);
Write_Data(0x21);
Write_Data(0x23);
Write_Data(0x25);
Write_Data(0x27);
Write_Data(0x2a);
Write_Data(0x2d);
Write_Data(0x30);
Write_Data(0x33);
Write_Data(0x36);
Write_Data(0x39);
Write_Data(0x3c);
Write_Data(0x3f);
Write_Data(0x42);
Write_Data(0x45);
Write_Data(0x48);
Write_Data(0x4c);
Write_Data(0x50);
Write_Data(0x54);
Write_Data(0x58);
Write_Data(0x5c);
Write_Data(0x60);
Write_Data(0x64);
Write_Data(0x68);
Write_Data(0x6c);
Write_Data(0x70);
Write_Data(0x74);
Write_Data(0x78);
Write_Data(0x7d);
Write_Data(0x82);
Write_Data(0x87);
Write_Data(0x8c);
Write_Data(0x91);
Write_Data(0x96);
Write_Data(0x9b);
Write_Data(0xa0);
Write_Data(0xa5);
Write_Data(0xaa);
Write_Data(0xaf);
Write_Data(0xb4);
Write_Command(0xbb);//set pre-charge period
Write_Data(0x17);
Write_Command(0xbe);//set vcomh voltage
Write_Data(0x05);
Write_Command(0xc1);//set contrast current
Write_Data(0xc8);
Write_Data(0x80);
Write_Data(0xc8);
Write_Command(0xc7);//set master current control
Write_Data(0x0f);
Write_Command(0xca);//set multiplex ratio
Write_Data(0x7f);
Write_Command(0xaf);//set display on
}
//Fill color
void fill(unsigned char data1,unsigned char data2)
{
unsigned int i,j;

Set_Address(0x00,0x7f,0x00,0x7f);
Write_Command(0x5C);

for(i=0;i<128;i++)
{
for(j=0;j<128;j++)
{
Write_Data(data1);
Write_Data(data2);
}
}
}

void picture1()
{
unsigned int i,j;
unsigned int x=0;

Set_Address(0x00,0x7f,0x00,0x79);
Write_Command(0x5C);

for(i=0;i<123;i++)
{
for(j=0;j<128;j++)
{
ram_val=pgm_read_byte(&(show1[x++]));// Read from Flash memoery
Write_Data(ram_val);
ram_val=pgm_read_byte(&(show1[x++]));// Read from Flash memoery
Write_Data(ram_val);
}
}
}

/* void font_show()
{
unsigned int i,j;
unsigned int x=0;

Set_Address(0x00,0x7f,0x00,0x79);
Write_Command(0x5C);

for(i=0;i<5;i++)
{
for(j=0;j<7;j++)
{
ram_val=pgm_read_byte(&(font[x++]));// Read from Flash memoery
Write_Data(ram_val);
ram_val=pgm_read_byte(&(font[x++]));// Read from Flash memoery
Write_Data(ram_val);
}
}
}
*/

void setup()
{

pinMode(RES,OUTPUT);
pinMode(DC,OUTPUT);
pinMode(CS,OUTPUT);

//SPI Initialization
spi_init();
}
void loop()
{
//RESET
digitalWrite(RES,HIGH); delay(100);
digitalWrite(RES,LOW); delay(100);
digitalWrite(RES,HIGH); delay(100);

SSD13xx();

//line(0x3c,0x6f,10,20,15,40);
fill(0xf8,0x00);
fill(0x07,0xe0);
fill(0x00,0x1f);
fill(0x22,0x00);
fill(0x07,0xff);
fill(0xff,0xe0);
fill(0xf8,0x1f);
fill(0x00,0x00);

while(1)
{
//Show Image
picture1();
Serial.print("compression ");
}
}

I would really appreciate you help! Thank you :)!

what is in image.h?

bperrybap:
what is in image.h?

There are images of the numbers. This is supossed to show them (like a tachometer).

Unfortunately, the program you are using is written for a Mega (the Uno pins are different). The Mega has 248k of flash memory (after subtracting the 8k for the bootloader), while the Uno has 30.5K (after subtracting 0.5k for the bootloader). The image for the screen takes up too much flash memory, and it won't fit in the Uno.

sofy:

bperrybap:
what is in image.h?

There are images of the numbers. This is supossed to show them (like a tachometer).

ok, but what is REALLY in there?
i.e. how big is the data?

Hoi,

There are images of the numbers. This is supossed to show them (like a tachometer).

Did you tried already to optimize the images (mostly colour depth and size brings the most savings of space).
There are many free ImageOptimizers available - if not yet done: give one a try :smiley:

A.R.Ty:
Hoi,

There are images of the numbers. This is supossed to show them (like a tachometer).

Did you tried already to optimize the images (mostly colour depth and size brings the most savings of space).
There are many free ImageOptimizers available - if not yet done: give one a try :smiley:

Thank you! Which one do you recommend me? :slight_smile:

However, note that the image stored is the direct image sent to the LCD, and not jpg or some such. You might be able to use a simple run length type compression and decompression, but anything more complicated will take more program space (which is what overflowed in the first place) or more SRAM (also in short supply).

Perhaps there is a sketch made for an UNO that doesn't use the full 128x128 lcd size. Or perhaps, the simplest thing might be to buy a Mega for which the sketch was designed.

If you just want to run a test, comment out picture1(), and uncomment font_show(). Call font_show() instead of picture1() from your loop, and the sketch will probably be small enough to run on your UNO (The image at show1 won't be linked in if not used, and that is over 15KB of data.

Thank you! Which one do you recommend me? smiley

Not real one - take anything you want.
Most give you a preview of the result for the different resolutions and colour depths.