Displaying temperature and pressure at the same time using java

include

include "Seeed_BMP280.h"

BMP280 bmp280;

define analogPin A0 //the thermistor attach to

define beta 3950 //the beta of the thermistor

define resistance 10 //the value of the pull-up resistor

void setup() {

Serial.begin(9600); if(!bmp280.init()) Serial.println("Device error!"); }

void loop() {

long a =1023 - analogRead(analogPin); int sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0; float c = (bmp280.getPressure()); unsigned long sensor2 = c;

char text [40]; sprintf (text, "%d, %d\n", sensor1, sensor2); Serial.println (text);

delay(1000); //wait for 100 milliseconds

}

You need to let sprintf know that it's being passed an unsigned long:

  sprintf (text, "%d, %lu\n", sensor1, sensor2);

Yes!!!! Thank you so much to you too!!! Thanksss guys you really made my day happier!

You wouldn't have had the problem if you'd just used Serial.print :)

how could I do that? Can you exemplify, please? :slight_smile:

One more thing guys: In the temperature values, I only get exact values like 24,25,26 and not 23.4 , 23.9, 24.56,etc. etc. Do you know why?

You're storing the result of your temperature calc in an integer. By definition, integers don't have a fractional component. Use float instead.

wildbill: You're storing the result of your temperature calc in an integer. By definition, integers don't have a fractional component. Use float instead.

Can you help me with that? I tried to change the code using float but I dont displays the right values

#include 
#include "Seeed_BMP280.h"
BMP280 bmp280;

#define analogPin A0 //the thermistor attach to 
#define beta 3950 //the beta of the thermistor
#define resistance 10 //the value of the pull-up resistor

void setup() {


  Serial.begin(9600);
 if(!bmp280.init())
    Serial.println("Device error!");
}

void loop() {

 long a =1023 - analogRead(analogPin); 
 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c =  bmp280.getPressure();
 unsigned long sensor2 = c;
  
  char text [40];
  sprintf (text, "%s, %lu\n", sensor1, sensor2);
  Serial.println (text);

  delay (500);
}

You can't use sprintf with floats easily on AVRs, but you'd need to use %f if you could.

As I pointed out earlier, you don't need sprintf.

 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 unsigned long sensor2 = c;
  sprintf (text, "%s, %lu\n", sensor1, sensor2);

The format specifier for a float is NOT %s. That is for strings.

By the way, by default, support for floats in sprintf() is disabled.

I assume you're using sprintf because it was in the example code you copied. As mentioned above, you really don't need it:

 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c =  bmp280.getPressure();
 Serial.print(Sensor1);
 Serial.print(", ");
 Serial.println(c);

Not tested. Not even compiled.

wildbill: I assume you're using sprintf because it was in the example code you copied. As mentioned above, you really don't need it:

 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c =  bmp280.getPressure();
 Serial.print(Sensor1);
 Serial.print(", ");
 Serial.println(c);

Not tested. Not even compiled.

Thank you so much guys again! I learnt one more thing thanks to you both!! It worked!! Now I will try to display on a LCD, wish me luck :D ahah

Guys, I manage to succesfully display the temperature and pressure on LCD. I have one final question, the pressure units that I get, are in pascal. How can I put them on bar? I know that 1 pascal = 0.00001 bar. Can I change the code in order have the pressure values in bar?

#include 
#include "Seeed_BMP280.h"
BMP280 bmp280;

#define analogPin A0 //the thermistor attach to 
#define beta 3950 //the beta of the thermistor
#define resistance 10 //the value of the pull-up resistor

#include 
#include                        // main hd44780 header
#include  // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip



// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

void setup()
{

  Serial.begin(9600);
 if(!bmp280.init())
    Serial.println("Device error!");
    
int status;

    status = lcd.begin(LCD_COLS, LCD_ROWS);
    if(status) // non zero status means it was unsuccesful
    {
        status = -status; // convert negative status value to positive number

        // begin() failed so blink error code using the onboard LED if possible
        hd44780::fatalError(status); // does not return
    }


}

void loop() {
  
  long a =1023 - analogRead(analogPin); 
   float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c =  bmp280.getPressure();
 Serial.print(sensor1);
 Serial.print(", ");
 Serial.println(c);

  delay (500);
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(sensor1); // Print a centigrade temperature to the LCD.
 lcd.setCursor(0, 1);
  lcd.print("Press:");
  lcd.print(c);

  
  }

Just divide your c variable by 100000.0 before displaying it.

BTW, single letter variable names are fine when your program is twenty lines long, but it's not a good habit to get into.

  long a =1023 - analogRead(analogPin);
   float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;

I hope a never equals 10.

wildbill: Just divide your c variable by 100000.0 before displaying it.

BTW, single letter variable names are fine when your program is twenty lines long, but it's not a good habit to get into.

Thanks a lot, really!!!

PaulS:   long a =1023 - analogRead(analogPin);   float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;

I hope a never equals 10.

Can you explain, please? ahah

Guys, I am now with another problem. I connected now 3 sensors to the arduino mega, 2 thermistors and 1 barometer and I wanted to display the data in the same format than the previous one (value, value, value). I tried writing on the code Serial.print (", , ") but it didnt worked. Also I tried writing Serial.print (", ") one time between the sensor 1 and 2 and the other between the sensor 2 and 3 but it isnt working. Does anyone know what to do?

#include 
#include "Seeed_BMP280.h"
BMP280 bmp280;


#define analogPin A0 //the thermistor attach to 
#define beta 3950 //the beta of the thermistor
#define resistance 10 //the value of the pull-up resistor

#include 
#include                        // main hd44780 header
#include  // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip



// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

void setup()
{

  Serial.begin(9600);
   if(!bmp280.init())
    Serial.println("Device error!");

    
int status;

  status = lcd.begin(LCD_COLS, LCD_ROWS);
  if(status) // non zero status means it was unsuccesful
  {
    status = -status; // convert negative status value to positive number

    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }

  
}

void loop() {

  // sensor1 = thermistor number 1 ; sensor2 = thermistor number 2 ; sensor3 = barometer 
  long a =1023 - analogRead(analogPin); 
   float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
long d = 1023 - analogRead (A7);
float sensor2 = beta /(log((1025.0 * 10 / d - 10) / 10) + beta / 298.0) - 273.0;
 float sensor3 =  bmp280.getPressure();
 Serial.print(sensor1);
 Serial.print(", ");
 Serial.println(sensor2);
 Serial.print (", ");
 Serial.println (sensor3);

  delay (500);
lcd.setCursor(0, 0);
lcd.print("Temp1:");
lcd.print(sensor1);// Print a centigrade temperature to the LCD.
lcd.print(" C");
 lcd.setCursor(0, 1);
  lcd.print("Temp2:");
  lcd.print(sensor2);
  lcd.print (" C");

This:

 Serial.println(sensor2);

Needs to change now to:

 Serial.print(sensor2);

brunopeniche: Can you explain, please? ahah

Because then you'll have a division by zero:

float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;