Print_text and variable issue

Hello,
I have a variable like
float temp;

And I have an st7735s display, i want to display like Temp is: ( value of temp)

print_text(5,90,"Temp is:"+temp,1,ST77XX_WHITE); - But when I try this i fail.

Can someone help me get around this issue. I want the variable to stay in the same line as the text with same color and height.

You forgot to write what happens

1.) When I try just to plug in like
print_text(20,5,boost,5,ST77XX_GREEN);

-boost is a variable

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino: In function 'void loop()':
Displej-Test-Print_greske_skoro:87:19: error: cannot convert 'float' to 'char*'
   print_text(20,5,boost,5,ST77XX_GREEN);
                   ^~~~~

And the error that I mentioned.

Displej-Test-Print_greske_skoro:89:33: error: invalid operands of types 'const char [13]' and 'float' to binary 'operator+'
   print_text(5,90,"Temp engine:"+temp,1,ST77XX_WHITE);
                   ~~~~~~~~~~~~~~^~~~~

This is because this is not Javascript and you cannot just add float to const char pointer. Try dtostrf dtostrf() with Arduino - Convert float to string - YouTube

Cant get it to work

Intro

float p = 3.1415926;
float temp=myELM327.engineCoolantTemp();
float usis=myELM327.intakeAirTemp();
float ugrana=myELM327.manifoldPressure();
float pritisak=myELM327.absBaroPressure();
float granausis= (uint8_t)ugrana;
float kpa= (uint8_t)pritisak;
float boost=(granausis-kpa)/100;

dtostrf

dtostrf(temp,3,0,temp_1);
dtostrf(usis,4,0,usis_1);
dtostrf(boost,3,2,boost_1);

Print

print_text(20,5,boost_1,5,ST77XX_GREEN);
  print_text(70,50,"BAR",2,ST77XX_GREEN);
  print_text(5,90,"Temp engine:",temp_1,1,ST77XX_WHITE); 
  print_text(5,100,"Temp usisa:",usis_1,1,ST77XX_BLUE); 
  print_text(146,116,"AM",1,ST77XX_WHITE);

Issues







C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino: In function 'void loop()':
Displej-Test-Print_greske_skoro:92:18: error: 'temp_1' was not declared in this scope
 dtostrf(temp,3,0,temp_1);
                  ^~~~~~
C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:92:18: note: suggested alternative: 'temp'
 dtostrf(temp,3,0,temp_1);
                  ^~~~~~
                  temp
Displej-Test-Print_greske_skoro:93:18: error: 'usis_1' was not declared in this scope
 dtostrf(usis,4,0,usis_1);
                  ^~~~~~
C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:93:18: note: suggested alternative: 'usis'
 dtostrf(usis,4,0,usis_1);
                  ^~~~~~
                  usis
Displej-Test-Print_greske_skoro:94:19: error: 'boost_1' was not declared in this scope
 dtostrf(boost,3,2,boost_1);
                   ^~~~~~~
C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:94:19: note: suggested alternative: 'boost'
 dtostrf(boost,3,2,boost_1);
                   ^~~~~~~
                   boost
C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:99:40: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   print_text(70,50,"BAR",2,ST77XX_GREEN);
                                        ^
C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:102:41: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   print_text(146,116,"AM",1,ST77XX_WHITE);
                                         ^
exit status 1
'temp_1' was not declared in this scope

Please show your whole code.

#include <SPI.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include "bitmaps.h"
#include "bitmapsLarge.h"
#include "BluetoothSerial.h"
#include "ELMduino.h"




#define TFT_CS        5
#define TFT_RST        4 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC         2
BluetoothSerial SerialBT;
#define ELM_PORT   SerialBT
#define DEBUG_PORT Serial


ELM327 myELM327;


uint32_t rpm = 0;


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

float p = 3.1415926;
float temp=myELM327.engineCoolantTemp();
float usis=myELM327.intakeAirTemp();
float ugrana=myELM327.manifoldPressure();
float pritisak=myELM327.absBaroPressure();
float granausis= (uint8_t)ugrana;
float kpa= (uint8_t)pritisak;
float boost=(granausis-kpa)/100;





