I am reading values such as GPS and environmental data which will be ‘float’ format. I have a small 0.96" OLED (SSD1315 monochrome) using the U8g2 library.
I can display ‘dummy’ values such as “123.456789” if they are text but the library doesn’t support float values. If I try to use a float it fails to compile (as expected really):
"Compilation error: no matching function for call to ‘U8G2_SSD1306_128X64_NONAME_F_HW_I2C::drawStr(int, int, float&)’
Is there an easy way to do this? ie convert a float to a text string? Possibly one before the decimal point and one after? Or some other solution?
#include <U8g2lib.h>
[other code etc in here]
float testval=123.456789
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0,20,"Sample"); //text works fine
u8g2.drawStr(0,30,"Date 23/12/2024"); //text works fine
u8g2.drawStr(0,40,"GPS Lo 0.00 La 0.00"); //DUMMY 'values' for GPS in text format works fine
u8g2.drawStr(0,50,"Light " light" Lux"); //text works fine
u8g2.drawStr(0,60,"T \ H 15C 30%"); //DUMMY 'values' for Temp and Humidity in text format works fine
u8g2.drawStr(0,10,testval); //FAILS trying to send a real value but it is in 'float' format
u8g2.sendBuffer();
I’m using the Arduino Nano Every based on ATMega4809. Is that the answer to your question?
I am wondering if the display would work with another library that allows a ‘print’ function to an OLED of a numerical value straight from a variable, rather than converting it to text first?
Although the MegaCoreX core does support printf (yay!) it doesn't have float support enabled (boo!). So you'll either have to go the usual dtostrf route or figure out how to add float support to the core, at which time snprintf would become an option.
I was looking in the MegaCoreX core all right, but I had compiled with the arduino:megaavr:nona4809 board. Different core; mea culpa!
Oh, cool. Perhaps that library has float support built in itself.
In any case, I just realized that I was using arduino:megaavr rather than MegaCoreX so my initial statement is very likely wrong.
Edit: As @david_2018 pointed out, the library inherits from Print, so of course print(float_var) works. The cold here has apparently seeped into my brain!