Display 1602 function

How to make a function for LCD 1602 so that I don't have to write 5 lines all the time, but use only one line and that's it.
For example: printLCD (TEXT_1, TEXT_2) ;
Thank you very much for your help

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(TEXT_1);
lcd.setCursor(0, 1);
lcd.print(TEXT_2);   
void printLCD(TEXT_1, TEXT_2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(TEXT_1);
lcd.setCursor(0, 1);
lcd.print(TEXT_2);
}
void printLCD(char* TEXT_1, char* TEXT_2)
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(TEXT_1);
  lcd.setCursor(0, 1);
  lcd.print(TEXT_2);  
}
1 Like

Right. forgot the char*

Hi @anon57585045 @anon73444976

A question for me to learn.

In this function, what is the advantage when using

void printLCD(char* TEXT_1, char* TEXT_2)
or
void printLCD(char TEXT_1, char TEXT_2) (without the *)

RV mineirin

The first one passes a C string, the second passes only one character.

tks

I guess you could do both, and allow the compiler to do the overloads

I assumed (perhaps wrongly) that TEXT meant more than a single character.

tks

@cevepe
you could use a template to spare a lot of overloads

template < typename T0, typename T1>
void printLCD(T0 line0, T1 line1)
{
  lcd.clear();
  //lcd.setCursor(0, 0); // done by clear anyway
  lcd.print(line0);
  lcd.setCursor(0, 1);
  lcd.print(line1);
}

this enables you do print out more than just char*,
Examples:

  printLCD("test", "string lit"); 
  printLCD("char", 'x');
  String aString = "FooFoo";
  printLCD("String", aString);
  printLCD("int", 1234);
  printLCD(1234, "int");
  printLCD("float", 12.34);
  printLCD(12.34, "float");
  printLCD(12.34, F("mixed with F-Makro"));

but if you need really a function for "that i don't have to write 5 lines all the time" ... and if you really need it a lot in your sketch, you might consider other concepts, for example holding the data in PROGMEM or using the F-Makro

2 Likes

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