F() equivalent on String Variables?

I've finished coding something, and it ended up using like 200% of my arduino's memory. So, I've been taking steps to reduce my program's memory output, starting with my usage of strings. I've learned that using F() helps optimize SRAM by putting Strings in flash when using functions like Serial.print() or Serial.println(). I've been working to get rid of strings, but oftentimes throughout my code, I have statements like this:

getIRBoolInput("Are you sure about this? (" + String(reconfirmsDone) + "/" + String(timesToReconfirm) + ")")

The only use for this string is to go into a Serial.print() function later down the line, so I want to be able to use F() on it. But, I can't-- you can only use F() on string literals, so anything like variables don't seem to be able to work with it. Are there any alternatives I can do to help optimize memory in situations like this?

First understand that String is a C++ specific dynamic type, while C strings are pointers to characters. Each F() represents a const C string that is only copied into SRAM when needed.

Use some other string conversion function to get rid of the String type.

1 Like

what's the type of reconfirmsDone and timesToReconfirm ?

you would need to write a etIRBoolInput() function that takes the text and those 2 variables as parameters and do the

Serial.print(text):
Serial.write ('(');
Serial.print(reconfirmsDone):
Serial.write ('/');
Serial.print(reconfirmsDone):
Serial.println (")");

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.