Comparing 2 values

I am looking to use Arduino to control a pump which will fill a tank with water.
This tank will have its temperature constantly monitored, I am attempting to set up a system which will allow the fill pump to be turned off if the temperature in the tank changes by too much over a certain period eg more than +/- 0.5 degrees over a 30 second period.

At the moment this is my code for the water change procedure

/********************************************************************/
// First we include the libraries
#include <OneWire.h> 
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 22 on the Arduino 
#define ONE_WIRE_BUS 22 
const int drain = 53;
const int button = 52;
const int pump = 50;
int buttonState = 0;         // variable for reading the pushbutton status
/********************************************************************/
// 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);

/////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600); 
  sensors.begin();
 // set pins as outputs/inputs
  pinMode (drain, OUTPUT);
  pinMode (pump, OUTPUT);
  pinMode (button, INPUT);
  digitalWrite (drain, LOW);
  digitalWrite (pump, LOW);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    digitalWrite (drain, HIGH); //open drain
    delay(120000); //2 minute delay
    digitalWrite (drain, LOW); //close drain
    digitalWrite (pump, HIGH);//on pump
    delay(120000); //2 minute delay
    digitalWrite (pump, LOW);//off pump   
  }
  else 
  {
    digitalWrite (drain, LOW);
    
  }

}

I have been using the onewire and dallas temperature libraries to read the temperature and write this to the serial monitor and would ideally like to incorporate this into this code to achieve the desired result.

/********************************************************************/
// First we include the libraries
#include <OneWire.h> 
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 22 on the Arduino 
#define ONE_WIRE_BUS 22 
/********************************************************************/
// 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);
/********************************************************************/ 
void setup(void) 
{ 
 // start serial port 
 Serial.begin(9600); 
 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 temperature readings 
 Serial.println("DONE"); 
/********************************************************************/
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 
   delay(1000); 
} 

I imagine I would need to set up some kind of loop on the fill stage with some kind of escape to halt the filling if the temperature varies too much but I am unsure how to do this.
I am aware this would mean assigning 2 variables to be the current temperature and the temperature 30 seconds ago but I am a little unsure how to write a reading from the temperature sensor to a variable and I have no idea how I would go about comparing those two values.

Needless to say I have hit the proverbial brick wall with this project and if anyone could point me in the right direction it would be greatly appreciated

Given that you are only filling for two minutes, would it make much difference if you just read the temperature at the beginning and stop the pump if the temperature changes too much during the fill?

In any event, you'll need to stop using delay and go over to millis based timing so you can read the temperature periodically.

Hi

see if your modified code does what you need.

/********************************************************************/
// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 22 on the Arduino
#define ONE_WIRE_BUS 22         // Mega
  const int drain = 53;
  const int button = 52;
  const int pump = 50;

/*#define ONE_WIRE_BUS 2         // my mini
const int drain = 3;
const int button = 4 ;
const int pump = 5;
*/
unsigned long controlT = 0;
unsigned long timeA = 12000;
byte flag = 0;
float actualTemp = 0;
float lastTemp = 0;
/********************************************************************/
// 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);
//----------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  sensors.begin();
  // set pins as outputs/inputs
  pinMode (drain, OUTPUT);
  pinMode (pump, OUTPUT);
  //pinMode (button, INPUT_PULLUP);
  pinMode (button, INPUT);
  digitalWrite (drain, LOW);
  digitalWrite (pump, LOW);
  readtemp();                           // Read temperature
  lastTemp = actualTemp;                // Save as lastTemp
}
//----------------------------------------------------------------------
void loop() {
  if (digitalRead(button) == HIGH)        // Read button state and if HIGH
  //if (digitalRead(button) == LOW)        // Read button state and if HIGH
  {
    if (flag == 0)                        // If is start activity
    {
      digitalWrite (drain, HIGH);         // open drain
      // Serial.println("flag 0");
      if (millis() - controlT > timeA)     // 2 minute delay
      {
        // Serial.print("1 "); Serial.println( millis() - controlT);
        flag = 1;                           // Next activity
      }
    }
    if (flag == 1)                        // If is 2 activity
    {
      // Serial.println("flag 1");
      digitalWrite (drain, LOW);          // close drain
      digitalWrite (pump, HIGH);          // on pump

      flag = 2;                           // Next activity
      controlT = millis();                // Refresh controlT again
    }
    if (flag == 2)                        // If is 2 activity
    {
      // Serial.println("flag 2");
      if (millis() - controlT > timeA)   // 2 minute delay
      {
        // Serial.print("2 "); Serial.println( millis() - controlT);
        digitalWrite (pump, LOW);         // off pump
        controlT = millis();              // Refresh controlT again
        flag = 0;                         // First activity
        // Serial.println("flag 0");
      }
    }
    if (actualTemp >= lastTemp  + 0.5)  // If it is greater than 0.5
    {
      //  Put here your instruction to turn off pump
      lastTemp = actualTemp;            // Save as lastTemp
      // Serial.println("Turn off pump");
    }
  }
  else                                    // If LOW
  {
    digitalWrite (drain, LOW);
    lastTemp = actualTemp;                // Save as lastTemp
    // Serial.println("No button");
    controlT = millis();                  // Refresh controlT again
  }
  readtemp();                             // Read temperature
  Serial.print("actualTemperature is: ");
  Serial.print(actualTemp);
  // Serial.print("  Last Temperature is:  ");
  // Serial.println(lastTemp);
}
//----------------------------------------------------------------------
void readtemp()
{
  sensors.requestTemperatures(); // Send the command to get temperature readings
  actualTemp = (sensors.getTempCByIndex(0));
}

