[Solved] Problem print float number to UTFT and Serial

The myGLCD.printNumF function has stopped working correctly
The controller stops.
Also, the function does not work: dtostrf when outputting to TFT.
Also does not output to port Serial.

Go on. Post your code.

My motor car is the wrong colour. What are you going to do about it?

David.

myGLCD.setColor(VGA_LIME);
txt = "Tust: ";
y = txt.length() * myGLCD.getFontXsize();
myGLCD.print(txt, 0, (TFT_SIZE_Y - myGLCD.getFontYsize() * i - corr));

char buffer[5];
float value = 12.987;
char str_temp[6];

/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
dtostrf(value, 5, 3, str_temp);
sprintf(buffer, "%s F", str_temp);
//Serial.println(value);
Serial.println(buffer);
//txt = buffer;
//Serial.println(txt);
//dtostrf(12.1, 2, 1, str);

//myGLCD.print(txt, y, (TFT_SIZE_Y - myGLCD.getFontYsize() * i - corr));
//myGLCD.printNumF(my_time_curr.temp, 1, 0, (TFT_SIZE_Y - myGLCD.getFontYsize() * i - corr)); i += 1;

Something is wrong with the conversion float to string.

Make a bigger buffer for str_value
Make a bigger buffer for buffer

Think about it. "12.987" needs str_value[7] at least.
"12.987 F" needs buffer[9] at least.

I need to see which Font you are using to see printNumF()
And check the printNumF() documentation

David.

Just to elaborate on what david_prentice wrote:

DrAngelOK:

 float value = 12.987;

char str_temp[6];

When working with strings, it's essential to always size the buffer to leave enough space for the null terminator at the end. The text representation of value will have 6 characters. So the smallest possible buffer length to can store this string is 7, not 6.

It works with such a buffer size if the clean code is in the IDE.
Thanks, I will try to increase.

Smallfont

#include <avr/dtostrf.h> //Float-String

void setup() {
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  char buffer[50];
  float value = 12.987;
  char str_temp[50];
  String txt;
  
  Serial.println("01");

  /* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
  dtostrf(value, 5, 3, str_temp);
  Serial.println("02");
  Serial.println(str_temp);
  sprintf(buffer, "%s F", str_temp);
  Serial.println("03");
  //Serial.println(value);
  Serial.println(buffer);
  Serial.println("04");
  txt = str_temp;
  Serial.println(txt);
  Serial.println("05");
  txt = buffer;
  Serial.println(txt);
  Serial.println("06");

 delay(3000);
}

This code works in IDE.

String makes my head hurt.

The printtNumF() method is pretty useful e.g.

#include <UTFT.h>
extern uint8_t SmallFont[];

UTFT myGLCD(ILI9341_S5P, 11, 13, 10, 8, 9);

void setup()
{
    myGLCD.InitLCD();
    myGLCD.setFont(SmallFont);
    myGLCD.setColor(VGA_LIME);
}

void somefunc(void)
{
    float temp;
    for (int y = 0; y < 240; y += 20) {
        temp = 0.01 * random(0, 10000);
        myGLCD.printNumF(temp, 1, 0, y, '.', 6, ' ');
    }
}

void loop()
{
    myGLCD.clrScr();
    somefunc();
    delay(1000);
}

You can read the documentation for printNumF(). It is often useful to change the width. It is sometimes useful to change the decimal or padding character.

David.

I understand. But this method does not work for me. Trying to understand why.
My code works in IDE. My code works in my project in loop (). But it does not work in the place where I need to.

printNumF () also uses the sprintf function to convert to String

I misunderstood. You want to create a char array (or String) that you can show on Serial or UTFT.

So you would simply use Serial.println(str) or myGLCD.print(x, y, str)

Yes, you can use sprintf() to make everything in one go.
On a normal target sprintf() could format f-p expressions normally.

Arduino disables the f-p facilities of sprintf(). So you have to use dtostrf() to get a formatted f-p expression.

Your example is pretty simple. Many projects will have several items that they would like to display on one nicely formatted line.

In which case it is easy to do the whole line with one sprintf() statement.
Note that dtostrf() uses a char array. You can create a String from a char array if that is what floats your boat.

Incidentally, it is worth creating a helper function that outputs to Serial and TFT.
It makes your main program simpler.

David.

dtostrf () also uses the sprintf function to convert to String.
these functions do not work from anywhere except loop ().
The transition to UMT did not give any results.

What is UMT?

Different Cores implement dtostrf() in different ways.

You have shown that dtostrf() and sprintf() can compose a suitable char array e.g. in buffer
You can use UTFT to display on the screen.

I would avoid the String class. There seems little point when UTFT uses char arrays.

What do you really want to do?
Most apps concentrate on the format of the TFT display. But repeat the data on Serial.

Typically text and graphics are drawn on the TFT. Then specific fields are updated with sensor data.
It is important to format the sensor data so that numbers are nicely arranged.

David.

Here is an example that shows helper functions. e.g. for char array or String

Please note that your use of dtostrf() specified a width of 5 for “12.987” which is 6 characters + NUL
If you want to format nicely, you must get the widths correct.

#include <UTFT.h>

extern uint8_t SmallFont[];

UTFT myGLCD(ILI9341_S5P, 11, 13, 10, 8, 9);

void setup()
{
    Serial.begin(9600);
    myGLCD.InitLCD();
    myGLCD.setFont(SmallFont);
}

void bofe(char *str, int x, int y)
{
    Serial.println(str);      //char array
    myGLCD.print(str, x, y);  //char array
}

void bofe_String(String str, int x, int y)
{
    Serial.println(str);      //String
    myGLCD.print(str, x, y);  //String
}

void somebofe()
{
    char buffer[50];
    float value = 12.987;
    char str_temp[10];                 //big enough for -9999.999
    String txt;
    dtostrf(value, 7, 3, str_temp);    //float into char array
    sprintf(buffer, "%s F", str_temp); //formatted char array
    bofe("02", 0, 20);
    bofe(str_temp, 0, 40);
    bofe("03", 0, 60);
    bofe(buffer, 0, 80);
    bofe("04", 0, 100);
    txt = str_temp;   //initialise String from a char array
    bofe_String(txt, 0, 120);
    bofe("05", 0, 140);
    txt = buffer;     //initialise String from a char array
    bofe_String(txt, 0, 160);
}

void loop()
{
    Serial.println("");
    myGLCD.clrScr();
    somebofe();
    delay(1000);
}

It is unstable anyway. The port displays output to the series stably.
After displaying on the screen, the controller freezes after some time.

The problem is resolved. In my case, it was a problem with the port.