float to integer reading from BMP085

Hi all,

I am fairly new with arduino and have problem converting the pressure reading from a BMP085 (BMP085 Barometric Pressure/Temperature/Altitude Sensor- 5V ready : ID 391 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits) from a float value to an integer. Using the library at Github (https://github.com/adafruit/BMP085-Library), the pressure value taken is a float value and I need to convert it to an integer.

This code:

pressure = bmp.readPressure();
Serial.println(pressure);

prints correct float values:

100669.00
100672.00
...etc...

If I try casting like this:

pressure = bmp.readPressure();
int press_int = (int) pressure;
Serial.println(press_int);

I get:

-30410
-30407
...etc...

Any idea why? I am new to C++ but have been trying to work this out for 3 days now and I would really appreciate any help.

Try casting to a long.

if that doesn't work you can use some basic arithmetic to convert it manually. If the long cast doesn't work we can try something else.

100672 is too big to fit in an int, hence the funky results you're seeing. As EvolveElectronics points out, using a long will solve this. Alternatively, why do you want it in an int? I suspect you're looking for a number in millibars, so just divide the pressure reading by 100.0 before you cast it to int and you should be fine.

Hey, thanks wildbill and EvolveElectronics for the replies.

Oh yes, 6 digits is too big for an int. That explains the weird number.

My ultimate goal is to simply display the number to my LCD display (1.8 Color TFT LCD display with MicroSD Card Breakout [ST7735R] : ID 358 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits) in which the codes tft.drawString seems to accept character strings only. The conversion is just my work around since I only know how to convert int to character string such as this:

temperature = bmp.readTemperature()*10;
int tempwhole = bmp.readTemperature();
int tempdec = temperature % 10;
char tempall[10];
sprintf(tempall, "%.d.%.d C", tempwhole, tempdec);
tft.fillRect(10, 70, 100, 8, BLACK);
tft.drawString(10, 70, tempall, BLUE, 1); //prints 29.1 C

I know (not really sure :D) that this is not efficient but works for the meantime. But doing the same with the barometric pressure like:

pressure = bmp.readPressure();
float pressfwhole = pressure/1000;
int presswhole = (int)pressfwhole;
float pressfdec = pressure - presswhole*1000; //pressure % 1000 does not work
int pressdec = (int)pressfdec;
char pressall;
sprintf(pressall, "%.d.%.3d kPa", presswhole, pressdec); //gives error
tft.fillRect(10, 110, 100, 8, BLACK);
tft.drawString(10, 110, pressall, GREEN, 1);

gives an error: invalid conversion from 'char' to 'char*', to the line sprintf

however, commenting out char pressall; and down and adding serial print line:

Serial.print(presswhole);Serial.print(".");Serial.print(pressdec);Serial.println(" kPa");

gives out correct result as:

100.669 kPa
100.672 kPa
...etc...

Having said this, the conversion to long works fine but the problem still stands.

Any solution to either the float conversion or character string printing on the LCD?

char pressall [MAX_PRESSURE_STRING_SIZE];

Aarg...my stupid mistake! :.

It works fine now. Thanks awol!

Thank you all!