Very (VERY) simple if/else if loop doesn't seem to work like I want

I want the relay to turn on when the pool temperature is below the set point AND when the pool temps is lower than the solar collector temperature. Otherwise, I want to relay to be off (low). What am I doing wrong ? The relay stay on after one cycle…

Thanks ! :slight_smile:

Here’s the code in question:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int relay = A1;
int targetTemp = 26; // Define a target temp when Arduino resets

#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup(void)
pinMode(relay, OUTPUT);
void loop(void)
lcd.print("Piscine: "); lcd.print(sensors.getTempCByIndex(0));lcd.print((char)223);lcd.print(“C”);
lcd.print(“S:”); lcd.print(sensors.getTempCByIndex(1));lcd.print((char)223);lcd.print("C ");
lcd.print(“T:”); lcd.print(targetTemp);lcd.print((char)223);lcd.print(“C”);

if (sensors.getTempCByIndex(0)<targetTemp){
else if (sensors.getTempCByIndex(0)<sensors.getTempCByIndex(1)){
digitalWrite(relay, HIGH);}
else {
digitalWrite(relay, LOW);}

It’s impossible to help directly since there is no way to tell what is what without human readable variable names.

In psuedocode:

if (pooltemp < setpoint) && (pooltemp < solartemp)
  relay on;
  relay off;

Please read and follow the sticky post at the top of each forum area named “How to use this forum”. There are instructions there for how to properly post your codes it appears like the code block above.