Help i creating function for setCursor and print to lcd from flash

Whenever I need to print to lcd display I have to type

lcd.setCursor(column, row);
lcd.print("String);

So I created a function

void lcdsnp(char col, char row, String data){  // Lcd set cursor and print
  lcd.setCursor(col, row);
  lcd.print(data);
}

which can be called by lcdsnp(col, row, "String"). I have a menu function which would be called once only if required for calibration. How to modify the function to use
lcd.print(F(data)) instead of lcd.print(data) to save SRAM space ?

Ah, you need to use the "flash string helper function/macro". I've forgot the details, but search for that.

I searched but couldn't understand any examples. Can you please help

 lcdsnp(col, row, F("String")).

This doesn't work

You need to create a new function that accepts the string created by F()

void lcdsnp(char col, char row, const __FlashStringHelper* data ){

The reset of the function should work ok without modification, since most LCD libraries work with the F() macro.

< edit >
Also, the following:

lcd.print("String");

does not create a String, it creates a null-terminated char array. The matching argument for a function would be const char*.

1 Like

isn't the issue that data can't be a variable passed to the sub-function?

is SRAM and issue?

The solution was given by @david_2018. Using function overloads:

void setup() {
  lcdsnp(1, 1, "Hello World");
  lcdsnp(1, 1, F("Hello World"));
}

void loop() {
}

void lcdsnp(uint8_t col, uint8_t row, const __FlashStringHelper *data ) {
  lcd.setCursor(column, row);
  lcd.print(data);
}

void lcdsnp(uint8_t col, uint8_t row, const char *data ) {
  lcd.setCursor(column, row);
  lcd.print(data);
}

It will work. The String class knows how to construct a String object from a pointer c-string (or c-string literal). It can even construct a String object from a __FlashStringHelper* pointer. So, without overloads like my previous post:

void setup() {
  lcdsnp(1, 1, "Hello World");
  lcdsnp(1, 1, F("Hello World"));
}

void loop() {
}

void lcdsnp(uint8_t col, uint8_t row, const String data ) {
  lcd.setCursor(column, row);
  lcd.print(data);
}

how do you know?

I think @dariods8474 tried it and find it would not compile. You cannot pass F("Hello") to a function expecting a parameter which is const char*.

Thanks. I managed to cut down the RAM occupied by global variables to 18% from 35%.

i'm curious what did you do?
reduce size: int to byte
reduce array sizes
make more variables dynamic

By using the method provided in the solution I made my 20 lcd.print("Some text) which was copying them from flash to RAM at startup was made to read from flash.

Serial print is used mostly for debugging. I won't need it now. Is it ok to leave Serial.begin(9600); as it is or should I delete it? Will it have any impact on resources?

It will take some flash and some RAM. It will not affect speed.

Compile your code and check flash and RAM as reported by the IDE. Next remove the Serial.begin line and compile again. Check for the difference.

Note: you only need to compile (verify), no need to upload.

1 Like

The Serial.begin uses 842 bytes of flash memory and 175 bytes of SRAM. If I delete Serial.begin the RAM usage comes down to 9% from 18%

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