Trying to display BMP085(Temp and Pressure) on TFT Display

Hi,

I am trying to display the Temperature and the Air pressure of a BMP085 on an Arduino 1.77 TFT Display, but I am getting strange results. I believe my issue is arising from my conversion of float variables to a char arrays.

My code is as follows:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Console.h>
#include <TFT.h>
#include <SPI.h>

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

// pin definition for the Arduino YUN
#define sd_cs  8
#define lcd_cs 7
#define dc     5
#define rst    6

TFT TFTscreen = TFT(lcd_cs, dc, rst);

char LCD_Temp[5];
char LCD_Pres[6];

void displaySensorDetails(void)
{
  sensor_t sensor;
  bmp.getSensor(&sensor);
  Console.println("------------------------------------");
  Console.print  ("Sensor:       "); Console.println(sensor.name);
  Console.print  ("Driver Ver:   "); Console.println(sensor.version);
  Console.print  ("Unique ID:    "); Console.println(sensor.sensor_id);
  Console.print  ("Max Value:    "); Console.print(sensor.max_value); Console.println(" hPa");
  Console.print  ("Min Value:    "); Console.print(sensor.min_value); Console.println(" hPa");
  Console.print  ("Resolution:   "); Console.print(sensor.resolution); Console.println(" hPa");  
  Console.println("------------------------------------");
  Console.println("");
  delay(500);
}

void setup(void) 
{
  Bridge.begin();
  Console.begin();
  
  TFTscreen.begin();
  TFTscreen.background(255, 255, 255);

  TFTscreen.stroke(0, 0, 255);
  TFTscreen.println();
  TFTscreen.println("Pressure Sensor Test");
  TFTscreen.stroke(0, 0, 0);
  TFTscreen.println("Open console monitor");
  TFTscreen.println("to run the sketch");
  
  while (!Console){
    ; 
  }
  
  Console.println("Pressure Sensor Test"); Console.println("");
  
  if(!bmp.begin())
  {
   Console.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
  
  displaySensorDetails();
  TFTscreen.background(0, 0, 0);
}

void loop(void) 
{
   sensors_event_t event;
  bmp.getEvent(&event);
 
  if (event.pressure)
  {
    float pressure;
    float pascal;
    bmp.getPressure(&pascal);
    pressure=(pascal/100);
    Console.print("Pressure:    ");
    Console.print(pressure);
    Console.println(" hPa");
    dtostrf(pressure,5,2,LCD_Pres);
        
    float temperature;
    bmp.getTemperature(&temperature);
    Console.print("Temperature: ");
    Console.print(temperature);
    Console.println(" C");
    dtostrf(temperature,4,2,LCD_Temp);
    

    Console.println("-----------------------");
  }
  else
  {
    Console.println("Sensor error");
  }
  
  TFTscreen.stroke(255,255,255);
  TFTscreen.setTextSize(2);
  TFTscreen.text("1/3",60,0);
  TFTscreen.text("Temperature",10,20);
  TFTscreen.text("(C)",60,40);
  TFTscreen.setTextSize(5);
  TFTscreen.text(LCD_Temp,5,90);
  
  delay(2000);
  TFTscreen.background(0, 0, 0);
  
  TFTscreen.stroke(255,255,255);
  TFTscreen.setTextSize(2);
  TFTscreen.text("2/3",60,0);
  TFTscreen.text("Pressure",30,20);
  TFTscreen.text("(hPa)",50,40);
  TFTscreen.setTextSize(2);
  TFTscreen.text(LCD_Pres,0,90);
  
  
  delay(5000);
  TFTscreen.background(0, 0, 0);
}

From what I have found on the Internet and on the reference page of arrays, the size of the char array must be one more element than the number of 'entries' in the array ?

...one more element than your initialization is required, to hold the required null character

So if I understand this correctly:
If Temp is 4 characters eg xx.xx, then the array size should be 5 ?
If the Pressure is 5 characters eg xxx.xx, then the array size should be 6 ?

char LCD_Temp[5];
char LCD_Pres[6];

This does not seems to work, it displays the Temp correctly, but nothing for the Pressure.

If I go and change:

char LCD_Temp[6];
char LCD_Pres[6];

Then Temp is displayed correctly and the Humidity is display, but with two funny characters after it.

If I go and change:

char LCD_Temp[4];
char LCD_Pres[5];

Then Temp is displayed correctly, but the only one digit of the Humidity is displayed and it is not even a relative digit to the current reading.

If someone would be so kind as to point me in the correct direction ?
Thanks
Gregg

If Temp is 4 characters eg xx.xx, then the array size should be 5 ?

xx.xx is five characters, so your array would need to be six chars long. The last element of the array is supposed to contain a zero ( the binary number zero, not the character '0' ).

Thank-you very much, it works!!!

Ah, I did not count the Decimal Point: $me="chop!!";

Thanks again for the help