Afternoon everyone
I've got a HTU21D temperature and humidity sensor set up to display to a 128x32 OLED using the U8G2 library. The HTU21D readings come in as floats, so I can't pass them to the drawStr() command. I know I can use "print()" but that doesn't let me find the number of pixels that it will take up before sending it to the screen.
What I want to do is convert the float to a char array. I have tried using sprintf but my code isn't working.
This is the code I'm using to read the sensor and attempt to convert it:
float humd = tempSensor1.readHumidity();
char humd1[10];
sprintf(humd1, "%f.0", humd);
Serial.println(humd);
Serial.println(humd1);
The output of the serial monitor shows the following:
49.53
?.0
What am I doing wrong?
use dtostrf...
dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
where
floatvar float variable
StringLengthIncDecimalPoint This is the length of the string that will be created
numVarsAfterDecimal The number of digits after the deimal point to print
charbuf the array to store the results
for example:
float f_val = 123.6794;
char outstr[15];
void setup() {
dtostrf(f_val,7, 3, outstr);
Serial.begin(9600);
Serial.println(outstr);
}
void loop(){
}
hope that helps
Float support is omitted for space reasons from sprintf.
wildbill:
Float support is omitted for space reasons from sprintf.
... and it compiles but doesn't function as it should? Wonderful.
pianomatt:
that doesn't let me find the number of pixels that it will take up before sending it to the screen.
OK, I have to ask: Why do you need to do that?
sherzaad:
use dtostrf...
dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
where
floatvar float variable
StringLengthIncDecimalPoint This is the length of the string that will be created
numVarsAfterDecimal The number of digits after the deimal point to print
charbuf the array to store the results
for example:
float f_val = 123.6794;
char outstr[15];
void setup() {
dtostrf(f_val,7, 3, outstr);
Serial.begin(9600);
Serial.println(outstr);
}
void loop(){
}
hope that helps
Many thanks, that worked a treat. Figured out that the numbers are for the number of digits either side of the decimal point.
Nick_Pyner:
OK, I have to ask: Why do you need to do that?
So that I could right-justify the sensor readings on the display. Take the width of the display, subtract the expected width of the string to be drawn and print it to that point. I couldn't find anything in the library that lets you do it.
pianomatt:
I couldn't find anything in the library that lets you do it.
There is just a faint possibility that you couldn't find anything because nobody else does it, and you don't need to.
I'm not familiar with that library, and I won't quibble about the need to use it, but I submit that, if you can't get a display looking like that using just the cursor control like everybody else does, you are not trying hard enough, or are simply on the wrong tram. There should be an example in the library that shows how to do it without having to engage in the above saga. A library like that should have a right align command for text and, even if it doesn't, you can condition the left-aligned text with leading blanks like you do in a plain vanilla character display.
Nick_Pyner:
There is just a faint possibility that you couldn't find anything because nobody else does it, and you don't need to.
I'm not familiar with that library, and I won't quibble about the need to use it, but I submit that, if you can't get a display looking like that using just the cursor control like everybody else does, you are not trying hard enough, or are simply on the wrong tram. There should be an example in the library that shows how to do it without having to engage in the above saga. A library like that should have a right align command for text and, even if it doesn't, you can condition the left-aligned text with leading blanks like you do in a plain vanilla character display.
I did a fair bit of searching posting in here, both on the general web and the library git page. There is right to left language support but that would print the characters in reverse order as well.
Because the font is not monotype you can't just count the characters to work the padding. I tried eyeballing it but subtracting the width of the string from the overall screen width gets it right on the edge every time, even if the numerical values go down to single digits.
Left<>right printing is not the same as aligning. One library is much the same as another, and I imagine any library with pixel resolution will have commands for left-centre-right text align. See what Henning Karlsen has to offer at RinkyDink. If you right-align, you will still need to consider blank padding, irrespective of the type of font. I imagine you would put the padding in even if you don't need it.