Pages: [1]   Go Down
Author Topic: How to print more than one variable to an lcd?  (Read 568 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8699
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's easy:
Code:
LCD.print(var1); LCD.print(var2);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
   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 (747.57 KB - downloaded 16 times.)
« Last Edit: December 28, 2012, 11:45:30 pm by Krupski » Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2865
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12562
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
   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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1257
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Pages: [1]   Go Up
Jump to: