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.
Thanks,
I'm now focusing on that section of code, but I'm still not having any luck.
I've changed the code to
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"
PaulS:
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
UKHeliBob:
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.
// 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");
}
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?
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.