# Pb with decimal

Hi everybody, (excuse my bad english, I’m french, nobody is perfect !)

I want to display numeric value with a monochrome Oled SH1106, and I use Olikraus’s library u8g2, and :

e.g.

float A = 1.234;
int d = 2;
u8g2.print(A,d);

→ output 1.00 :o

and with the command
Serial.print(A,d);

→ output 1 ;D …that I want

An idea ?
Mike

d appears to be the number of decimal places to print, as it is with Serial.print() so what about

u8g2.print(A,0);


or cast A to an int

u8g2.print((int)A);


It does, however, raise the question as to why A is a float in the first place rather than an integer ?

I get a different result from your code.

float A = 1.234;
int d = 2;
void setup()
{
Serial.begin(115200);
while (!Serial) ;

Serial.print(A, d);
//  --> output 1    ;D  ..................that  I want
}

void loop() {}


The output I get on Serial Monitor is:

1.23


Exactly what I would expect.

Yes @johnwasser,

you obtain 1.23 with Serial.print because d=2, but if d=0 you must obtain 1 (the result I want with u8g2library).

if you use u8g2library with oled display, the result is different, with d=0, you get 1.00 on the screen, with d=1 ou get 1.20 , and I don't want the decimal point and the two zeroes.

Olikraus said that U8g2 supports the standard Arduino Print command, but I think not :

The result Serial.print(integer,0); is different from u8g2.print(integer,0);

Mike

Sorry, in my first post , I do a mistake, you read d=0 (and not d=2)

mikegiver:
Olikraus said that U8g2 supports the standard Arduino Print command

That class does indeed inherit from the Print class.

mikegiver:
, but I think not :

The result Serial.print(integer,0); is different from u8g2.print(integer,0);

You haven’t shown any evidence of that.

HI,

here the serial output —>

and here the Oled display —>

and here the arduino program

#include "Arduino.h"

/*
Takes an input on the Teensy 3.2 from analog ADC input A14
and outputs it using u8g2library & I2C Oled
*/

#include "Arduino.h"
#include <U8g2lib.h>
#include <U8x8lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

//////////////////              ECRAN           ///////////////////////

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

/////          MPXV5004DP
//              _______
//             =|      |=----- A14  ADC converter 13bits res. (Teensy 3.2)
//             =|      |=----- GND
//             =|      |=----- 3.3v
//              |------|       0.1 mf between GND & 3.3v
//               ||  ||
//

int16_t analogPin = A14;  // MPXV5004DP Vout pin - A14   ADC converter
double a14OffsetV = 0.0;  // variable for V offset (autozero)
int16_t airSpeed = 0;

void setup () {

// Initialize I2C and OLED Display

Serial.begin(115200);
analogReadResolution(13);                     // 3.3v ref   8192 points (13 bits)
a14OffsetV = analogRead(analogPin) * .0004;  // Zero speed sensor reading * 0.61 mV per count
u8g2.begin();              ////
u8g2.enableUTF8Print();    ////    enable UTF8 support for the Arduino print()

///////////////////////////////////  ecran blanc
u8g2.setDrawColor(1);
u8g2.drawBox(0,0,130,70);
u8g2.sendBuffer();
u8g2.setFont(u8g2_font_profont22_tf );
u8g2.setDrawColor(0);
u8g2.drawStr(5,20,"AirSpeed:");   // affichage de la 1ere ligne
u8g2.sendBuffer();
delay(2000);

Serial.print("a14Offset = ");
Serial.println(a14OffsetV);
}
void loop () {

//  float density = (externalpressure * DRY_AIR_MOLAR_MASS) / (temperature * UNIVERSAL_GAS_CONSTANT);
//  return sqrt ((2 * (frontpressure - externalpressure)) / density);

double compVOut = 0.0;
double dynPress = 0.0;
double airSpeed = 0.0;
int speed = 0;
float IN = analogRead(analogPin) * 0.0004;
Serial.print("V input = ");
Serial.println(IN);
compVOut = IN - a14OffsetV;
Serial.print("compVOut =");
Serial.println(compVOut);
if(compVOut < .005)  {                    // Set noise to 0, min speed is ~8mph********* à vérifier
compVOut = 0.0;
}
dynPress = compVOut * 1000.0;             // With autozero, dynamic pressure in kPa = Vout, convert kPa to P

airSpeed = sqrt((2 * dynPress)/1.225);   // Converts pressure to m/s, 1.225 k/m3 is standard air density
//speed = airSpeed * 3.6;  // convert m/s en km/h
speed = airSpeed * 1.943;  //  convert en Knots

Serial.print("Speed (m/S): ");
Serial.println(airSpeed);
Serial.print("*********************Speed (mph): ");
Serial.println((airSpeed * 2.236),1);  // m/s to mph
Serial.print("Speed (knots): ");
Serial.println((airSpeed * 1.943), 1);
Serial.print("Speed (km/h): ");
Serial.println((airSpeed * 3.6), 0);
Serial.println();

Serial.print("  ");
u8g2.setDrawColor(1);
u8g2.drawBox(1,25,64,30);
u8g2.setDrawColor(0);
u8g2.drawStr(28,50,"0.00");
u8g2.sendBuffer();
if (int((airSpeed * 3.6)) < 100) { u8g2.setCursor(16,50);}
else if  (int((airSpeed * 3.6)) > 99) { u8g2.setCursor(4,50);}

if ((airSpeed * 3.6) > 25) {
u8g2.setDrawColor(0);
//   u8g2.print((airSpeed * 1.943), 1);   // knots
u8g2.print((airSpeed * 3.6), 0);      // km/h
u8g2.drawStr(80,50,"Km/h");
u8g2.sendBuffer();

}

delay(500);

}


you can see well :

Mike

By which process do you expect the

   u8g2.drawStr(28,50,"0.00");


to vanish miraculously?

   if (int((airSpeed * 3.6)) < 100)
{ u8g2.setCursor(16,50);}
else if  (int((airSpeed * 3.6)) > 99)   //?????????????????
{ u8g2.setCursor(4,50);}


Why check for >99 when you already know the value is >= 100?

Congratulations @Whandall,

at the beginning, I displayed the speed with 2 decimals and as it was not useful, I changed the display code > 25 but not the “0.00” display (the display not being refreshed the data persisted) …thank you again.

I shift to the left by one character for values > 99 to have the decimal units always in the same place on the display (aesthetic) in relation to the text “Km/h”

Yesterday I do a test with my car and I put this arduino airspeed with another eagletree airspeed in paralell.

At 90km/h, the arduino airspeed indicates 45km/h and eagletree indicates 85km/h (this last value seems good, because all cars's speedometers are allways optimist)

I must calibrate the arduino speedometer .

I don't see how to edit the title of this post and mark it "SOLVED" ?

Edit the title of your original post