# writing variable to LCD problem

#### Bear_2759

Joined May 23, 2008
120
hey guys, I'll try and make this short and sweet. using Arduino Uno R3 and nokia 6100 LCD Shield from sparkfun.
I have integer Y which for this example increases by 1 each loop while the program is running. each time it increases I need to write to the screen.
Rich (BB code):
void    loop()
{
Y += 1;
LCDClear(x);
setStr(Y, 0, 0, RED, x);
delay(1000)
}
this does not work, returns an error while compiling.
"Cannot convert 'string' to 'char*' for arg '1' to 'void setStr(char*, int, int, int, int)'"

if I set the value in the setStr line then it works fine. e.g.
Rich (BB code):
 setStr("Hello World", 0, 0, RED, x);
with Y it does not work, I have also tried putting Y in inverted commas e.g. \"Y\"
I've also tried converting it from an integer to a string which also didn't work.
any ideas? the setStr code is as follows.

Rich (BB code):
void setStr(char *pString, int x, int y, int fColor, int bColor)
{
x = x + 16;
y = y + 8;
// loop until null-terminator is seen
while (*pString != 0x00) {
// draw the character
setChar(*pString++, x, y, fColor, bColor);
y = y + 8;
// bail out if y exceeds 131
if (y > 131) break;
}
}
most of it is just copy/paste out of examples.

#### ErnieM

Joined Apr 24, 2011
8,177
Rich (BB code):
void    loop()
{
Y += 1;
LCDClear(x);
setStr(Y, 0, 0, RED, x);
delay(1000)
}
this does not work, returns an error while compiling.
"Cannot convert 'string' to 'char*' for arg '1' to 'void setStr(char*, int, int, int, int)'"
The line "Y += 1;" is treating Y as if it is a number (an integer or such).

The line "setStr(Y, 0, 0, RED, x);" is treating Y as a pointer to a character (char*).

Also, a 'string' is an array of characters.

You cannot just add one (+= 1) to a character string and expect some magic to happen. You really need something like a new counter variable to increment.

That number needs to be converted to a string,I'm not an Arduino guy so someone else will need suggest a good way to do that conversion on that platform (it's some library thing).

#### MrChips

Joined Oct 2, 2009
24,173
You need to convert Y into a string. You can write this function yourself or you can use a library function such as num2str( ) or sprintf( ).

#### Bear_2759

Joined May 23, 2008
120
problem is I did try to convert it to a string. I also tried, as a test creating a string and putting that in the code and it returned the same error.
lets say this is in place of Y.

Rich (BB code):
String mystring = "Hello World";
setStr(mystring, 0, 0, RED, x);
exactly the same error is returned, that it cannot convert 'string' to 'char*'...

excuse me for being a bit of a noob at programming. I will learn quick .

#### MrChips

Joined Oct 2, 2009
24,173
Try using &mystring

#### Bear_2759

Joined May 23, 2008
120
compiling still fails. same err. I've also tried setting the string as the following to make sure the inverted commas are included without success.

Rich (BB code):
String mystring = "\"Hello World\"";

#### MrChips

Joined Oct 2, 2009
24,173
Declare the string as
char mystring[20];

#### Bear_2759

Joined May 23, 2008
120
Rich (BB code):
char mystring[] = "Hello World";
setStr(&mystring, 0, 0, RED, x);
same error, this time though it's cannont convert 'char (*)[12]' to 'char*' for argument '1'....

#### Bear_2759

Joined May 23, 2008
120
would it be possible to write my own setStr function? the only thing I dont fully understand in that function is what *pString does.

what I have in my head at the moment is something a little more simple. because the
Rich (BB code):
char mystring[]
is an array, could I just pass them individually to the setChar function that the setStr funciton uses?

I know this code isn't right, but to give you the general idea

Rich (BB code):
for each char in mystring
setChar(char, x, y, fcolor, bcolor)
y=y + 8
next
I'm more familiar with VBS so it's the way my head works at the moment lol. trying to change the way I think.

#### MrChips

Joined Oct 2, 2009
24,173
I will get back to you later unless someone else figures out the problem. Don't despair.

#### MrChips

Joined Oct 2, 2009
24,173
Did you try? :

Rich (BB code):
char mystring[] = "Hello World";
setStr(mystring, 0, 0, RED, x);

#### ErnieM

Joined Apr 24, 2011
8,177
Rich (BB code):
char mystring[] = "Hello World";
setStr(&mystring, 0, 0, RED, x);
same error, this time though it's cannont convert 'char (*)[12]' to 'char*' for argument '1'....
That is the silliest compile erroe I have ever seen. It is saying "you told me you will pass a pointer to a char, but the thing you are pointing to has 12 characters." That is truly a "WTF?"

Perhaps try:

Rich (BB code):
char mystring[] = "Hello World";
setStr(&mystring[0], 0, 0, RED, x);
That is a one character string.

What compiler is this? All the previous suggestions worked using Hi-Tech C.

#### Bear_2759

Joined May 23, 2008
120
thanks guys.

Rich (BB code):
char mystring[] = "Hello World";
setStr(&mystring[0], 0, 0, RED, x);
this works, a mate of mine was helping me with it today and I logged on here to tell you a solution had been found lol.

now I just need to know, how do I convert an integer, to a string. during each loop the integer will increase by 1 and then needs to have the value of the integer copied to the string.

ErnieM, to answer your question I'm using the Arduino complier beta build 023. this may have been fixed when they released 1.0 recently but I have to use 023 for backwards compatability with the libraries for the LCD. unless I want to re-write the libraries and my skills aren't quite up to that yet.

#### ErnieM

Joined Apr 24, 2011
8,177
now I just need to know, how do I convert an integer, to a string. during each loop the integer will increase by 1 and then needs to have the value of the integer copied to the string.
Sorry I did a little searching but didn't come up with a library reference for that. But teh interwebs are full of conversion functions, from tthe simple to the ridiculously complicated foolproof ones. Here's a simple one:

Rich (BB code):
char* itoa(int val){
#define MAX_BUF 6      // set to hold maximum characters (2 for 0-99, 3 for 0-999, etc)
static char buf[MAX_BUF+1];
buf[MAX_BUF] = 0;
int i = MAX_BUF-1;
for(; val && i ; --i, val /= 10)
buf = '0' + (val % 10);
return &buf[i+1];
}

It returns a char* (pointer) to the string to print out.

#### MrChips

Joined Oct 2, 2009
24,173
(I post this here from another thread since this is the same problem.)

As in any programming problem such as this one, you need to come up with a clear, precise algorithm. This particular problem is not so difficult.

8-bit binary can represent unsigned integer values from 0 to 255. Hence you will need three decimal digits to cover the range. The result you are seeking is

N = D2 x 100 + D1 x 10 + D0 x 1

where N = 8-bit binary and D2, D1, D0 are the resulting decimal digits.

If your MCU has an efficient integer division instruction you can use this to find D2, D1, D0 by dividing by 100 and by 10.

If you want to try this without a divide operation the algorithm is as follows.

1. Find how many times (D2) you can subtract 100 from N without underflowing. Save the remainder R.

2. Find how many times (D1) you can subtract 10 from R without underflowing. The remainder is D0.

This is a classic programming exercise for every student learning to program in ASM and C.
Try to do it in both languages.