Temperature sensor relay control

This should be a walk in the park for the experienced Arduino programmer. I am still relatively new to the C++ language and I'm getting confused... Though I am probably still regarded as a "noob", I am experienced enough to know that I have all my libraries, sensors, and sensor addresses correctly applied and I will explain my situation further..

My project is simple enough. If the INSIDE temperature is less than the OUTSIDE temperature(or vice verses depending on the weather or your personal preference..) then flip a single relay, or multiple relays, either on or off, according to your preference. Easy right? My serial monitor works perfectly showing real time data from each sensor and I can even throw in some commands for the relays if I want.

The problem I am running into is this: My program looks straight forward enough, however, the "if" command portion at the end of the chain is completely ignored by my arduino.

This portion is my problem...

if (inside >= outside) //<-- The inside of the parentheses is my problem 
{
  digitalWrite(CHANNEL1, LOW); 
   delay(4000); 
  digitalWrite(CHANNEL1, HIGH);
}

I am assuming this has something to do with a lack of "int" or "float" line for each individual sensor in order for the controller to be able to function the way I want it too, but like I said, I'm new to this and I don't know the proper way to set up this kind of sensor as an int or float. Yes, I have tried replacing:
if (inside >= outside) with if (int(inside) >= int(outside))

yet the problem still persists.

If someone who has done this before could show me how to complete this code I would really appreciate it!!!
:slight_smile: XD

FULL CODE...

#define CHANNEL1 13//RelayIN1
#define CHANNEL2 12//RelayIN2

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

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// 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);
DeviceAddress inside = { 0x28, 0xB3, 0x58, 0x66, 0x04, 0x00, 0x00, 0xEC };
DeviceAddress outside = { 0x28, 0x12, 0x31, 0x66, 0x04, 0x00, 0x00, 0x65 };


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

 
  Serial.begin(9600);
  
  pinMode(CHANNEL1, OUTPUT);
  pinMode(CHANNEL2, OUTPUT);
  Serial.println("Ready. Type 0 to reset all relays, 1 - 4 to activate a relay.");
}

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

void loop() {
  

  delay(2000);
  //Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Inside: ");
  Serial.print(int(inside));
  printTemperature(inside);
  Serial.print("\n\r");
  
  Serial.print("Furnace: ");
  Serial.print(int(outside));
  printTemperature(outside);
  Serial.print("\n\r");
     
  
 if (inside >= outside) 
{
  digitalWrite(CHANNEL1, LOW); 
   delay(4000); 
  digitalWrite(CHANNEL1, HIGH);
}

 
}

You are trying to compare inside and outside which look like arrays of addresses.

Elsewhere in the program you seem to get the temperature by using:

float tempC = sensors.getTempC(deviceAddress);

so I don't know why you would stop doing that all of a sudden?
You need to get the temperature from one of your devices on the inside and one on the outside and then compare those two values.

I know what I need, I just don't know how to code it correctly. If someone could post example codes of using the Dallas Temperature sensors in multiples, that alone would help me greatly!

I am equally curious as to why my existing if command is not working, but my float integer is working perfectly for displaying the temps on the serial monitor... however I'm sure with a little help I can define the sensors differently and/or separately and get my if commands working correctly.

In loop, you need something like this:

float InsideTempC = sensors.getTempC(inside);
float OutsideTempC = sensors.getTempC(outside);

Then you can do your comparisons on the two floats.

Thanks for that suggestion! I tried it and then I tried it in multiple variations yet still no response.. Any other suggestions?

From the example here
http://milesburton.com/Dallas_Temperature_Control_Library
in line 33

Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print(sensors.getTempCByIndex(1)); // You have two sensors!

Gustermaximus:
Thanks for that suggestion! I tried it and then I tried it in multiple variations yet still no response.. Any other suggestions?

Post the latest version and describe what went wrong. You might add some serial.Print statements to the sketch to get a better idea of what it's doing too.

The original full code I added with my first post has not changed. I have tried all the suggested operations but I still have no RELAY CONTROL. I can view TEMPERATURE data on the serial monitor just fine. Everything seems perfectly normal, except for the fact that when one sensor is reading a higher(or lower.. depending on your preference) temperature than the other, my if command that should flip a relay on or off, is ignored.

From my understanding, it is not the serial monitor that has the problem. I believe I am needing some form of a #define command, an integer, or something like that to help correspond sensors with their individual commands. Does that make sense? I'm still fairly new to this so I just don't know what I don't know.

Thanks again for all the great comments!

Your "if" statement is comparing the addresses of the two arrays, not the temperatures reported by the corresponding sensors. The solution is as noted by earlier posters. For example, change

if (inside >= outside)

to

float insideTempC = sensors.getTempC(inside);
float outsideTempC = sensors.getTempC(outside);

if (insideTempC >= outsideTempC)

Yay! It works now!

 float insideTempC = sensors.getTempC(inside);
float outsideTempC = sensors.getTempC(outside);

if (insideTempC >= outsideTempC)

That was the key! I think I just missed having to make my integers inside (insideTempC >= outsideTempC) instead of (inside >= outside).
Here is the full completed working code:

#define CHANNEL1 13//RelayIN1
#define CHANNEL2 12//RelayIN2

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

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// 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);
DeviceAddress inside = { 0x28, 0xB3, 0x58, 0x66, 0x04, 0x00, 0x00, 0xEC };
DeviceAddress outside = { 0x28, 0x12, 0x31, 0x66, 0x04, 0x00, 0x00, 0x65 };
  


void setup()
{ 
  // start serial port
  Serial.begin(9600);
  // Start up the library

 
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
 // sensors.setResolution(inside, 10);
 // sensors.setResolution(outside, 10);
 
 
 
  Serial.begin(9600);
  float InsideTempC = sensors.getTempC(inside);
  float OutsideTempC = sensors.getTempC(outside);
  
  pinMode(CHANNEL1, OUTPUT);
  pinMode(CHANNEL2, OUTPUT);
  Serial.println("Ready. Type 0 to reset all relays, 1 - 4 to activate a relay.");
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);

 if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop() {
  
 float insideTempC = sensors.getTempC(inside);
float outsideTempC = sensors.getTempC(outside);
 
  delay(2000);
  //Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Inside: ");
  Serial.print(int(inside));
  printTemperature(inside);
  Serial.print("\n\r");
  
  Serial.print("Furnace: ");
  Serial.print(int(outside));
  printTemperature(outside);
  Serial.print("\n\r");
     
  
 if (insideTempC >= outsideTempC) //Trip relay if temp is exceeded
{
  digitalWrite(CHANNEL1, LOW); 
   delay(4000); 
  digitalWrite(CHANNEL1, HIGH);
}

 
 

} // LEAVE THIS ONE HERE FOR LOOP COMPLETION

Once again, thanks to all who commented and helped me figure this out!