void setup(void) {
  Serial.begin(9600);
  Serial.print(F("Hello! ST77xx TFT Test"));
 tft.initR(INITR_BLACKTAB);
  tft.setRotation(0);
  tft.fillScreen(ST7735_BLACK);

{
#if LED_BUILTIN
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
#endif

  DEBUG_PORT.begin(115200);
  //SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);
  
  if (!ELM_PORT.connect("OBDII"))
  {
    DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");
    while(1);
  }

  if (!myELM327.begin(ELM_PORT, true, 2000))
  {
    Serial.println("Couldn't connect to OBD scanner - Phase 2");
    while (1);
  }

  Serial.println("Connected to ELM327");
}


//Case 2: Multi Colored Images/Icons
  int h = 160,w = 128, row, col, buffidx=0;
  for (row=0; row<h; row++) { // For each scanline...
    for (col=0; col<w; col++) { // For each pixel...
      //To read from Flash Memory, pgm_read_XXX is required.
      //Since image is stored as uint16_t, pgm_read_word is used as it uses 16bit address
      tft.drawPixel(col, row, pgm_read_word(evive_in_hand + buffidx));
      buffidx++;
    } // end pixel
  }
  delay(5000);
  // Use this initializer if using a 1.8" TFT screen:
 // tft.initR(INITR_BLACKTAB);      // Init ST7735S chip, black tab
  tft.setRotation(1); // set display orientation 
}

void loop() {



  
dtostrf(temp,3,0,temp_1);
dtostrf(usis,4,0,usis_1);
dtostrf(boost,3,2,boost_1);


  tft.fillScreen(ST77XX_BLACK);
  print_text(20,5,boost_1,5,ST77XX_GREEN);
  print_text(70,50,"BAR",2,ST77XX_GREEN);
  print_text(5,90,"Temp engine:",temp_1,1,ST77XX_WHITE); 
  print_text(5,100,"Temp usisa:",usis_1,1,ST77XX_BLUE); 
  print_text(146,116,"AM",1,ST77XX_WHITE);
  
  {
  float tempRPM = myELM327.rpm();

  if (myELM327.nb_rx_state == ELM_SUCCESS)
  {
    rpm = (uint32_t)tempRPM;
    Serial.print("RPM: "); Serial.println(rpm);
  }
  else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
    myELM327.printError();
}
  delay(5000);


  
}

void print_text(byte x_pos, byte y_pos, char *text, byte text_size, uint16_t color) {
  tft.setCursor(x_pos, y_pos);
  tft.setTextSize(text_size);
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);
}

Before use a variable you should declare it. Where are you declare temp_1, usis_1 and boost_1 ?

If i define it as string it still doesnt work.

Print worked just fine, and showed on display. ( wihout variables ofc.)

The easiest way to print a mixture of text and variables is to use separate print statements for each

What size of the strings you define?

  // Try adding these three lines at the top of loop()
  char temp_1[17];
  char usis_1[17];
  char boost_1[17];

Thanks, I've tried but i get errors when compiling.
forbids converting a string constant to 'char*
I think this print won't work I will have to choose another way.

These two lines are also wrong. You can't put two strings in one print_text().

  print_text(5, 90, "Temp engine:", temp_1, 1, ST77XX_WHITE);
  print_text(5, 100, "Temp usisa:", usis_1, 1, ST77XX_BLUE);

You have to print the label and the value separately:

  print_text(5, 90, "Temp engine:",  1, ST77XX_WHITE);
  print_text(50, 90, temp_1, 1, ST77XX_WHITE);

  print_text(5, 100, "Temp usisa:", 1, ST77XX_BLUE);
  print_text(50, 100, usis_1, 1, ST77XX_BLUE);

Thanks, I just tired it. Don't work, I still get error when compiling. I will have to start from scratch.

I've tried it ditching dtostrf, and printing one under other doesnt work.

If you'd like any help with that, you know what to do.

When I put the code like this

#include <SPI.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include "bitmaps.h"
#include "bitmapsLarge.h"
#include "BluetoothSerial.h"
#include "ELMduino.h"




#define TFT_CS        5
#define TFT_RST        4 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC         2
BluetoothSerial SerialBT;
#define ELM_PORT   SerialBT
#define DEBUG_PORT Serial


ELM327 myELM327;


uint32_t rpm = 0;


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

float p = 3.1415926;
float temp=myELM327.engineCoolantTemp();
float usis=myELM327.intakeAirTemp();
float ugrana=myELM327.manifoldPressure();
float pritisak=myELM327.absBaroPressure();
float granausis= (uint8_t)ugrana;
float kpa= (uint8_t)pritisak;
float boost=(granausis-kpa)/100;





