Making a Menu

Trying to create a cooler controller with 3x4 matrix keypad and small OLED 0.91" as UI. see the attached files.
The programming is far from finished but I have SRAM memory problems.
The problem seems to be related to menu variables.
Any way to reduce the gloabal memory used by this code.
Any help would be appreciated.

Firmware.zip (30.5 KB)

Any way to reduce the gloabal memory used by this code.

Quit using Strings.

Modify the writeToOLED() function to use the FlashString helper class used by the F() macro.

You are putting stuff into PROGMEM that you never use, and you are putting stuff in PROGMEM that can NOT go in PROGMEM (class instances).

Please, pass me the code NOT in a .zip file, because someone can't ooen it

Change the writeToOLED to accept flashstringhelper and the code utilizing it to this:

void writeToOLED(const __FlashStringHelper* outString,int textSize,int posX,int posY,bool CLRSCR)
{
  if(CLRSCR==true){
    display.clearDisplay();
    display.display();
  }
  display.setTextSize(textSize);
  display.setTextColor(WHITE);
  display.setCursor(posX,posY);
  display.print(outString);
  display.display();
}

uint8_t decimals = 1;
uint8_t digits = 3;
float temp = GetNumber(digits,decimals);
float temp = GetNumber(digits,decimals);
String tmpString = "Are you sure to use ";
char temp_string[5];
dtostrf(temp, 4, 1, temp_string);
tmpString = tmpString + String(temp_string);
tmpString = tmpString + " ";
tmpString = tmpString + TemperatureUnits;
tmpString = tmpString + " as temperature set point? (#:Yes, other:No)";
writeToOLED(F(tmpString),1,0,0,true);

but getting the following error:

In file included from …\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:28:0,
…\Firmware\Firmware.ino:2:
…\Firmware\Firmware.ino: In function ‘void menuUseEvent(MenuUseEvent)’:
…\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/WString.h:38:74: error: initializer fails to determine size of ‘__c’
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
^
…\Firmware\Firmware.ino:180:21: note: in expansion of macro ‘F’
writeToOLED(F(tmpString),1,0,0,true);
^
…\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/WString.h:38:74: error: array must be initialized with a brace-enclosed initializer
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
^
…\Firmware\Firmware.ino:180:21: note: in expansion of macro ‘F’
writeToOLED(F(tmpString),1,0,0,true);
^

The F macro doesn't work on char pointer variables - it works on string constants.

not

 char tmpString[] = "a temp string";
// …
 writeToOLED(F(tmpString),1,0,0,true);

but

 writeToOLED(F("a temp string"),1,0,0,true);

or possibly

 const __FlashStringHelper *tmpString = F("a temp string");
// …
 writeToOLED(tmpString,1,0,0,true);

Thanks
I know that already. But what about a time when you want to output sth like this
("a temp string" + my_float_number + "trailing temp string") into screen?

Free calls of Serial.print (), and in this way you can also use F("…") to store the const parts in the program memory.
After I have some little trips to use less space (they are general trips, I can’t open the code so I can’t give you more particolar ones)

  1. use always the smallest variables you can:
    byte<255
    int<2^16
    unsigned long only millis

  2. put in #define all the constant variables, so you haven’t this space used

3)don’t use variables to collect the retuen of functions used only one time (the famous buttonState=digitalRead();if (buttonState)…) use directly the function

  1. use the minimum nunber possible of global variables, specially not the ones used only into a singular function.

5)I prefer don’t use bool type, because you use a byte for a bit. You can use a byte/char variable and, witch bitRead and bitWrite, use it like 8 bools.

This general trips may don’t be usefull to you or not enought, but if you need memory every byte is important

But what about a time when you want to output sth like this
("a temp string" + my_float_number + "trailing temp string") into screen?

Then you either make three calls to the function, for each of the three things, or you suffer the SRAM hit of making one string to pass to it.

I know which I'd do.

nimanouri:
Thanks
I know that already. But what about a time when you want to output sth like this
("a temp string" + my_float_number + "trailing temp string") into screen?

I think I'd probably do domething like:

writeToOLED(F("a temp string"),1,0,0,true);
writeToOLED(my_float_number),1,0,0,true);
writeToOLED(F("trailing temp string"),1,0,0,true);

Actually I have done what evanmars said, 700Byte free SRAM (just be carefull with the positions)
thanks all of you guys.

Firmware.zip (31 KB)