How to print a Variable in TFT 3.5 in with the MCUFRIEND Library?

Hello everyone, I am currently working on a project in which I need to measure the analog voltage of a sensor, and for that, I have created the following functions, I would like to get the sensor value updated constantly on the screen, but I do not know how to do it, I thought about transforming the value to type “char”, to be able to print it in each program cycle, how can I do that?

The model of my screen is the following:https://articulo.mercadolibre.com.mx/MLM-613677029-kuman-arduino-uno-r3-de-35-pulgadas-de-pantalla-tactil-tft-_JM

This is my Sketch that I have done so far, it throws me an error when compiling, it throws me the following error:

no matching function for call to ‘MCUFRIEND_kbv :: print (char [4], int, int)’

My Sketch:

#include <MCUFRIEND_kbv.h>   
MCUFRIEND_kbv tft;

#include <FreeDefaultFonts.h>   //SmallFont, BigFont, SevenSegFont
#include <Fonts/FreeSerif12pt7b.h> //Free Font that comes with GFX
#include <Fonts/FreeSansBoldOblique24pt7b.h> //Free Font that comes with GFX

#define BLACK           0x0000      /*   0,   0,   0 */
#define DARKGREEN       0x03E0      /*   0, 128,   0 */
#define BLUE            0x001F      /*   0,   0, 255 */
#define GREEN           0x07E0      /*   0, 255,   0 */
#define RED             0xF800      /* 255,   0,   0 */
#define MAGENTA         0xF81F      /* 255,   0, 255 */
#define YELLOW          0xFFE0      /* 255, 255,   0 */
#define WHITE           0xFFFF      /* 255, 255, 255 */
#define GREENYELLOW     0xAFE5      /* 173, 255,  47 */

const int Vpin = A8;  //Entrada Analógica del sensor de voltaje
int led = 10; //Pin PWM donde va conectado el LED
int ValorVsignal;
float vrms = 0;
float ValorVfloat;
float suma = 0;
char vrms_1[4];

//Define el ancho y largo de la pantalla en pixeles
int x_size = 480; int y_size = 320;

bool settscrsize (int w, int h)
{
    if ((w <= 0) || (h <= 0)) return false;
    x_size = w; y_size = h;
    return true;
}

void setup()
{
    uint16_t ID = tft.readID();
    tft.begin(ID);
    tft.setRotation(1);
    tft.fillScreen(WHITE);
}

void loop()
{
 Parametros_CFE();
 Leer_Voltage();
 Imprimir_Voltage_CFE();
}

void Parametros_CFE() {
  LogoCFE(180, 80);
  tft.setFont(&FreeBigFont);
  tft.setTextColor(BLACK);
  tft.setCursor(100,130);
  tft.print("PARAMETROS DE RED");
  tft.setCursor(30,160);
  tft.print("VOLTAJE:");
  tft.setTextColor(RED);
  tft.print(vrms_1, 190, 120);
  tft.setCursor(270,160);
  tft.print("V");
}

void LogoCFE(int x1, int y1) {
  tft.setFont(&FreeSansBoldOblique24pt7b);
  tft.setTextColor(DARKGREEN);
  tft.setCursor(x1,y1);
  tft.print("CFE");
}

void Leer_Voltage() {
  for (int i = 0; i <= 1666; i++) {
    ValorVsignal = analogRead(Vpin);
    ValorVfloat = map(ValorVsignal, 0, 1023, -268, 268);
    suma = suma + (pow(ValorVfloat, 2));
    delayMicroseconds(10);
  }
  vrms = sqrt((suma / 1666));
  suma = 0;
}

void Imprimir_Voltage_CFE() {
  tft.setTextColor(RED);
  String ValorV = String(vrms);
  ValorV.toCharArray(vrms_1,4);
  tft.print(vrms_1, 190, 120);
}

All Print.h style print methods just take values of different types e.g. String, int, float, ...

If you require to change the print position, use a separate setCursor() e.g.

void Imprimir_Voltage_CFE() {
    tft.setTextColor(RED);
    String ValorV = String(vrms);
    ValorV.toCharArray(vrms_1, 4);
    tft.setCursor(190, 120);
    tft.print(vrms_1);
}

Note that there are far easier ways to do evaluate the voltage and do the printing.

Personally, I would write a helper function that formats the voltage, draws a fresh background, prints the new value of the voltage.

David.

david_prentice:
All Print.h style print methods just take values of different types e.g. String, int, float, ...

If you require to change the print position, use a separate setCursor() e.g.

void Imprimir_Voltage_CFE() {

tft.setTextColor(RED);
    String ValorV = String(vrms);
    ValorV.toCharArray(vrms_1, 4);
    tft.setCursor(190, 120);
    tft.print(vrms_1);
}




Note that there are far easier ways to do evaluate the voltage and do the printing.

Personally, I would write a helper function that formats the voltage, draws a fresh background, prints the new value of the voltage. 

David.

Thanks David!!, Now I understand it, ¿Can you help me with this function that you says?

Here is a helper function. The Free Fonts always print transparently. So you need to draw the background to rub out the previous number. You could add width, prec as parameters to control the format.

void print_float_at(float val, int x, int y)
{
    char buf[10];
    int16_t x1, y1, w, h;
    tft.getTextBounds("000.0", x, y, &x1, &y1, &w, &h); 
    dtostrf(val, 5, 1, buf);   //e.g. 123.4
    tft.fillRect(x1, y1, w, h, YELLOW);
    tft.setTextColor(RED);
    tft.setCursor(x, y);
    tft.print(buf);
}

Use like this:

void Imprimir_Voltage_CFE() {
    print_float_at(vrms, 190, 160);
}

and your setup(), loop() could be:

void setup()
{
    uint16_t ID = tft.readID();
    tft.begin(ID);
    tft.setRotation(1);
    tft.fillScreen(WHITE);
    Parametros_CFE();    //print main screen once
}

void loop()
{
    Leer_Voltage();
    Imprimir_Voltage_CFE();
}

David.