void setup(void) {


  Serial.begin(9600);
  Serial.print(F("Hello! ST77xx TFT Test"));
 tft.initR(INITR_BLACKTAB);
  tft.setRotation(0);
  tft.fillScreen(ST7735_BLACK);

{
#if LED_BUILTIN
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
#endif

  DEBUG_PORT.begin(115200);
  //SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);
  
  if (!ELM_PORT.connect("OBDII"))
  {
    DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");
    while(1);
  }

  if (!myELM327.begin(ELM_PORT, true, 2000))
  {
    Serial.println("Couldn't connect to OBD scanner - Phase 2");
    while (1);
  }

  Serial.println("Connected to ELM327");
}


//Case 2: Multi Colored Images/Icons
  int h = 160,w = 128, row, col, buffidx=0;
  for (row=0; row<h; row++) { // For each scanline...
    for (col=0; col<w; col++) { // For each pixel...
      //To read from Flash Memory, pgm_read_XXX is required.
      //Since image is stored as uint16_t, pgm_read_word is used as it uses 16bit address
      tft.drawPixel(col, row, pgm_read_word(evive_in_hand + buffidx));
      buffidx++;
    } // end pixel
  }
  delay(5000);
  // Use this initializer if using a 1.8" TFT screen:
 // tft.initR(INITR_BLACKTAB);      // Init ST7735S chip, black tab
  tft.setRotation(1); // set display orientation 
}

