Arduino Forum

Using Arduino => Programming Questions => Topic started by: brunopeniche on Mar 19, 2019, 01:28 pm

Title: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 01:28 pm
Hi!!

I am very new in arduino and in programming (I started 4 days ago :D ). And now I am trying to display the data from my thermistor (from sunfounder) and from my barometer (also from sunfounder BMP280) in the same way that a lot of people are doing:  https://www.youtube.com/watch?v=lFZ26gD7OIE . I manage to get the right codes for my thermistor and barometer separately but when I connect the two sensors in my arduino and try to do the same thing that is in the youtube video, It doesnt work entirely. I manege to get the temperature well, in celsius, but the pressure values are not shown in Pa. I cant understand why... Can someone help me with this, please?

Here are the individual codes for the thermistor and barometer:

Thermistor:

 #include <Wire.h>



#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);
}

void loop()
{
  long a =1023 - analogRead(analogPin); //read thermistor value
 
  //the calculating formula of temperature
 
 float tempC = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 
  Serial.println(tempC);
 

  delay(1000); //wait for 100 milliseconds
}

Barometer:

/*
 * bmp280_example.ino
 * Example sketch for BMP280
 *
 * Copyright (c) 2016 seeed technology inc.
 * Website    : www.seeedstudio.com
 * Author     : Lambor, CHN
 * Create Time:
 * Change Log :
 *
 * The MIT License (MIT)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "Seeed_BMP280.h"
#include <Wire.h>

BMP280 bmp280;

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

void loop()
{
  float pressure;
 
 
 
  //get and print atmospheric pressure data
 
  Serial.print(bmp280.getPressure());
 
 
 
 
  Serial.println("\n");//add a line between output of different times.

  delay(1000);
}

My code following the youtube template to show the two graphs in java:


#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);
 int sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c = (bmp280.getPressure());
 int sensor2 = c;
 
  char text [40];
  sprintf (text, "%d, %d\n", sensor1, sensor2);
  Serial.println (text);
 

  delay(1000); //wait for 100 milliseconds

}

With that coding I get this:

25, -28643  (as an example)

Again, the temperature is fine, but I cant understand why the pressure isnt in the correct units...

Can someone help me? :)

Cheers!! 


Title: Re: Displaying temperature and pressure at the same time using java
Post by: AWOL on Mar 19, 2019, 01:31 pm
Code: [Select]
float c = (bmp280.getPressure());What units does getPressure return?


Please remember to use code tags when posting code
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 01:36 pm
Sorry!! In pascals. It is really strange because, as soon as I put the Serial.print before the (bmp280.getPressure()) I got the right values: 10241224, 6. But of course in a completely wrong way of displaying the thing. The 102412 is the pressure in pascals the following two and four is the temperature and then I dont know what is the 6 after the comma ahah
Title: Re: Displaying temperature and pressure at the same time using java
Post by: AWOL on Mar 19, 2019, 01:40 pm
You can't fit the pressure in Pascals into a sixteen bit "int".

Use unsigned long
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 01:50 pm
how? Just change the int sensor2 = (...) to long sensor2 = (...)?

Thank you so much for your help!!!
Title: Re: Displaying temperature and pressure at the same time using java
Post by: AWOL on Mar 19, 2019, 01:53 pm
You can use long if you expect to measure negative pressure, but I suggested unsigned long.

Your choice.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 02:03 pm
I used the unsigned long (just replaced the int sensor2 = (...) to unsigned long sensor2 = (...)) and the values that I am getting are the same: - 28665 (as an example)
Title: Re: Displaying temperature and pressure at the same time using java
Post by: AWOL on Mar 19, 2019, 02:04 pm
Post the code.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 02:13 pm
#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);
 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

}
Title: Re: Displaying temperature and pressure at the same time using java
Post by: wildbill on Mar 19, 2019, 02:24 pm
You need to let sprintf know that it's being passed an unsigned long:
Code: [Select]

  sprintf (text, "%d, %lu\n", sensor1, sensor2);
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 02:30 pm
Yes!!!! Thank you so much to you too!!! Thanksss guys you really made my day happier!
Title: Re: Displaying temperature and pressure at the same time using java
Post by: AWOL on Mar 19, 2019, 02:47 pm
You wouldn't have had the problem if you'd just used Serial.print :)
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 03:09 pm
how could I do that? Can you exemplify, please? :)
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 19, 2019, 03:23 pm
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?
Title: Re: Displaying temperature and pressure at the same time using java
Post by: wildbill on Mar 19, 2019, 04:16 pm
You're storing the result of your temperature calc in an integer. By definition, integers don't have a fractional component. Use float instead.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 20, 2019, 01:48 pm
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);
}
Title: Re: Displaying temperature and pressure at the same time using java
Post by: AWOL on Mar 20, 2019, 01:54 pm
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.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: PaulS on Mar 20, 2019, 01:56 pm
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.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: wildbill on Mar 20, 2019, 02:01 pm
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.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 20, 2019, 02:27 pm
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
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 21, 2019, 04:29 pm
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);

 
  }
Title: Re: Displaying temperature and pressure at the same time using java
Post by: wildbill on Mar 21, 2019, 04:53 pm
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.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: PaulS on Mar 21, 2019, 06:47 pm
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.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 22, 2019, 12:32 pm
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!!!
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 22, 2019, 12:42 pm
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
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 22, 2019, 03:11 pm
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");
Title: Re: Displaying temperature and pressure at the same time using java
Post by: wildbill on Mar 22, 2019, 04:03 pm
This:
Code: [Select]
Serial.println(sensor2);
Needs to change now to:
Code: [Select]

 Serial.print(sensor2);
Title: Re: Displaying temperature and pressure at the same time using java
Post by: neiklot on Mar 22, 2019, 04:24 pm
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;
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 22, 2019, 04:34 pm
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)
Title: Re: Displaying temperature and pressure at the same time using java
Post by: AWOL on Mar 22, 2019, 04:41 pm
Code: [Select]
long a =1023 - analogRead(analogPin);If analogRead returns 1013.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: neiklot on Mar 22, 2019, 04:46 pm
note to self: must check what an Arduino actually does with a runtime error like that.

Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 22, 2019, 04:47 pm
This:
Code: [Select]
Serial.println(sensor2);
Needs to change now to:
Code: [Select]

 Serial.print(sensor2);

wow thanks!!! It worked!! So what is the difference of putting Serial.println instead of Serial.print? the "ln" kind of finishes the line where the data is displayed, with the next values being displayed in the next paragraph?
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 22, 2019, 04:53 pm
Code: [Select]
long a =1023 - analogRead(analogPin);
If analogRead returns 1013.
So if the analog read of the thermistor gives 1013 the code will have some kind of error, right? Do you guys then think that I should change the code? Because that equation was really the best that I found to display the temperature in degrees celsius
Title: Re: Displaying temperature and pressure at the same time using java
Post by: neiklot on Mar 22, 2019, 05:00 pm
the "ln" kind of finishes the line where the data is displayed, with the next values being displayed in the next paragraph?
Yes, and more "technically" it inserts a CR carriage return (to go back to the left) and a LF line feed (to click one line ahead) into the stream sent to the monitor.

Have a Google for the ascii table, and you will see character hex 0D, or decimal 13 is the CR and 0A or 10 is the LF.

Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 22, 2019, 05:18 pm
Yes, and more "technically" it inserts a CR carriage return (to go back to the left) and a LF line feed (to click one line ahead) into the stream sent to the monitor.

Have a Google for the ascii table, and you will see character hex 0D, or decimal 13 is the CR and 0A or 10 is the LF.


Thanks!!
Title: Re: Displaying temperature and pressure at the same time using java
Post by: PaulS on Mar 25, 2019, 10:53 am
Can you explain, please? ahah
I was seeing parentheses where I would have put them, not where you didn't put them.

You are dividing by a. If a is ever 0, you are doing A BAD THING.

You SHOULD be using parentheses to make it clear how that equation is to be evaluated.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 25, 2019, 12:34 pm
I was seeing parentheses where I would have put them, not where you didn't put them.

You are dividing by a. If a is ever 0, you are doing A BAD THING.

You SHOULD be using parentheses to make it clear how that equation is to be evaluated.
Ok, I have put the equation like this now:

Code: [Select]
float sensor1 = beta /(log(((1025.0 * 10 / a) - 10) / 10) + beta / 298.0) - 273.0;

instead of:

Code: [Select]
float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;

And it seems fine. Do you agree with that?

Title: Re: Displaying temperature and pressure at the same time using java
Post by: PaulS on Mar 25, 2019, 01:03 pm
Quote
Do you agree with that?
No. You are still dividing by a, even if a is 0.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 25, 2019, 02:33 pm
No. You are still dividing by a, even if a is 0.
Oh yeah. So how you suggest me to change it? I would have to change to another equation?
Title: Re: Displaying temperature and pressure at the same time using java
Post by: PaulS on Mar 25, 2019, 02:38 pm
Quote
So how you suggest me to change it?
Code: [Select]
if(a != 0)
{
   sensor1 = beta /(log(((1025.0 * 10 / a) - 10) / 10) + beta / 298.0) - 273.0;
}
else
{
   sensor1 = someOtherValue;
}

I have no idea what you want to use as someOtherValue, when a is 0.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 25, 2019, 03:27 pm
Code: [Select]
if(a != 0)
{
   sensor1 = beta /(log(((1025.0 * 10 / a) - 10) / 10) + beta / 298.0) - 273.0;
}
else
{
   sensor1 = someOtherValue;
}

I have no idea what you want to use as someOtherValue, when a is 0.
I dont know, maybe the result of the equation when a=0.00001? Is it correct?
Title: Re: Displaying temperature and pressure at the same time using java
Post by: PaulS on Mar 25, 2019, 03:53 pm
I dont know, maybe the result of the equation when a=0.00001? Is it correct?
Since you have declared a to be an integral type, setting it to 0.000001 seems unlikely.
Title: Re: Displaying temperature and pressure at the same time using java
Post by: brunopeniche on Mar 27, 2019, 01:01 pm
Since you have declared a to be an integral type, setting it to 0.000001 seems unlikely.
sorry for not answering sooner but I was out of this project for 2 days. So what value do you recommend? I sincerely dont know ahah