Passing a value to a function

Hi all.

I have a 16x4 LCD and want to add a small function that takes care of the offset problem on the 3rd and 4th lines.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  lcd.print("hello, world!");
}

void LCDprint(int Lcol,int Lrow,String & Ltext) {
  if(Lrow > 1){
    Lrow = Lrow - 2;  
    Lcol = Lcol + 16;
  }  
  lcd.setCursor(Lcol, Lrow);
  lcd.print(Ltext);
}

void loop() {
LCDprint(0,1,String(millis()/1000));
LCDprint(0,2,(millis()/500));
LCDprint(0,3,(millis()/250));
}

What I can’t find is the correct syntax to use when calling the function : for example the ‘string’ in : String(millis()/1000) - I assume that there is something that will take any value, text or numeric, and convert it to a string / text type variable ?

Then, in the function itself, what do I need to receive that string : void LCDprint(int Lcol,int Lrow,String & Ltext) {

Any help appreciated. Regards.

The String constructor only accepts byte[] and char[] if I remember correctly. You will need to use sprintf to convert the number to an array of characters (char[]).

There's print methods for char, byte, int and long, not just string, so do this:

void LCDprint(int Lcol,int Lrow, long data) {
  if(Lrow > 1){
    Lrow = Lrow - 2;  
    Lcol = Lcol + 16;
  }  
  lcd.setCursor(Lcol, Lrow);
  lcd.print(data);
}

void loop()
{
  unsigned long time = millis () ;  // call millis() once only
  LCDprint(0,1, time/1000);
  LCDprint(0,2, time/500);
  LCDprint(0,3, time/250);
}

Thanks MarkT

From what I have tested, doing that limits the data that the function can receive to a Long.

I want to be able to pass any datatype to the function. Is there a ‘catch all’ variable that can receive any data type, or otherwise something that I can add to the calling line (in the void loop) that will convert anything to a passable variable ?

There is no catch-all variable (or type).

Use polymorphism:

void foo(int i) {};
void foo(char i) {};
void foo(long i) {};
void foo(char* i) {};
void foo(string i) {};

etc.

Although for what you're doing, I think you'd be better off making a function that simply sets the position of the LCD cursor correctly - then you can use the normal LiquidCrystal print commands.

For ex:

void setLCDCursor(&LiquidCrystal l, int row, int col) {
  if (row > 1) {
    l.setCursor(row - 2, col + 16);
  } else {
    l.setCursor(row, col);
  }
}

...

setLCDCursor(lcd, 2, 17);
lcd.print(value);

(note: untested)

Thanks to all for the replies.

I have gone with the idea of using the function to set the LCDcursor position, and the print to the lcd from within the loop()

here’s the code if it is of use to anyone.

// include the library code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {

  lcd.begin(16, 4);
  lcd.print("hello, world!");

}

void LCDpos(int Lcol, int Lrow) {

  lcd.setCursor(Lcol, Lrow);
  if(Lrow > 1) lcd.setCursor(Lcol + 16, Lrow - 2);

}

void loop() {

  LCDpos(0,1); lcd.print(millis()/1000);
  LCDpos(0,2); lcd.print(millis()/500);
  LCDpos(0,3); lcd.print(millis()/250);

}