Pages: [1]   Go Down
Author Topic: need help averaging values from multiple one wire temperature sensors  (Read 565 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,
I'm trying to average the values from several Maxim DS18B20's. I want to measure the temperature of several pots of water (well, water, wort, and mash for beer making) from several different strata in each pot.

The problem is that I don't understand the code here (http://www.hacktronics.com/code/DallasTemperature.zip 'multiple' sketch) very well to understand how to write each sensor's output to a variable I can do some math on.

Here's how I want my code to work

pot1Temp = (topSensor1+bottomSensor1)/2
pot2Temp = (topSensor2+bottomSensor2)/2

any help on how I can average these sensor values?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3512
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This function call
Code:
  float tempC = sensors.getTempC(deviceAddress);
gets the temperature for a specific device.  Use it  to read the temp for a device and save it to a variable to do your maths on later.

Simplistically
Code:
read temp of device 1
save temp of device 1
read temp of device 2
save temp of device 2
do maths
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,
I'm now focusing on that section of code, but I'm still not having any luck.

I've changed the code to
Code:
void printTemperature(DeviceAddress deviceAddress)
{
  float foo = (float tempC = sensors.getTempC(insideThermometer)); // looks like deviceAddress  is just a vector of inside + outside so we can just read one here into a singe variable
  Serial.print("Temp C: ");
  Serial.print(1tempC);
  Serial.print(" Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(1tempC));
 
  bar = float tempC = sensors.getTempC(outsideThermometer); // looks like deviceAddress  is just a vector of inside + outside so we can just read one here into a singe variable
  Serial.print("Temp C: ");
  Serial.print(2tempC);
  Serial.print(" Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(2tempC));
 
 
}

but now I'm getting an error
"expected primary-expression before float"
So, i'm not doing something right.
I've tried to initialize "foo" and "bar"
Code:
DeviceAddress insideThermometer, outsideThermometer;
float foo;
float bar;

void setup(void)
{...

Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, got it figured out.
Changed the code to
Code:
{
  float temp1C = sensors.getTempC(insideThermometer);
  //Serial.print("Sensor 1 Temp C: ");
  //Serial.print(temp1C);
  Serial.print(" Sensor 1Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(temp1C));
  float foo = temp1C;
 
  float temp2C = sensors.getTempC(outsideThermometer);
  //Serial.print("Sensor 2 Temp C: ");
  //Serial.print(temp2C);
  Serial.print(" Sensor 2 Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(temp2C));
  float bar = temp2C;
 
  float avg = (temp1C + temp2C)/2;
  //Serial.print("Average Temp C: ");
  //Serial.print(avg);
  Serial.print(" Average Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(avg));
}

Now it functions as desired
my output now looks like
Code:
Requesting temperatures...DONE
 Sensor 1Temp F: 71.94 Sensor 2 Temp F: 71.15 Average Temp F: 71.54
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, what are the foo and bar variables for?

Have you ever considered arrays?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3512
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You seem to be getting the temperatures in Centigrade using getTempC and converting to Farhrenheit.  Is there not a getTempF method available ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, what are the foo and bar variables for?

Have you ever considered arrays?

To have global level variables, but not really necessary, strictly speaking. I don't really know how to use arrays in arduino smiley-sad

You seem to be getting the temperatures in Centigrade using getTempC and converting to Farhrenheit.  Is there not a getTempF method available ?

Yes, there is smiley

Here's my code now. I'd love any feedback on cleaning it up.

Code:
// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress insideThermometer = { 0x28, 0x4C, 0xB6, 0x9A, 0x04, 0x00, 0x00, 0xA1 };
DeviceAddress outsideThermometer = { 0x28, 0x3F, 0xF2, 0x9A, 0x04, 0x00, 0x00, 0x96 };

float tempIn;
float tempOut;
float avg;
void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(insideThermometer, 12);
  sensors.setResolution(outsideThermometer, 12);
}



void loop(void)
{
  delay(2000);
    Serial.print("Getting temperatures...\n\r");
 sensors.requestTemperatures();

     tempIn = sensors.getTempF(insideThermometer);
  if (tempIn == -127.00) {
    Serial.print("Error getting temperature");
  } else {
  }
 
    tempOut = sensors.getTempF(outsideThermometer);
  if (tempOut == -127.00) {
    Serial.print("Error getting temperature");
  } else {
   
  }

  avg = (tempIn+tempOut)/2;
  Serial.print("Inside temperature is: ");
  Serial.print(tempIn);
  Serial.print("\n\r");
  Serial.print("Outside temperature is: ");
  Serial.print(tempOut);
 Serial.print("\n\r");
  Serial.print("avg House temperature is: ");
 Serial.print(avg);
 Serial.print("\n\r"); 
  Serial.print("\n\r\n\r");
 
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (tempOut == -127.00) {
    Serial.print("Error getting temperature");
  } else {
   
  }
Why is there an empty else block? If there is nothing to do, leave it out.

On the other hand, if the temperature is invalid, why average and print it?

And really, is -126.99 and -127.01 OK? Exactly -127.00 degrees is bad, but anything else is OK?

Code:
Serial.print("\n\r"); 
  Serial.print("\n\r\n\r");
You have something against Serial.println()?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (tempOut == -127.00) {
    Serial.print("Error getting temperature");
  } else {
   
  }
Why is there an empty else block? If there is nothing to do, leave it out.

On the other hand, if the temperature is invalid, why average and print it?

And really, is -126.99 and -127.01 OK? Exactly -127.00 degrees is bad, but anything else is OK?

Code:
Serial.print("\n\r"); 
  Serial.print("\n\r\n\r");
You have something against Serial.println()?

Those came from the original dallastemperature library. I think the 127 value is just what's returned on error(?), but I'm not certain. println() would be a lot cleaner. Thanks.

Logged

Pages: [1]   Go Up
Jump to: