Go Down

Topic: Help modifying DS18B20 sample code to activate a relay (Read 1 time) previous topic - next topic

scotty562

Sweet I got it working like I want. It's a rough draft, but this definitely got me past where I was stuck. Next is to figure out the relay. Thanks everyone.

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 3 on the Arduino
#define ONE_WIRE_BUS 10

// 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, 0xF0, 0x4A, 0x04, 0x00, 0x00, 0xD5 };
DeviceAddress outsideThermometer = { 0x28, 0x79, 0x35, 0x4A, 0x04, 0x00, 0x00, 0x23 };
DeviceAddress dogHouseThermometer = { 0x28, 0x59, 0xBE, 0xDF, 0x02, 0x00, 0x00, 0x9F };

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);
  sensors.setResolution(outsideThermometer, 10);
  sensors.setResolution(dogHouseThermometer, 10);
}

float printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {

  }
return tempC;
}

void loop(void)
{
 
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  float collector = printTemperature(insideThermometer);
  float tank = printTemperature(outsideThermometer);
 
  if (collector > 5)
  {
    Serial.print("Temp of collector is: ");
    Serial.print(DallasTemperature::toFahrenheit(collector));
    Serial.print("F ");
    Serial.print(collector);
    Serial.print("C");
    Serial.print("\n\r");
  }

  if (tank > 5)
  {
    Serial.print("Temp of collector is: ");
    Serial.print(DallasTemperature::toFahrenheit(tank));
    Serial.print("F ");
    Serial.print(tank);
    Serial.print("C");
    Serial.print("\n\r");
  delay(30000);
  }

}



PaulS

So. printTemperature() has a side effect of returning the temperature printed. Bad idea.

The function name should reflect what the function does. There is no real reason to have a separate function to print the temperature.

scotty562

I just renamed the function to getTemperature.

I would like to have float collector = printTemperature(insideThermometer); not need a separate function to work. I'd like to be able to say: float collector = value of sensor1, but my lack of programming knowledge inhibits this. For what I'm doing it doens't really bother me that much as it's functional. Although it would be nice to know how to do it.

For the relay control I got one of these: http://www.powerswitchtail.com/Pages/default.aspx should be good to go once that gets here.

PaulS

Quote
I would like to have float collector = printTemperature(insideThermometer); not need a separate function to work. I'd like to be able to say: float collector = value of sensor1, but my lack of programming knowledge inhibits this. For what I'm doing it doens't really bother me that much as it's functional. Although it would be nice to know how to do it.

You mean like this?
Code: [Select]
  float tempC = sensors.getTempC(deviceAddress);
That's the important line of code from the function.

Go Up