How to print more than one variable to an lcd?

How can I print more than one variable at a time to an LCD? For example, print(var1 . var2); or print("var1" + "var2");

It's easy:

LCD.print(var1); LCD.print(var2);

CDCosma:
How can I print more than one variable at a time to an LCD? For example, print(var1 . var2); or print("var1" + "var2");

The way I like to do it is to format my output using SPRINTF.

For example:

    int sensor = 0;
    int value = 1023; // demo values
    const char *mask = "Sensor #%d value is %4d\r\n";
    char buffer[20];
    sprintf(buffer, mask, sensor, value);
    lcd.print(buffer);

That works a lot better than line after line of "lcd.print(firstpart), lcd.print(nextpart), lcd.print(morestuff), etc....."

Only downside is if you try to use sprintf with floating point values... the Arduino AVR libraries are stripped down to minimize code size and floating point is not supported.

You CAN either change the compiler option strings and rebuild the IDE to support floating point, or you can "patch" your "libc.a" files to put back in the floating point support, but then the resulting code ends up being about 1.5K larger.

If you are interested in using these methods, let me know and I'll give you the files you need.

-- Roger

(edit to add): Attached is a ZIP file of a new "pde.jar" file for Arduino IDE 1.0.3.

It's the same as original, except that it adds the option "Enable floating point support" in the "Preferences" dialog and saves the choice in the "preferences.txt" file as "build.floating_point=true/false".

If it's checked on (true), then the compiler option "-Wl,-u,vfscanf,-lscanf_flt,-u,vfprintf,-lprintf_flt,--gc-sections"+optRelax," is used. If it's checked off (false), the standard non-floating point "-Wl, --gc-sections"+optRelax," option is used.

The .jar file should be compatible with Windows, MAC and Linux since only the Java code was changed. If you like, please give it a try... FIRST make a backup copy of your "pde.jar" file (located in the /arduino/lib directory) by naming it to something like "pde.backup" (don't use a .jar extension!). Then unzip the attached file in the same place. The new "pde.jar" file will be extracted. If it's working, you should have a new option in your "Preferences" dialog and you should be able to turn on or off floating point support.

Note that your compiled sketches will end up about 1.5K larger if you use the floating point option.

Anyone who tries it, please let me know if it works for you (specifically I want to know if it works in Windows and MAC since I compiled it in Linux). Please feel free to email me (the icon under the avatar) and tell me if it works for you.

Thanks!

-- Roger

pde_with_floating_point_option.zip (748 KB)

What does it take to open the archive? both the PDE.zip and the Krupski files are apparently corrupted with an "! C:\Users\End User\Downloads\pde.zip: Unexpected end of archive
I have tried both krupski's and the 'original' and neither will unzip... What Am I doing wrong?

Bob

Krupski:

CDCosma:
How can I print more than one variable at a time to an LCD? For example, print(var1 . var2); or print("var1" + "var2");

The way I like to do it is to format my output using SPRINTF.

For example:

    int sensor = 0;

int value = 1023; // demo values
   const char *mask = "Sensor #%d value is %4d\r\n";
   char buffer[20];
   sprintf(buffer, mask, sensor, value);
   lcd.print(buffer);




That works a lot better than line after line of "lcd.print(firstpart), lcd.print(nextpart), lcd.print(morestuff), etc....."

You should take a look at snprintf - it enables you to avoid overrunning the output buffer, which I think your example code will do.

which I think your example code will do.

It will write anywhere from 30 to 33 characters in the 20 character buffer. Looks like overrun to me.

Using lcd.print() you can do one value at a time. What about using the lcd setCursor() function to move the cursor to a noew position to print the second value?

  • print first value
  • move cursor to new position
  • print second value

If you want to print a formatted message then sprintf or a variation of that is the easiest way to go.