Problem using sprintf with LCD

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem using sprintf with LCD

1,177 Views
johnt
Contributor I

Hello everybody,

 

I'm trying to print the value of a int variable to one LCD but it's not working properly, I've tried many things but I would like to know how can I include a int variable to a normal a string that I want to print to the LCD.

 

I can only print normal texts to the LCD but without variables, for example doing this:

 

char *text="Hello world";

 

  while (*text)

  {  

    senddatatoLCD (*text++);  //Function made to send data to LCD in 2 lines using 4 bits only.

  }

 

I would like to build a string with value of int variable already included and then just send this string with a while loop as I've on the example shown before.

 

Something like this: sprintf (*string, "The value: %d", var);

 

and the use the *string to print it to the LCD.

 

Does someone knows how to do it?

 

Thanks in advance.

 

Labels (1)
0 Kudos
1 Reply

487 Views
TomE
Specialist II

This is very basic C programming. You need "string" to be an ARRAY to define some storage for sprintf to write to.

char vString[100];
char *pText;
int nVar = 123

sprintf(vString, "The value: %d", nVar);
pText = vString; /* this is the same as "pText = &vString[0];" */
while (*pText)
{
    ... And so on, the same as before ...

See if you can get an old K&R C book and work through that.

Tom