I2C - OLED display - lenght of code problem

Hello, I have a code that works perfectly for the Oled display 128x64 connected to two analog pins running on I2C (A4, A5). However some parts of the code have to be commented and then everything runs smoothly. As soon as I uncomment some parts the code the serial monitor says "SSD1306 allocation failed". It is not the fault of any specific code because I tried uncommenting some parts and commenting the ones that were not commented and everything was ok. Is there a chance that the oled display has some problems with reading longer codes?

I'm using these two libraries..

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

This is why the message pops up..

if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);

Thank you for your replies

Post the complete sketch if you want practical help.

The Adafruit_SSD1306 library allocates 1024 bytes for a 128x64 display at run time.
Which reduces the amount of SRAM for the rest of your program.

Make sure that you are using appropriate type and size of arrays.
Make sure that "anonymous text" strings are stored in Flash e.g.

    display.print("message is wasting SRAM");
    display.print(F("this message is stored in Flash"));

David.

I assume you are using a UNO, have you considered a MEGA, it has more RAM. You could also use some external memory to get more space and possibly save data in EEPROM.

So I replaced all my display.print() with display.print(F()) but a bunch of new errors popped up and now I'm confused..

I have attached the code. It should be understandable but some variables are named in czech.. just ignore that

Thank you for your replies

Display_programme.ino (20.2 KB)

I am using an UNO and unfortunately I have everything connected with a printed circuit board so I would rather stick with the UNO.

I don't have time to print another circuit board because this is a school project and I have to finish it in roughly a week.

Thank you for the idea tho

I have made several changes for you. Look for comments .kbv

  1. I installed Adafruit DHT_Sensor library from the IDE Library Manager.
  2. I had to change the constructor() name and the methods()
  3. I corrected your blind use of F() on variables. F() only works with "literal text"
  4. I changed lots more print("text") and println("text") statements.

I built for a Uno. It now has Global variables use 635 bytes (31%) of dynamic memory, leaving 1413 bytes for local variables.

Using library SPI at version 1.0 in folder: C:\Program Files (x86)\Arduino-1.8.9\hardware\arduino\avr\libraries\SPI 
Using library DHT_sensor_library at version 1.3.2 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\DHT_sensor_library 
Using library Wire at version 1.0 in folder: C:\Program Files (x86)\Arduino-1.8.9\hardware\arduino\avr\libraries\Wire 
Using library Adafruit_GFX_Library at version 1.7.5 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\Adafruit_GFX_Library 
Using library RF24 at version 1.3.1 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\RF24 
Using library Adafruit_SSD1306 at version 2.0.1 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\Adafruit_SSD1306 
Using library Adafruit_Unified_Sensor at version 1.0.3 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\Adafruit_Unified_Sensor 
"C:\\Program Files (x86)\\Arduino-1.8.9\\hardware\\tools\\avr/bin/avr-size" -A "C:\\Users\\DAVIDP~1\\AppData\\Local\\Temp\\arduino_build_240292/Daniel_DMP_ssd1306.ino.elf"
Sketch uses 24428 bytes (75%) of program storage space. Maximum is 32256 bytes.
Global variables use 635 bytes (31%) of dynamic memory, leaving 1413 bytes for local variables. Maximum is 2048 bytes.

Untested. But it compiles ok. And after Adafruit_SSD1306 has taken 1024 bytes of SRAM, you still have 389 bytes of SRAM left.

David.

p.s. You have a lot of duplicate code. It would be wise to simplify your sketch e.g. by putting repeated sequences into helper functions.

Daniel_DMP_ssd1306.ino (24.5 KB)

Thank you so much David you're an Arduino god..

The display works perfectly throughout all of the code. There are many duplicate parts because I didn't get to polishing out the code yet. As for the DHT thank you for the changes but the library is one that a friend of mine wrote and works very well too.

I strongly recommend using libraries supported by the IDE Library Manager.

There is nothing complex about DHT libraries. I am sure your friend's one works fine.

Did you understand the difference between print("text"); and print(F("text));

In the future, you can use the Editor ctrl-F command
to search for print(" and replace with print(F("
and then search for "); and replace with "));

You must be careful with the second search and replace. Make sure that you only replace on F() statements.

David.

Yeah evrerything is clear now thanks to you