Go Down

### Topic: need help averaging values from multiple one wire temperature sensors (Read 1 time)previous topic - next topic

#### jrubins

##### Feb 03, 2013, 06:03 am
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?

#### UKHeliBob

#1
##### Feb 03, 2013, 10:06 am
This function call
Code: [Select]
`  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: [Select]
`read temp of device 1save temp of device 1read temp of device 2save temp of device 2do maths`
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### jrubins

#2
##### Feb 03, 2013, 09:04 pm
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: [Select]
`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: [Select]
`DeviceAddress insideThermometer, outsideThermometer;float foo;float bar;void setup(void){...`

Thanks

#### jrubins

#3
##### Feb 03, 2013, 09:40 pm
Ok, got it figured out.
Changed the code to
Code: [Select]
`{  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: [Select]
`Requesting temperatures...DONE Sensor 1Temp F: 71.94 Sensor 2 Temp F: 71.15 Average Temp F: 71.54`

#### PaulS

#4
##### Feb 04, 2013, 11:48 am
So, what are the foo and bar variables for?

Have you ever considered arrays?

#### UKHeliBob

#5
##### Feb 04, 2013, 02:48 pm
You seem to be getting the temperatures in Centigrade using getTempC and converting to Farhrenheit.  Is there not a getTempF method available ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### jrubins

#6
##### Feb 08, 2013, 07:11 am

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

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

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

Code: [Select]
`// 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 devicesOneWire 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.htmlDeviceAddress 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");  }`

#### PaulS

#7
##### Feb 08, 2013, 07:22 am
Code: [Select]
`  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: [Select]
` Serial.print("\n\r");    Serial.print("\n\r\n\r");`
You have something against Serial.println()?

#### jrubins

#8
##### Feb 08, 2013, 08:59 pm

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

Go Up

Please enter a valid email to subscribe