String Problem using Tft.drawString

Hi Community,

i have a problem with strings using the command "Tft.drawString".

I wrote the following:
void CenterText(String cttext, int ctx, int cty, int ctgroesse, int ctfarbefront, int farbeback)
{
x=ctgroesse6cttext.length();
y=ctgroesse*7;
Tft.fillRectangle(ctx-x/2,cty-y/2,x,y,farbeback);
Tft.drawString(cttext, ctx-x/2 ,cty-y/2 ,ctgroesse, ctfarbefront);
}

This should give me a centered text. The idea is to give in the text "cttext" which should be centered at the ctx and cty position on the 2.8" TFT screen.
But there is something wrong with the use of the String cttext...

The Compiler says:

C:\Program Files (x86)\Arduino\libraries\TFT_Touch_Shield_V2-master/TFTv2.h:248:10: note: no known conversion for argument 1 from 'String' to 'char*'
exit status 1
no matching function for call to 'TFT::drawString(String&, int, int, int&, int&)'

How to solve this ???

Thanks! Heiko

drawString() doesn't want a String object but a simple C string aka character array. So change your function definition to

void CenterText(char *cttext, int ctx, int cty, int ctgroesse, int ctfarbefront, int farbeback)

and it will probably compile. That way you eliminate the use of the String class which you shouldn't use on an AVR Arduino (as the UNO is).

If you accept the risk of using the String class, you can use the class' c_str() method to get a character pointer which you can feed to drawString().

You can read the reasons why you should not use Strings on Arduino here
The Evils of Arduino Strings

best regards Stefan

1 Like

YES, thats the solution! Thanks!

void CenterText(char cttext, int ctx, int cty, int ctgroesse, int ctfarbefront, int farbeback)
{
x=ctgroesse
6strlen(cttext);
y=ctgroesse
7;
Tft.fillRectangle(ctx-x/2,cty-y/2,x,y,farbeback);
Tft.drawString(cttext, ctx-x/2 ,cty-y/2 ,ctgroesse, ctfarbefront);
}