if &&

Good day everyone

I hope i am doing this right and not posting something stupid in the wrong place.

I am new to arduino and have been messing around with the basics i mostly copy examples and then add and modify to suit what i want i have got 3 ds18b20 temp probes that i want to check to keep my fish tank warm in the winter with a solar heater. i got all the temps to read correctly and i have a basic circuit to test it but I’m really struggling with the If && i want the pump to only come on if the solar is 5 deg hotter than the tank and the tank is cooler than the tank max temp i when i test it the pump comes on when the solar is 5 deg warmer but it does not stop once the max temp is reached.

Could someone please help me
Thanks

colin_2_tempswith_3led.ino (4.45 KB)

Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]

Sorry i was not sure how it works

/* This Arduino sketch reads 2 DS18B20 "1-Wire" digital
temperature sensors. Marcs testing.
*/

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

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

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

//Adjustments

//Set max pool temp
float  tankMax = 28.0;

//Set solpanel diff to start
float setDiff = 5.0;


//set state
int state1 = 0;
int state2 = 0;


// Assign the addresses of your 1-Wire temp sensors.
// Solpanel S3, tank S1
DeviceAddress solpanel = { 0x28, 0xFF, 0x55, 0x02, 0x2E, 0x04, 0x00, 0x53 };
DeviceAddress tank = { 0x28, 0x56, 0x22, 0x46, 0x06, 0x00, 0x00, 0xC1 };
DeviceAddress room = { 0x28, 0xAF, 0xBC, 0x99, 0x05, 0x00, 0x00, 0x4F };


// Number of temperature devices found
int numberOfDevices;

//variables used for control logic

float   Temp1, Temp2, Temp3;            //  storage for comparing the tank and solarpanel temps
//const boolean ON = HIGH;        //  makes the code easier to read
//const boolean OFF = LOW;        //  makes the code easier to read


//variables used for control logic
float differential1 = 0.0 ;

//define pin for pump relay
const int pump = 4;
const int redled = 10;
const int yellowled = 11;
const int greenled = 12;


//this part of the code runs once to get everything setup and started.

void setup(void)
{


  // start serial port
  Serial.begin(9600);
  // Start up the library
  Serial.println ("starting one wire sensors");
  sensors.begin();
 
  // locate devices on the one wire bus
  Serial.println("");
  Serial.print("Locating 1-wire devices...");
  Serial.println ("");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
 
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(solpanel, 10);
  sensors.setResolution(tank, 10);
  sensors.setResolution(room, 10);


    //set pins to output
   {
  pinMode (pump, OUTPUT);
  pinMode (redled, OUTPUT);
  pinMode (yellowled, OUTPUT);
  pinMode (greenled, OUTPUT);
 }

   //give time for everything to start
  delay (2000);

}
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    }
}


void loop(void)
{
  Serial.print("Temps are...\n\r");
  sensors.requestTemperatures();
 
  Serial.print("Temp solpanel is: ");
  printTemperature(solpanel);
  Serial.print("\n\r");
  Serial.print("Temp tank is: ");
  printTemperature(tank);
  Serial.print("\n\r");
  Serial.print("Temp room is: ");
  printTemperature(room);
  Serial.print("\n\r");
  Serial.print ("Status State1: ");
  Serial.println(state1);
  Serial.println("");
  Serial.print ("Status State2: ");
  Serial.println(state2);
  Serial.println("");
  delay(1500);


//Sensors for logic

Temp1 = sensors.getTempC(tank);
Temp2 = sensors.getTempC(solpanel);
Temp3 = sensors.getTempC(room);


//difference between solpanel and tank gives us our differential
      differential1 = Temp2-Temp1;
      Serial.print("Diff 1 is:");
      Serial.println(differential1);
      Serial.println("");


       //logic1
     {
       if (differential1 > setDiff); { // check if solarpanel is hotter than tank,
      (state1 = 1);      // return 1 if heat is on
           
     if (differential1 < 5.0 )
    (state1 = 0);
      }

if (Temp1 > tankMax); { // compare the temps,
      (state2 = 0);      // return 0 if tank is hotter tham tankmax temp
           
     if (Temp1 < tankMax )
    (state2 = 1);
      }

    {
       if ((state1 == 1) && (state2 ==1)); { // compare the temps,
      digitalWrite(pump, HIGH);      // put pump on
           
     if ((state1 == 0) && (state2 == 0))
    digitalWrite(pump, LOW);  // put pump off
      }


{
       if (Temp1 > 20); { // check temp of tank,
      digitalWrite(greenled, HIGH);      // turn green led on

     if  (Temp1< 20)
    digitalWrite(greenled,LOW);     // turn green led off
    
     if (Temp1> 25)
    digitalWrite(yellowled, HIGH);      // turn yellow led on

    if  (Temp1< 25)
    digitalWrite(yellowled,LOW);     // turn yellow led off

    if  (Temp1> 30)
    digitalWrite(redled,HIGH);      // turn red led on

     if  (Temp1< 30)
    digitalWrite(redled,LOW);       // turn red led off

     

     
      }
      delay (1500);


}

 
     }
}
}

OOPS

if ((state1 == 1) && (state2 ==1));

;
We are sure you know what these do :wink:

.

float   Temp1, Temp2, Temp3;            //  storage for comparing the tank and solarpanel temps

So, why not name them solarTemp, tankTemp, and roomTemp?

Ditch that stupid printTemperature() function. You want to do far more than PRINT the temperatures.

      if (differential1 > setDiff); { // check if solarpanel is hotter than tank,

If statements rarely have semicolons.

Especially when followed by a {.

PaulS:

float   Temp1, Temp2, Temp3;            //  storage for comparing the tank and solarpanel temps

So, why not name them solarTemp, tankTemp, and roomTemp?

Ditch that stupid printTemperature() function. You want to do far more than PRINT the temperatures.

      if (differential1 > setDiff); { // check if solarpanel is hotter than tank,

If statements rarely have semicolons.

the Temp1, Temp2, Temp3 was from the original program fixed that now.

so should i remove the printTemperature()? I was just using it to check the temps on the serial monitor.

should there not be any semicolons in the if statement?

should there not be any semicolons in the if statement?

Have you ever seen any in any of the tutorials and examples?

see: C - Control Statements, if, elseif, while, do, for loop (modified):

if (expression)   statement;

or

if (expression)
 {
   statement1;
   statement2;
   statement3;
   statement4;
 }