low memory/streamlinging remedies?

Through some piece-meal and trial & error I have a code that compiles & runs as desired.

It passively monitors the optical density of a fluid as well as the air & fluid temps while also monitoring & regulating pH via addition of CO2 through a gate valve.

My concern is its using 21066 bytes and I’m getting a low memory/stability warning.

I’d still consider myself still pretty green behind the ears when it comes to understanding how coding works so I’m seeking any help/guidance on ways to streamline what I have written. That said, I’d expect there to be a way to streamline how the timestamp is logged for each data point.

Thank you for any/all guidance!

MassCulture_Sept17.ino (11.9 KB)

An easy place to start is changing all the Serial prints with constant strings, to use the F macro:
replace all such:

    Serial.print("Flow rate: ");


    Serial.print( F("Flow rate: ") );

Edit - you can use it not just for serial prints, but all constant strings. e.g.

    myFile.print( F(" Int. Temp: ") );

"streamlining the timestamp" is something you should do yourself. It's fine that you got this far with "piece-meal and trial & error" but now that you're blocked and coming for help, that should end. First of all, you need to explain why you think it needs streamlining.

Came to post that, typed too slow. It may be enough by itself, so just do that before looking for anything fancier.

One way to save program memory is to create a function to write the time tag to the file. You have the same code 3 times in the file. The other is to use the F() macro. Like this:

Serial.println(F("Hello World"));

The F macro will save you some SRAM.

My concern is its using 21066 bytes and I'm getting a low memory/stability warning.

210066 bytes of what on which board ?

210066 bytes of what on which board ?

Doh. Board is an Uno & 21066 of 32256 bytes used.

I will read up on strings.

Thank you!

The low memory warning is for dynamic memory (ram), you are looking at flash memory. The UNO only has 2048 bytes ram.

When you wrote this: “My concern is its using 21066 bytes and I’m getting a low memory/stability warning”, my first thought was: “No, you don’t” :wink: It is about ram, as david_2018 already wrote.

The SD library uses a lot of ram. There are smaller libraries (made by the same author), PetitFS and FAT16. I know no one who uses those, so I can not recommend this.

Which Time library do you use ?
Is that the TimeLib, maintained by Paul Stoffregen ?
Then you have two “time” libraries running. Can you change your sketch to use only one ?
What happens if you remove the #include <Time.h> ?

The OneWire library has example code to get the temperature of a DS18B20. If you really need the ram then you could remove the DallasTemperature library and use the code from the OneWire library.

Who made that filter for the PH probe ?
Mathematically, every sample has information and every sample should be used for an average, assuming the noise is evenly spread. Is the filter to get rid of random noise spikes ?
I think ‘buf’ and ‘avgValue’ are only used locally, so you can declare it locally. The ‘buf’ is an array, but it will be only 20 bytes on the stack. There might be other variables that can be declared locally as well. Make variables as local as possible. That reduces the global block of variables in ram and the compiler can optimize better with local variables.

Using the F() macro will release a lot of bytes. That is hopefully enough to make your sketch reliable.
Not every function accepts the F() macro.
I think it should work with all the

  • Serial.print(), Serial.println()
  • myFile.print(), myFile.println()
    I’m not sure if the lcd.print() uses the same “print” class, but give it a try.

When you have done that, please copy the compiler message about the memory to show it to us.

Then you can relax and sip from a drink, thinking about your nice project. When you do that, start thinking about a MKR Arduino board with onboard SD socket and builtin RTC and more memory (32k ram, 256k flash): Arduino MKR ZERO | Arduino Official Store.