Go Down

Topic: Displaying temperature and pressure at the same time using java  (Read 635 times) previous topic - next topic

brunopeniche

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

Code: [Select]
#include <Wire.h>
#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);
}

AWOL

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.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

PaulS

Code: [Select]
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.
The art of getting good answers lies in asking good questions.

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:

Code: [Select]

 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.

brunopeniche

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

Code: [Select]

 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

brunopeniche

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?

Code: [Select]
#include <Wire.h>
#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 <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // 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);

 
  }

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.

PaulS

Code: [Select]
  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.
The art of getting good answers lies in asking good questions.

brunopeniche

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!!!

brunopeniche

Code: [Select]
  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

brunopeniche

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?

Code: [Select]
#include <Wire.h>
#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 <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // 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");

wildbill

This:
Code: [Select]
Serial.println(sensor2);
Needs to change now to:
Code: [Select]

 Serial.print(sensor2);

neiklot

Can you explain, please? ahah
Because then you'll have a division by zero:

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

brunopeniche

Because then you'll have a division by zero:


Yes that I understood gladly eheh :D. What I dont understood was why a could be equal to 10. I even dont know what a stands for ahah (I copied this equation for the temperature from another code on the internet)

AWOL

Code: [Select]
long a =1023 - analogRead(analogPin);If analogRead returns 1013.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up