RV mineirin

Thanks
the reason for the 2 minutes was to allow this to be tested without having to wait for long periods the idea is that this will perform 50% water changes on a system which I imagine will probably end up somewhere close to an hour
Thanks I will investigate this timing system

Wow thanks very much
I am currently at home and don't have all the parts with me but I will give this a go when I am back at work

Thanks again

You may want to think about what to do for restarting after a stop triggered by temperature. You will still 'owe' the tank some water, but you as you have it, you can't use the button because it'll drain even more water out.

What is the volume of water (size of tank)? What is the flow rate? 0.5° C or F? Does the water drain out at the exact same rate as it's pumped in? If not, how do you control level (underfill or overflow)? Can the DS18B20 respond quickly enough to temperature change? What is the temperature difference between water in and water out?

And don’t forget those waterproof ds18b20 are not water proof -the tips are but the cable entry mostly leaks

This is very much an early stage project and it will probably become more complex as the project develops - some of these are things that we have already thought of but for now I need a proof of concept type prototype that we can show to the higher ups to show that it is worth the time. as for some of the things mentioned with regards to tank size and flow rate my current plan is to spend some time with this draining and filling to set timings accordingly. I will likely include a water sensor that will read a maximum level and allow a top up and we already have a touch screen shield which I am looking to incorporate (but it is currently proving a major headache) that would allow different water changes to be triggered (10, 20, 50% etc and then hopefully an overriding top up to fill) as well as allowing temperatures to be monitored using the built in probe which is currently a daily occurrence.
The idea is that this will be used by several staff and the early discussions were that an LCD display would likely be more descriptive and make the system a little more user friendly.
But as I said this would all likely come further down the line

Hi,
Do you have a float switch to detect when the tank is full.
From what I see you have no way of knowing where the level is in the tank, so I could be 80% full and need 25% more water to cool it.

I'd be looking at possibly three float sensors;

  1. Tank Low
  2. Tank Half, or some intermediate level show if more than half full or half empty.
  3. Tank Full.

What is causing the tank to change temperature?
What is the temperature of the pumped in water?

Can you tell us the application of your project?
Do you need failsafe type system?

Can you post a basic block diagram of your project system?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

The idea is that this will be used to automate water changes in an aquarium quarantine system
top up water will be heated as well so the temperature sensing shut off should really just be a redundancy for if someone doesn't complete all their checks
I don't have a diagram at the moment but will throw something together at some point.
overflowing is not a major issue (although would prefer to avoid it) but maintaining temperature will be

Really appreciating all the feedback from everyone, apologies that I haven't responded to every reply but all the input has been great

Thanks

Hi,
Thanks for the info.
Instead of three float switches, two would do.

  1. Tank full.
  2. Tank level at minimum allowable level.

Tom... :smiley: :+1: :coffee: :australia: