3 DS18B20 to control 3 relays (relays on or of depending on temperature per D20.

Hi,

obviously a new guy here, new to arduino, new to programming, new to this forum.

I have set up a uno with 3 DS18B20’s (on 1 pin) and this works, I have created (read: borrow/stolen/copied/googled) some coding that should switch something between 0 and 3 relais depending on the temperatures these B20’s return.

I thought to do this with “if” and “If else” but I run into the problem that itseems to find 1 of the various situations and gets stuck on that situation, if I change the situation (I heat 1 D20 a little to push it over a limit set in the “ïf” it gets into this state and seems “trapped”. I think there must be a more elegant way to deal with this, but not sure where to start. (googling various terms like: “3 D20’s to control relays” or “multiple inputs to control relay assy” did not bring me mutch join.

The end goal is to have 3 rooms fitted with a B20 feeding information to the uno that can than decide what valve (hench the relays) should be opened or shut to redirect hot water to the right area.

There is no lay out of the connected situation (I can provide a stock picture of the situation) as for me the hardware side works fine. Using serial monitor I can see all 3 D20’s and see there temp)
the software on the other hand… Below my handywork… The comments are in dutch.

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

// Data wire is plugged into port 5 on the Arduino

#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
// Relay 1 = Arduino pin 4 Relay 2 = Arduino pin 7 Relay 3 = Arduino pin 8 Relay 4 = Arduino pin 12
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
#define relay1 4
#define relay2 7
#define relay3 8
#define relay4 12

void setup(void)
{
// start serial port
Serial.begin(9600);
pinMode(relay1, OUTPUT);// connected to Relay 1, relay 1 sluit vvw af
pinMode(relay2, OUTPUT);// connected to Relay 2 relay 2 sluit vvw en 1e verdieping af
pinMode(relay3, OUTPUT);// connected to Relay 3 schakelt ketel in door maak verbinding op 2e thermostaat.
pinMode(relay4, OUTPUT);// connected to Relay 4
Serial.println(“Dallas Temperature IC Control Library Demo”);

// Start up the library
sensors.begin();
}

void loop(void)
{
//call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println(“DONE”);

Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
// (0) is woonkamer
Serial.print("Temperature for the device 2 (index 1) is: ");
Serial.println(sensors.getTempCByIndex(1));
//(1) is kamer 1
Serial.print("Temperature for the device 3 (index 2) is: ");
Serial.println(sensors.getTempCByIndex(2));
// (2) is werkkamer

if(sensors.getTempCByIndex(0) < 20){
digitalWrite(relay1,HIGH);// turn relay 3 ON
Serial.println(" relay 1 OFF, klep open");
//delay(3000);// keep in relay 3 On for 3 seconds
digitalWrite(relay2, HIGH);// turn relay 3 OFF
Serial.println(" relay 2 OFF, klep open");
//delay(3000);// keep in relay 3 OFF for 3 seconds
digitalWrite(relay3, HIGH);// turn relay 3 OFF
Serial.println(" relay 3 OFF, klep open");
// delay(3000);// keep in relay 3 OFF for 3 seconds

(}else if(sensors.getTempCByIndex(0) < 20 && sensors.getTempCByIndex(1) < 19|| sensors.getTempCByIndex(2) < 19 ){
//woonkamer is warm, (door de zon?) dus warmte naar de bovenverdieping.
digitalWrite(relay1,LOW);// high = klep gesloten
Serial.println(" relay 1 ON, klep VVW dicht");
//delay(3000);// keep in relay 3 On for 3 seconds
digitalWrite(relay2, LOW);// high = klep gesloten
Serial.println(" relay 2 OFF, klep gesloten");
// delay(3000);// keep in relay 3 OFF for 3 seconds
digitalWrite(relay3, LOW);// turn relay 3 OFF
Serial.println(" relay 3 OFF, ketel aan");
//delay(3000);// keep in relay 3 OFF for 3 seconds

}else if(sensors.getTempCByIndex(2) <22 ){
// werkkamer vraagt warmte, dat krijgt voorrang op de vvw. hiervoor hoeft alleen de klep voor de 1e verdieping dicht, dus alles dicht is niet nodig, immers, de eerste klep in de serie dicht is voldoende.
// niet nodig:
//digitalWrite(relay1, HIGH);// turn relay 3 ON
Serial.println(" relay 1 OFF, klep open");
//delay(3000);// keep in relay 3 On for 3 seconds
digitalWrite(relay2, HIGH);// turn relay 2 ON
Serial.println(" relay 2 ON, klep dicht"); // klep naar de 1e verdieping ook dicht, hiermee gaat alle warmte naar de zolder en dus ook werkkamer.
//delay(3000);// keep in relay 3 OFF for 3 seconds
digitalWrite(relay3, HIGH);// turn relay 3 on
Serial.println(" relay 3 ON, Ketel gaat branden");
// delay(3000);// keep in relay 3 OFF for 3 seconds

}
}

Using if…else if the conditions will be evaluated in the order they are encountered. Therefore, if you look at the following code structure, you will never enter the second else if block because if (sensors.getTempCByIndex(0) < 20) is true then the first if block will ALWAYS be entered and the rest skipped.

  if (sensors.getTempCByIndex(0) < 20) {
   .
   .
   .
  } else if (sensors.getTempCByIndex(0) < 20 && sensors.getTempCByIndex(1) < 19 || sensors.getTempCByIndex(2) < 19 ) {
   .
   .
   .
  } else if (sensors.getTempCByIndex(2) < 22 ) {
   .
   .
   .
  }

The solution is simple, swap the order of evaluation:

  if (sensors.getTempCByIndex(0) < 20 && sensors.getTempCByIndex(1) < 19 || sensors.getTempCByIndex(2) < 19 ) {
   .
   .
   .
  } else  if (sensors.getTempCByIndex(0) < 20){
   .
   .
   .
  } else if (sensors.getTempCByIndex(2) < 22 ) {
   .
   .
   .
  }

thank you ToddL1962! i have got it working now,
The code around the if’s looks like inserted below, this is a crude version of what I am aiming for, but on the breadboard it worked. (the switch is included to use in one of the rooms, as this is only from time to time occupied). I will try and see if “do while” or “cases” might be a better solution, since this code now has rooms ranked in importance in a certain way, which might not be the most optimal for the heating system… maybe the addition of the switch to the first if as being low as a condition to be met would do this… (this last brain fart came to me while writing this post…) The temperatures are fictional and not based on real life peopl… i mean temperatures :grinning: .

if(sensors.getTempCByIndex(1) < 20  )
{do stuff...     }

else if(sensors.getTempCByIndex(1) > 22  )
{undo previous stuff... }

else if(switchState == HIGH && sensors.getTempCByIndex(2) <19)
{do stuff...}

else if(switchState == HIGH && sensors.getTempCByIndex(2) >21)
{ undo previous stuff...}

If you want to control each room independently, don’t use else if, just if.

if(sensors.getTempCByIndex(0) < 20  )
{
  heat1ON; 
}
if(sensors.getTempCByIndex(0) > 22  )
{
   heat1OFF;
}

if(sensors.getTempCByIndex(1) < 20  )
{
  heat2ON; 
}
if(sensors.getTempCByIndex(1) > 22  )
{
   heat2OFF;
}

if(sensors.getTempCByIndex(2) < 20  )
{
   heat3ON; 
}
if(sensors.getTempCByIndex(2) > 22  )
{
   heat3OFF;
}

Hi JCA34F ,
I will have to look Into that! I choose else if as it was used in the tutorial books I got with the Arduino starter kit.