My inability to use one temperature probe to derive two readings

I am writing a small sketch to control a solar heating system for a pool.

I am using one temperature probe.
A pump take water from the pool into the heating coil
We read the temperature of this water in the heating coil and call this poolTemp
This value is then stored and used for comparison as the water in the heating coil heats up
Then we read the temperature again and call this coilTemp
I have a variable called deltaTemp which can be set via a pot
These three variables are all displayed on a 4 x 20 lcd screen.
When the coiltTemp is greater than than the poolTemp + deltaTemp a relay is thrown to activate a pump which will replace the now warm water in the coil with cold from the pool and the process starts again.

My problem is that I can’t stop the system from constantly updating the poolTemp so poolTemp and coilTemp are permanently the same value.

I wonder if anyone can spot the error in my sketch.

/* This program is designed to contro;l a simple solar heating system using just one temperature probe.
The pump will only operate when the coil temperature exceed the pool temperature by more than a value set by the user.
 */



// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3f for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x3f, 20, 4);

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

// 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);
int poolTemp = 0; // use this variable  and newTemp if using just one temp probe
int deltaTemp = 0; //adjuated via pot
int coilTemp = 0; //temp of water in the coil
int potPin = A1; //wiper connection on pot
int relayPin = 7; //set this HIGH to operate the relay
int changeSource = 1; //use this to change between pool and coil temps
int pTemp = 0;

 
 void setup()

{
    pinMode (potPin, INPUT);
    pinMode(relayPin, OUTPUT);
 

  // Start up the library
  sensors.begin();
  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight 
  lcd.backlight();
  
  
}

/*
 * Main function, get and show the temperature
 */
void loop()
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
 
  sensors.requestTemperatures(); // Send the command to get temperatures
 
  deltaTemp = analogRead(potPin);
  deltaTemp = map(deltaTemp, 0, 1023, 0, 20); //this gives a range or 0 to 20 for the delta value
  if (changeSource = 1)
  {
poolTemp = (sensors.getTempCByIndex(0));
changeSource = 0;
  }

coilTemp = (sensors.getTempCByIndex(0));

       lcd.setCursor(0,0);
       lcd.print("Pool Temp  ");
       lcd.print(poolTemp);
       lcd.print ("  ");
       lcd.setCursor(0, 1);
       lcd.print("Coil Temp  ");
       lcd.print(coilTemp);
       lcd.print("  ");
       lcd.setCursor(0,2);
       lcd.print("Delta Temp  ");
       lcd.print(deltaTemp);
       lcd.print ("  ");

 if (coilTemp > poolTemp + deltaTemp) 
 {
  digitalWrite (relayPin, HIGH);
  // turn on the pump for 4 minutes to move warm water to pool
  changeSource = 1;
  
  }
  }
  if (changeSource = 1)

BONG !

Open your code in the IDE and press control-T.

Isn’t that better.

When you see real coders write with all the blocks lined up all nice and neat. Yeah, we don’t do that just to make it pretty. It really helps you see what parts go where and see the logic flow. The IDE will do this for you so there really is no excuse for code that wanders all over the page. If you’ll pay attention to that while you write it will help you keep things straight.

Thank you so much for that. Instant improvement in layout but still stymied with the sketch

still stymied with the sketch

Look carefully at the line of code in reply #1

What should it do ?
What does it actually do ?
What does

changeSource = 1

actually do ?

What should changeSource do
It should stop the variable poolTemp being constantly updated. The temperature sensor is only read when changeSource = 1 and then set to 0 where it remains until the temperature exceeds poolTemp + deltaTemp

I know this is where the problem is but I can't see what I'm doing wrong.

I have looked again and I realise the line in question should have read if (changeSource == 1)

Thats three weeks of my life I'll never get back again.

Thanks for taking the time to rspond and thanks for the ctrl T tip

I have looked again and I realise the line in question should have read if (changeSource == 1)

Whew !

You got there in the end, which is what matters

Its number 9 of the common pitfalls on this page: 11 Most Common Pitfalls in C Programming Language

Worth knowing what the common pitfalls are (don't worry if many of these don't make sense yet)

UKHeliBob:

  if (changeSource = 1)

BONG !

This is a very common mistake, especially with people who are new to programming in C++. (The Arduino programming language is an extension of C++, so, for the most part, the same rules apply.)

People new to C/C++/Arduino programming should learn:

  • x = y will make x be equal to y
  • x == y will check whether x is equal to y

This is important information, and "BONG!" is not much help in conveying this information. In fact, "BONG!" doesn't really convey much of any information, except perhaps that your clock is nearly an hour ahead of the correct time.

The OP didn’t seem to have any trouble. Sometimes yo just need the error spotted for you.

"BONG!" doesn't really convey much of any information,

The OP wrote

I wonder if anyone can spot the error in my sketch.

What I did was to point out where the error was. True, I could have explained the problem in detail but I chose not to as I felt it better to let him/her work it out for themselves, which they did with a further nudge. Hopefully having done so will prevent them making the error in future or, if they do, help them spot the problem