Program crash when trying to display on OLED

Hi guys, first time asking a question here, and quite new to coding & Arduino.

I have written a sketch for the "Arduino Sensor Kit". What the code does is reading all sensor values and then based on user input (via button) display either:

  1. The reading from an individual sensor
  2. Readings from all sensors at the same time
  3. Only the sensor reading that triggered an alarm

The alarm function can also be inhibited by the user using the same button.

Now, everything works great when printing on Serial interface, but the moment I want to display any of the above on the OLED 0.96" display, the program either stops or start printing garbage on the serial interface, without displaying anything on the OLED display.

If I remove all the "user interface" and just do a simple display on the OLED, then it works. I have tried many different approaches, but keep running into issues with the OLED.

Sketch attached.

Any assistance is appreciated.

Regards
Chris

Test_2.ino (14.4 KB)

I would first suspect that you're running out of memory - which Arduino are you using?

You can save some RAM using the F macro in situations like this:

Serial.print(F("Toggle alarmEnable before: "));

Can you get it working if you just display "Hello" on the display in setup?

Thanks for your quick reply.
I am using an Arduino UNO clone.
Displaying on OLED from Setup is working fine. I have also done the (F( suggestion and was able to display some info on the OLED display.
Looks like my issue is indeed memory related. Any suggestions on how to further reduce memory usage?

if (micLevel > 900)
    {
      Serial.print("micLevel"), Serial.print("-------------------"), Serial.println(micLevel);
    }

Why all the commas?

Serial.print("bmpPres"), Serial.print("----------------------case 1--"), You KNOW which case you're in, so why bother printing it?
Again, the commas.

Post the new version of your code.

There are some trivial savings to be made by using const byte for pin numbers instead of int.

You guys are geniuses! Thanks a lot!

I have removed all Serial.print from the sketch and minimized the text that is to be displayed on the OLED display.

Also did display.println**(F**(.... for OLED.

By doing so I was able to reduce, according to the compiler, Dynamic memory usage down to 33% from 41%.

Sketch is now working exactly as intended.

Thanks again! ;D ;D ;D ;D ;D :grin: :grin: :grin: :grin:

Chrisbad:
Thanks for your quick reply.
I am using an Arduino UNO clone.
Displaying on OLED from Setup is working fine. I have also done the (F( suggestion and was able to display some info on the OLED display.
Looks like my issue is indeed memory related. Any suggestions on how to further reduce memory usage?

Your posted program (no use of the F macro) when compiled for a UNO here;

Sketch uses 23836 bytes (73%) of program storage space. Maximum is 32256 bytes.
Global variables use 1115 bytes (54%) of dynamic memory, leaving 933 bytes for local variables. Maximum is 2048 bytes.

Looks just fine to me, plenty of free memory.

srnet:
Your posted program (no use of the F macro) when compiled for a UNO here;

Sketch uses 23836 bytes (73%) of program storage space. Maximum is 32256 bytes.

Global variables use 1115 bytes (54%) of dynamic memory, leaving 933 bytes for local variables. Maximum is 2048 bytes.




Looks just fine to me, plenty of free memory.

For some reason the moment my Dynamic memory usage is above 41% my sketch crashes.

Any idea why?

Some display libraries are memory hogs.

I know that some Adafruit libraries need a display buffer for those displays. Almost all the display functions operate on the buffer and then you call display.display() to send the bits to the physical OLED.

It may be that when you never called any of those functions that hit the buffer, the optimizer threw it away. Alternatively, it may be that that buffer is created by a malloc call that wouldn't be counted as part of your dynamic memory by the compiler.

Grab a copy of the freemem function if you're curious and see how much RAM you have at the end of setup.

wildbill:
Grab a copy of the freemem function if you're curious and see how much RAM you have at the end of setup.

In void setup
freeMemory()=256

Thanks guys, I've learnt a lot!

Chrisbad:
In void setup
freeMemory()=256

Case closed: you're out of memory :slight_smile:

If you've got a graphical display (not solely text), take the XY resolution of your display.
Multiply the two numbers together, and divide the result by 8.
Unless you've got a partial frame buffer (and consequently slower display updates) that number represents how many bytes the library probably reserves for its frame buffer AFTER the sketch has started.