Printing a word in a LCD display

Hi,

I am trying to display a dynamic word using LiquidCrystal,

#include <LiquidCrystal.h>

LiquidCrystal lcd(3, 5, 9, 10, 11, 12);

void setup() {
lcd.begin(16, 2);
}

void loop() {
int a = 1;
int b = 2;
int c = 3;

char d = (’%s : %s : %s’, a, b, c);
lcd.setCursor(0, 0);
lcd.print(d);
delay (1000);
}

but the LCD doesn’t display 1:2:3. and only shows something like “R”
my project is to display something like aa:bb:cc where aa bb and cc ranges from 00-99

Thank you.

And where does A, B, and C get changed…
This:

char d = (’%s : %s : %s’, a, b, c);

Is Not going to do much…

Bob

char d = ('%s : %s : %s', a, b, c);

is incorrect.

char s[10];

sprintf(s, "%d:%d:%d", a, b, c);
lcd.print(s);

use %02d to get 2 digits always.

marco_c:
use %02d to get 2 digits always.

That won't always give 2 digits :stuck_out_tongue: Put in a value of 193 and it will display 193. "%02d" is a minimum of 2 digits, padded with leading zeroes. Something to be aware of on an LCD where it could mess up your display formatting (not to mention potentially causing a buffer overflow) if you're not careful. To limit it to 2 digits you could "modulus" your values with:

snprintf(s, 9, "%02d:%02d:%02d", a % 100, b % 100, c % 100);

But of course, if you know that your values are never going to go above 99, then that's not a problem.

Docedison:
And where does A, B, and C get changed...
This:

char d = ('%s : %s : %s', a, b, c);

Is Not going to do much..

Bob

I have a loop which gives me a, b, c i just simplified the code so that everyone can understand easily...

marco_c:

char d = ('%s : %s : %s', a, b, c);

is incorrect.

char s[10];

sprintf(s, "%d:%d:%d", a, b, c);
lcd.print(s);




use %02d to get 2 digits always.

Hi marco_c

it works perfectly,thanks,

but i just have a question regarding s[10], correct me if i'm wrong it's an array with 10 data storage, {1,2,3,4,5,6,7,8,9,10},
but i'm not sure how does sprintf is using it,because i tried using s[0], and prints a=99,b=99, c=99, and it still outputs "99:99:99"

majenko:

marco_c:
use %02d to get 2 digits always.

That won't always give 2 digits :stuck_out_tongue: Put in a value of 193 and it will display 193. "%02d" is a minimum of 2 digits, padded with leading zeroes. Something to be aware of on an LCD where it could mess up your display formatting (not to mention potentially causing a buffer overflow) if you're not careful. To limit it to 2 digits you could "modulus" your values with:

snprintf(s, 9, "%02d:%02d:%02d", a % 100, b % 100, c % 100);

But of course, if you know that your values are never going to go above 99, then that's not a problem.

Thanks for this info...I've noted your tip..:slight_smile:

but i just have a question regarding s[10], correct me if i'm wrong it's an array with 10 data storage, {1,2,3,4,5,6,7,8,9,10},

There are 10 elements, numbered 0 to 9.

but i'm not sure how does sprintf is using it,because i tried using s[0], and prints a=99,b=99, c=99, and it still outputs "99:99:99"

Even a snippet would be better than handwaving.

PaulS:

but i just have a question regarding s[10], correct me if i'm wrong it's an array with 10 data storage, {1,2,3,4,5,6,7,8,9,10},

There are 10 elements, numbered 0 to 9.

can you expound this statement, for what I understand my range is 0-99, so i need s[100]?

char s[10]

defines an array of 10 characters. You can put a 9-character "string" in there, because the last character should
always have a zero byte in it, which marks the end of your "string".

Your original code was only allocating a variable of one character, which you were trying to stick seven characters into - won't fit !

tacet777:
can you expound this statement, for what I understand my range is 0-99, so i need s[100]?

Depends on if you are talking about the values of your data, or the number of samples.

A char can hold a value from -128 to +127.

If you declare a char array, each index can hold a value between -128 to +127.

michinyon:
char s[10]

defines an array of 10 characters. You can put a 9-character "string" in there, because the last character should
always have a zero byte in it, which marks the end of your "string".

Your original code was only allocating a variable of one character, which you were trying to stick seven characters into - won't fit !

I still don't get it, but thanks for the explanation, i'll try to read more and return to this explanation. i might find what i'm looking for thanks alot.

An array of chars, s[10], can hold 9 characters and a terminating NULL. Those 9 characters can look like a number, like "12" or they can look like a word, like "Hey". You can mix what the characters represent, like "99:99:99".

Any given position in the array can hold a value in the range -128 to 127. The ASCII table assigns values to characters that can be printed to be in the range 1 to 127.

PaulS:
An array of chars, s[10], can hold 9 characters and a terminating NULL. Those 9 characters can look like a number, like "12" or they can look like a word, like "Hey". You can mix what the characters represent, like "99:99:99".

Any given position in the array can hold a value in the range -128 to 127. The ASCII table assigns values to characters that can be printed to be in the range 1 to 127.

Hi, i think i'm starting to understand what it is meant, for example "99:99:99" has 8 characters, so that s[0] - s[7] is used to contain each character in the string?

Hi, i think i'm starting to understand what it is meant, for example "99:99:99" has 8 characters, so that s[0] - s[7] is used to contain each character in the string?

Yes. Don't forget that s[8] contains the NULL that terminates the string.