Problem with controller

I have build a controller to open and close a valve when there is a 10 degree(celsius) colder in the tank from the thermometer placed near the heat pump, then if this condition is true it need to turn on a relay that controls a valve.

For reading the temperature i have 2 DS18B20's

Hope someone out there can help me because this has troubled me the last few days.

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

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 10

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress tankThermometer, pumpThermometer;

int RelayPin = 4;
int failpin = 13;
boolean Running = false;
boolean fail = false;
float TempOpen;
float TempEt;
float TempTo;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  //Setting outputs to led and relay
  pinMode(RelayPin, OUTPUT);
  pinMode(failpin, OUTPUT);
  
  // Start up the library
  sensors.begin();

  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");

  // assigns the first address found to pumpThermometer
  if (!oneWire.search(pumpThermometer)) Serial.println("Unable to find address for outsideThermometer");
  // assigns the seconds address found to tankThermometer
  if (!oneWire.search(tankThermometer)) Serial.println("Unable to find address for insideThermometer");

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(pumpThermometer);
  Serial.println();

  Serial.print("Device 1 Address: ");
  printAddress(tankThermometer);
  Serial.println();

  // set the resolution to 12 bit
  sensors.setResolution(pumpThermometer, TEMPERATURE_PRECISION);
  sensors.setResolution(tankThermometer, TEMPERATURE_PRECISION);

  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(pumpThermometer), DEC); 
  Serial.println();

  Serial.print("Device 1 Resolution: ");
  Serial.print(sensors.getResolution(tankThermometer), DEC); 
  Serial.println();
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
}

// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
  Serial.print("Resolution: ");
  Serial.print(sensors.getResolution(deviceAddress));
  Serial.println();    
}

// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
  Serial.print("Device Address: ");
  printAddress(deviceAddress);
  Serial.print(" ");
  printTemperature(deviceAddress);
  Serial.println();
}

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

  // print the device information
  printData(tankThermometer);
  printData(pumpThermometer);

  //Getting temperatures
  TempEt = sensors.getTempC(tankThermometer);
  TempTo = sensors.getTempC(pumpThermometer);

  //Doing some math
  TempOpen = TempEt + 10;

  //Measuring for fails 
  if(TempEt == -127.00 || TempTo == -127.00){
    fail = true;
    digitalWrite(failpin, HIGH);
  }
  else{
    fail = false;
    digitalWrite(failpin, LOW);
  }

  //Checking when to open
  if(TempOpen <= TempTo && Running == false && fail == false){
    Running = true;
    digitalWrite(RelayPin, LOW);
    Serial.println("Relay On");
  }

  //Checking when to close
  if(TempEt >= TempTo && Running == true && fail == false){
    Running = false;
    digitalWrite(RelayPin, HIGH);
    Serial.println("Relay Off");
  }
}

StyringOS.ino (4.06 KB)

Hope someone out there can help me because this has troubled me the last few days.

What is it that is troubling you ?

I'm having trouble with it turning the relay randomly on or off

By the way it mostly happens when the room temperature passes 25-26 degrees celsius.

I don't think this is helpful

  printData(tankThermometer);
  printData(pumpThermometer);

As far as I can see it reads a new temperature measurement rather than printing the value that the decision making part of the code is using.

Just read the temperatures once per iteration and use the saved values for all purposes until the next iteration.

...R