Value of an integer to be shown on LCD.

Hi, I am new to Arduino.
How can I get the value of one integer (not the letter with corresponding number from ASCII table) to be shown on LCD display? Could not find neither tutorial nor example.
Wal

http://arduino.cc/en/Reference/LiquidCrystalPrint

Is this on a standard display or a serial display?

I have never used a normal LCD display but I presume that when you would normally do something like this:

lcd.print(“hello, world!”);

you want to do something like this:

lcd.print(number, DEC);
or
lcd.print(i, DEC);

I presume someone can confirm this :wink:

Mowcius

Thank You for the answers.
This is normal display (parallel).
what is confusing, the seller advised to use a different header, not
#include <LiquidCrystal.h>
but:
#include <LCD4Bit.h>
What is the difference?
when I tried lcd.print(int);
or lcd.print(int, DEC);
on the LCD was seen a symbol (not letter or number) from ASCII table.

You can use either, the LCD4bit library uses slightly less pins as I understand it but both can be used.

Have you tried, BYTE, BIN etc to see what comes up?

Mowcius

when lcd.print(int,BYTE); I get compilation error, (probably, because of different header)
tried again lcd.print(int,DEC); as well error,
no error with: lcd.print(int); but rubbish on the display

Did you have a space inbetween int, DEC and int, BYTE?

I will take a closer look at that library if I get time. Can you use the other library instead?

Mowcius

Or, can you just convert the int to a string, and print the string?

int myInt = 47;
char myStg[10];
sprintf(myStg, "%d", myInt);
lcd.print(myStg);

Yes, I tried with and without spaces.

About conversion from integer to string:
compiler stops at the line lcd.print(myStg); with a message: invalid conversion from ‘char*’ to ‘int’.

Looking at the LCD4Bit code, it appears as though you need to use printIn to print a string. All that printIn does is print one byte at a time using print, so you could do the same thing:

int myInt = 47;
char myStg[10];
sprintf(myStg, "%d", myInt);
for(int i=0; i<strlen(myStg); i++)
{
  lcd.print(myStg[i]);
}

It won’t make much difference for this example code, but I wanted to note that using strlen() like this is not a good programming practice. The reason is that the strlen() function will be called each time through the loop. In this case, the result of strlen() will stay the same, so it’s not necessary to call it each time. You could call strlen() once and save the result:

int len = strlen(myStg);
for(int i=0; i<len; i++)
{
  lcd.print(myStg[i]);
}

Better yet, you don’t need the strlen() function at all here. Just check for the NUL terminator at the end of the string:

for(int i=0;myStg[i] != '\0'; i++)
{
  lcd.print(myStg[i]);
}

Regards,

-Mike

Any halfway decent compiler will detect that the argument to strlen is not being changed in the loop, and will create a temporary variable to cache the result. So, the overhead of a function call is eliminated.

And, yes, checking for a NULL is quicker, too. It’s just not as readable, to me. I know what strlen does, and can follow the loop without thinking much about how strlen works. The code with the NULL check does not feel as obvious to me.

Why not just make it semi-simple for him? lol

You want to use the ITOA Command (Integer TO Ascii)… You can also use the ‘sprintf’ if you’re more comfortable with C.

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

For example, at the top of your sketch, you’ll want to make a buffer:

char myData[5]; // BEFORE SETUP

Now, inside the loop where you want to change the integer:

int myValue = analogRead(0);

itoa(myValue, myData, 10); // does same thing as sprintf, put’s myValue into a String called myData, then you print that string on the lcd! (this can also be used for almost any kind of variable)
lcd.print(myData);

char myData[5]; // BEFORE SETUP

Dangerous.

char myData[6];

would be better, for an “int” (well, an “int” on an Arduino anyway).

Thank You very much for the precious recomendations, I’ll try them probably in the next weekend.
Another questions: how can cursor be moved with the LCD4Bit library?
The job is to start motor after pressing “Run” button and then count pulses from the 3 different inputs. After receiving preprogrammed quantity of pulses from one input, motor should stop and all results be shown on screen. Another task is to measure time between the “Run” button was pressed and the last pulse was received. But how the working time could be measured?

With Lcd4bit, you move the cursor with this function:
cursorTo(int line_num, int x);

But I suggest you use the more capable LiquidCrystal library that comes with Arduino 0017. The earlier versions had some issues but you should be fine the latest release.

Each time through loop, you are checking for events. You check to see if a button was pushed, or that the required number of pulses was received. Each time an event of interest occurs, use millis() to capture the number of milliseconds since the Arduino restarted. The function returns a long int. You can subtract one time value from another to get the difference in milliseconds.

Tried itoa(myValue, myData, 10);
at first was compilation error, but when lcd.print(myData); was replaced with lcd.printIn(myData); it run OK.
Why
char muData[6];
is better than
char myData[5]; ?
With millis() was no problem. But how transform, for example, 6935 mS into 6.9 s? When milliseconds were divided by 1000, the result was 6, not what was wanted.

Why
char myData[6];
is better than
char myData[5]; ?

The maximum value that analogRead can return is +1023. That’s 5 characters. There also needs to be room in the array for a trailing NULL. That makes 6 characters, so “char myData[6]” allocates room for all 6 characters. Therefore it is better, because there can be no buffer overrun when the buffer is big enough.

With millis() was no problem. But how transform, for example, 6935 mS into 6.9 s? When milliseconds were divided by 1000, the result was 6, not what was wanted.

long then = millis();
//do something
long now = millis();
int secs = (now - then) / 1000;

This will result in secs = 6.

long then = millis();
//do something
long now = millis();
float secs = ((float)now - (float)then) / 1000.0;

This will result in secs = 6.935.

There is no ftoa function, though, so you have to use sprintf to convert floats to strings.

Thank You PaulS.
Is it possible to round 6.935 to 6.9?
Another question:On the LCD shield there are some buttons. All connected through resistors to one analog input. There is need to program the quantity of expected pulses. As usual, press some seconds “Menu” button and get the value to be changed flashing, the with arrow buttons “Up” or “Down” change the value. Then “Enter” or wait some seconds.
Is somewhere an example?