I2C Temp Threshold

Hey Guys,

So i would like to say thank you all for all the feedback. I got another one for you. Please see code.

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

#define ONE_WIRE_BUS 3

OneWire oneWire(ONE_WIRE_BUS);
 
DallasTemperature sensors(&oneWire);

DeviceAddress Outside            =   { 0x28, 0xF0, 0xE4, 0x9F, 0x05, 0x00, 0x00, 0xE7 };
DeviceAddress UtilitiesEnclosure =   { 0x28, 0x92, 0xBE, 0x7B, 0x05, 0x00, 0x00, 0x25 };
DeviceAddress FreshWaterIntake   =   { 0x28, 0xD1, 0x91, 0xA0, 0x05, 0x00, 0x00, 0x0B };
DeviceAddress WasteWaterDrain    =   { 0x28, 0xE9, 0xF9, 0x9F, 0x05, 0x00, 0x00, 0xBA };
DeviceAddress FreshWaterPiping   =   { 0x28, 0x13, 0x8A, 0xA0, 0x05, 0x00, 0x00, 0xF6 };
DeviceAddress Basement           =   { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99 };
DeviceAddress MasterSuite        =   { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99 };
DeviceAddress LivingRoom         =   { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99 };

void setup(void)
{
  
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(Outside, 12);
  sensors.setResolution(UtilitiesEnclosure, 12);
  sensors.setResolution(FreshWaterIntake, 12);
  sensors.setResolution(WasteWaterDrain, 12);
  sensors.setResolution(FreshWaterPiping, 12);
  sensors.setResolution(Basement, 12);
  sensors.setResolution(MasterSuite, 12);
  sensors.setResolution(LivingRoom, 12);
  
}

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

void loop(void)
{ 
  delay(2000);
  Serial.print("Reading Temperatures...\n\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Outside Temp: ");
  printTemperature(Outside);
  Serial.print("\n\n\r");
  
  Serial.print("Utilities Enclosure Temp: ");
  printTemperature(UtilitiesEnclosure);
  Serial.print("\n\n\r");
  
  Serial.print("Fresh Water Intake Temp: ");
  printTemperature(FreshWaterIntake);
  Serial.print("\n\n\r");
  
  Serial.print("Waste Water Drain Temp: ");
  printTemperature(WasteWaterDrain);
  Serial.print("\n\n\r");
  
  Serial.print("Fresh Water Piping Temp: ");
  printTemperature(FreshWaterPiping);
  Serial.print("\n\n\r");
  
  Serial.print("Basement Temp: ");
  printTemperature(Basement);
  Serial.print("\n\n\r");
  
  Serial.print("Master Suite Temp: ");
  printTemperature(MasterSuite);
  Serial.print("\n\n\r");
  
  Serial.print("LivingRoom Temp: ");
  printTemperature(LivingRoom);
  Serial.print("\n\n\r");  
  
  delay(2000);  
}

Im trying to figure out how to read a temp sensor and control a relay based on the temperature. Im not sure how to approach it. Thanks again guys.

LC 8)

First off, get rid of that stupid printTemperature() function. The loop() function doesn't ever get a temperature, so you'll never be able to use the temperature value in loop().

Create a function, with a meaningful name, that gets the temperature from the specified sensor AND returns it. Then, call that function in loop(), and you will have a temperature that can be used to diddle a relay.

Awesome, can you walk me through how to accomplish this? I just got back into arduino programming and have not delt with these temp probes before. Thanks again.

LC

can you walk me through how to accomplish this?

For $100/hour, yes.

It isn't rocket science. Look at the stupid function that you have. See where it gets (and prints) the temperature? Instead of printing the temperature, return it. Of course, you need to change the function return type, too.

That condition, not knowing... is usually solved by reading... Not by asking for help that is obvious if you had taken the trouble to find some examples, read them and then do the rest of the work.. read some more until you do understand it...
Pain is the final teacher... If you wish to not experience the pain then you must do the work "Yourself"... once you have done all the research... and read a book or two... then when you get stuck at "See Bobby Run" you just might find that you've already found the answer... Ya Think?
BTW I bought my first Uno in 2012 and I've posted 3 questions here since then... I also managed to read 3 C/C++ books in the same time.
Look at my post count...

Doc

Here is the stupid function in question:

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

Getting rid of the stuff that actually prints the temperature, we are left with:

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

You can see that there is only ONE line that actually does something critical. Is there a reason to hide that in a function? I don't think so.

Move that line of code to the place in your sketch where you call the stupid function, and you then have something (tempC) that you can use in an if statement.