void loop() {

  char temp_1[17];
  char usis_1[17];
  char boost_1[17];


   //D-to-string
  dtostrf(temp,3,0,temp_1);
  dtostrf(usis,4,0,usis_1);
  dtostrf(boost,3,2,boost_1);




  tft.fillScreen(ST77XX_BLACK);

  print_text(20,5,boost_1,5,ST77XX_GREEN);
  print_text(70,50,"BAR",2,ST77XX_GREEN);                 ////Boost

  print_text(5, 90, "Temp engine:",  1, ST77XX_WHITE);
  print_text(70, 90, temp_1, 1, ST77XX_WHITE);            ///Engine_temp

  print_text(5, 100, "Temp usisa:", 1, ST77XX_BLUE);      ///Intake_temp
  print_text(70, 100, usis_1, 1, ST77XX_BLUE);
   
  print_text(146,116,"AM",1,ST77XX_WHITE);
  
  {
  float tempRPM = myELM327.rpm();

  if (myELM327.nb_rx_state == ELM_SUCCESS)
  {
    rpm = (uint32_t)tempRPM;
    Serial.print("RPM: "); Serial.println(rpm);
  }
  else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
    myELM327.printError();
}
  delay(5000);


I get these errors

Arduino: 1.8.19 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 40MHz, 4MB (32Mb), 921600, Core 1, Core 1, None"





C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino: In function 'void loop()':

Displej-Test-Print_greske_skoro:109:3: error: 'print_text' was not declared in this scope

   print_text(20,5,boost_1,5,ST77XX_GREEN);

   ^~~~~~~~~~

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:109:3: note: suggested alternative: 'sbintime_t'

   print_text(20,5,boost_1,5,ST77XX_GREEN);

   ^~~~~~~~~~

   sbintime_t

Displej-Test-Print_greske_skoro:131:14: error: expected '}' at end of input

   delay(5000);

              ^

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:92:13: note: to match this '{'

 void loop() {

             ^

exit status 1

'print_text' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

EDIT: Here I was missing curly bracket

When I ditch d to string and char
code be like this

#include <SPI.h>
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include "bitmaps.h"
#include "bitmapsLarge.h"
#include "BluetoothSerial.h"
#include "ELMduino.h"




#define TFT_CS        5
#define TFT_RST        4 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC         2
BluetoothSerial SerialBT;
#define ELM_PORT   SerialBT
#define DEBUG_PORT Serial


ELM327 myELM327;


uint32_t rpm = 0;


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

float p = 3.1415926;
float temp=myELM327.engineCoolantTemp();
float usis=myELM327.intakeAirTemp();
float ugrana=myELM327.manifoldPressure();
float pritisak=myELM327.absBaroPressure();
float granausis= (uint8_t)ugrana;
float kpa= (uint8_t)pritisak;
float boost=(granausis-kpa)/100;





void setup(void) {


  Serial.begin(9600);
  Serial.print(F("Hello! ST77xx TFT Test"));
 tft.initR(INITR_BLACKTAB);
  tft.setRotation(0);
  tft.fillScreen(ST7735_BLACK);

{
#if LED_BUILTIN
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
#endif

  DEBUG_PORT.begin(115200);
  //SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);
  
  if (!ELM_PORT.connect("OBDII"))
  {
    DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");
    while(1);
  }

  if (!myELM327.begin(ELM_PORT, true, 2000))
  {
    Serial.println("Couldn't connect to OBD scanner - Phase 2");
    while (1);
  }

  Serial.println("Connected to ELM327");
}


//Case 2: Multi Colored Images/Icons
  int h = 160,w = 128, row, col, buffidx=0;
  for (row=0; row<h; row++) { // For each scanline...
    for (col=0; col<w; col++) { // For each pixel...
      //To read from Flash Memory, pgm_read_XXX is required.
      //Since image is stored as uint16_t, pgm_read_word is used as it uses 16bit address
      tft.drawPixel(col, row, pgm_read_word(evive_in_hand + buffidx));
      buffidx++;
    } // end pixel
  }
  delay(5000);
  // Use this initializer if using a 1.8" TFT screen:
 // tft.initR(INITR_BLACKTAB);      // Init ST7735S chip, black tab
  tft.setRotation(1); // set display orientation 
}

void loop() {


  tft.fillScreen(ST77XX_BLACK);

  print_text(20,5,boost,5,ST77XX_GREEN);
  print_text(70,50,"BAR",2,ST77XX_GREEN);                 ////Boost

  print_text(5, 90, "Temp engine:",  1, ST77XX_WHITE);
  print_text(70, 90, temp, 1, ST77XX_WHITE);            ///Engine_temp

  print_text(5, 100, "Temp usisa:", 1, ST77XX_BLUE);      ///Intake_temp
  print_text(70, 100, usis, 1, ST77XX_BLUE);
   
  print_text(146,116,"AM",1,ST77XX_WHITE);
  
  {
  float tempRPM = myELM327.rpm();

  if (myELM327.nb_rx_state == ELM_SUCCESS)
  {
    rpm = (uint32_t)tempRPM;
    Serial.print("RPM: "); Serial.println(rpm);
  }
  else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
    myELM327.printError();
}
  delay(5000);

}
  void print_text(byte x_pos, byte y_pos, char *text, byte text_size, uint16_t color) {
  tft.setCursor(x_pos, y_pos);
  tft.setTextSize(text_size);
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);
}

I get this error

Arduino: 1.8.19 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 40MHz, 4MB (32Mb), 921600, Core 1, Core 1, None"





















C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino: In function 'void loop()':

Displej-Test-Print_greske_skoro:97:19: error: cannot convert 'float' to 'char*'

   print_text(20,5,boost,5,ST77XX_GREEN);

                   ^~~~~

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:122:47: note:   initializing argument 3 of 'void print_text(byte, byte, char*, byte, uint16_t)'

   void print_text(byte x_pos, byte y_pos, char *text, byte text_size, uint16_t color) {

                                         ~~~~~~^~~~

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:98:40: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

   print_text(70,50,"BAR",2,ST77XX_GREEN);                 ////Boost

                                        ^

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:100:53: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

   print_text(5, 90, "Temp engine:",  1, ST77XX_WHITE);

                                                     ^

Displej-Test-Print_greske_skoro:101:22: error: cannot convert 'float' to 'char*'

   print_text(70, 90, temp, 1, ST77XX_WHITE);            ///Engine_temp

                      ^~~~

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:122:47: note:   initializing argument 3 of 'void print_text(byte, byte, char*, byte, uint16_t)'

   void print_text(byte x_pos, byte y_pos, char *text, byte text_size, uint16_t color) {

                                         ~~~~~~^~~~

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:103:51: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

   print_text(5, 100, "Temp usisa:", 1, ST77XX_BLUE);      ///Intake_temp

                                                   ^

Displej-Test-Print_greske_skoro:104:23: error: cannot convert 'float' to 'char*'

   print_text(70, 100, usis, 1, ST77XX_BLUE);

                       ^~~~

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:122:47: note:   initializing argument 3 of 'void print_text(byte, byte, char*, byte, uint16_t)'

   void print_text(byte x_pos, byte y_pos, char *text, byte text_size, uint16_t color) {

                                         ~~~~~~^~~~

C:\Users\X260\Documents\Arduino\Displej-Test-Print_greske_skoro\Displej-Test-Print_greske_skoro.ino:106:41: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

   print_text(146,116,"AM",1,ST77XX_WHITE);

                                         ^

exit status 1

cannot convert 'float' to 'char*'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

That's an easy one to figure out. Put the expected '}' at the end of the file.

If only there was a way to convert a 'float' variable to a character array. Oh yes! dtostrf(float,3,0,charBuffer);