Writing a float to a display

I have a sketch that measure temperature and I want to display it on an OLED.

If I calculate an interger I can use ITOA and a buffer to display the numbers.

But if I need a float as in the sketch below it obviously wont convert the same way.

Is there another simple convert function?I dont need to display decimals.

setup not shown for globals etc.

Temp calculator is;

void watertemp() {
  int Vo;
  float R1 = 11000;
  float logR2, R2;//temperature now global
  float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
  Vo = analogRead(waterSenderPin);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  temperature = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  temperature = temperature - 273.15;
  // temperature = (temperature * 9.0)/ 5.0 + 32.0;

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" C");
  temperature = map(Vo, 400, 1023, 0, 255);
  analogWrite(watertempoutputpin, temperature);
  //analogWrite(watertempoutputpin, 255);//test
  delay(500);
}

the display section is then

watertemp();
    tft.setCursor(30, 5);
    tft.setTextColor(WHITE, BLACK);
    tft.setTextSize(2);
    tft.println("Temp");
    tft.setTextSize(4);
    tft.setCursor(30, 30 );
    tft.setTextColor(WHITE, BLACK); // refreshes text area
    itoa(temperature, buf6, 10);
    tft.println(itoa(temperature, buf6, 10));

Hi,

You could try it with dtostrf():

dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);

what's going on here?:

    itoa(temperature, buf6, 10);
    tft.println(itoa(temperature, buf6, 10));

the first line is redundant

You say you don't need decimals. Then why do you need a float?

If you really want to print a float, which is far from clear, you should find an example of that included in whatever library you are using, and it should be a lot simpler than what you are doing. I can't imagine there is a library that cannot do this, and you are probably just not looking hard enough.

First thing to check is if the

kpg:
setup not shown for globals etc.

Too bad. There's likely some important information there that could have helped us help you.

Check the class defined by the library. Does it inherit from the Print class? If so, it already knows how to print a float.

Sorry guys, I am a bit new to this. I probably generated some confusion.

The float is needed in the original temp calculation. I had hoped to just print that to the OLED.

That said, as I dont need a decimal point and doing some more digging after your comments, I believe I can use CAST to get an integer from the float then use ITOA again?

Yes, you can indeed use a CAST operator, for instance like: (int) float
But that will just truncate the float removing the decimal part.
So depending on the required accuracy this might be acceptable.

Why not provide complete information? Post a GitHub link the the exact display library you're using.

Thankyou for the CAST info.

The OLED libraries are

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>

I installed them through the library manager so not sure where they are on Github sorry.

Thew final reporting of temperature to the display does not need the decimal. I will try the CAST.

That's the great part about providing complete information. With a little digging, you'll see that Adafruit_SSD1351 inherits from Adafruit_SPITFT:

class Adafruit_SSD1351 : public Adafruit_SPITFT {

Adafruit_SPITFT inherits from Adafruit_GFX:

class Adafruit_SPITFT : public Adafruit_GFX {

And Adafruit_GFX inherits from Print

class Adafruit_GFX : public Print {

So, it already knows how to print a float (as I conjectured in Reply #4). So, all you have to do is:

tft.println(temperature);

or:

tft.println(temperature, n);

where 'n' is the number of digits you want to the right of the decimal point (could be 0 if you like). The result will be rounded accordingly. No conversions or casts required.

Thank you.

I am just learning about navigating libraries...appreciate the help. It work well.

Sorry 1 last question so that I totally understand....

So how does

class Adafruit_GFX : public Print {

tell me it is able to print floats directly?

Because it inherits from the Print class

kpg:
Sorry 1 last question so that I totally understand....

So how does

class Adafruit_GFX : public Print {

tell me it is able to print floats directly?

Since Adafruit_GFX inherits from Print, it knows how to do what Print does. From that, there are a couple of ways to determine it can print a float. These you will only pick up from experience:

  • You know that the standard "Serial" object is of a class that also (indirectly) inherits from Print. So, since Serial's class and Adafruit_GFX both inherit from the same base class AND Serial knows how to print a float, then chances are that objects of the Adafruit_GFX class do also.

  • You could look inside Print.h and find the prototype:

size_t print(double, int = 2);

On an 8-bit AVR, float and double are the same thing. Even on 32-bit processors, a float will be implicitly up-cast to a double when passed to this function.