If else not right

I have a basic program I’m testing to make it part of a larger project later. Everything works as it should. It measures temperature and turns a relay on/off at different temperatures.
I’m also trying to have an indication of when the fan is on or off.

The code at lines 113 to 119 always returns OFF. I’m pretty sure the problem is in line 113, I haven’t figured out how to say it.

#include <OneWire.h>

#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2




OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);




// Addresses of 9 DS18B20s  Change these to match temp sensors used




uint8_t sensor1[8] = { 0x28, 0x9F, 0x9D, 0x7A, 0x00, 0x00, 0x00, 0x63 };  //battery 1--#16

uint8_t sensor2[8] = { 0x28, 0xC9, 0xEF, 0x7C, 0x00, 0x00, 0x00, 0x18 };  //battery 2--#14

//uint8_t sensor3[8] = {  0x28, 0xFF, 0x5F, 0x98, 0xA1, 0x15, 0x04, 0x60 };//battery 3--#1

//uint8_t sensor4[8] = {  0x28, 0xFF, 0x11, 0x57, 0x73, 0x15, 0x01, 0xE1 };//battery 4--#12

//uint8_t sensor5[8] = {  0x28, 0xFF, 0x2E, 0x9F, 0x81, 0x15, 0x01, 0xED };//battery 5--#2

//uint8_t sensor6[8] = {  0x28, 0xFF, 0x1C, 0xB3, 0x61, 0x15, 0x01, 0x4F };//battery6--#5

//uint8_t sensor7[8] = {  0x28, 0xFF, 0x21, 0x43, 0x74, 0x15, 0x03, 0x95 };//cabin--#15

//uint8_t sensor8[8] = {  0x00, 0xC9, 0xEF, 0x7C, 0x00, 0x00, 0x00, 0x18 };//heater--#17

//uint8_t sensor9[8] = {  0x00, 0x9F, 0x9D, 0x7A, 0x00, 0x00, 0x00, 0x63  };//outside--#13




const int relay_1 = 7;

const int relay_2 = 3;

//const int relay_3 = 4;

//const int relay_4 = 5;





const int relay1_temp_below = 69;

const int relay1_temp_above = 70;




const int relay2_temp_below = 69;

const int relay2_temp_above = 70;




//const int relay3_temp_below = 45;

//const int relay3_temp_above = 50;




//const int relay4_temp_below = 45;

//const int relay4_temp_above = 50;






void setup(void) {

  Serial.begin(9600);

  sensors.begin();

  // Serial.println();




  pinMode(relay_1, OUTPUT);

  pinMode(relay_2, OUTPUT);

  //pinMode(relay_3, OUTPUT);

  //pinMode(relay_4, OUTPUT);




  //all relays are set to off

  digitalWrite(relay_1, LOW);

  digitalWrite(relay_2, LOW);

  //digitalWrite(relay_3, LOW);

  //digitalWrite(relay_4, LOW);





}





void loop(void) {




  sensors.requestTemperatures();




  //++++++++++++++++++++++++++++Temperature




  float temperature1 = sensors.getTempF(sensor1);

  float temperature2 = sensors.getTempF(sensor2);

  // float temperature3 = sensors.getTempF(sensor3);

  //float temperature4 = sensors.getTempF(sensor4);

  //float temperature5 = sensors.getTempF(sensor5);

  //float temperature6 = sensors.getTempF(sensor6);

  //float temperature7 = sensors.getTempF(sensor7);

  //float temperature8 = sensors.getTempF(sensor8);

  //float temperature9 = sensors.getTempF(sensor9);





  Serial.print("Temp 1    ");

  Serial.println(temperature1);

  Serial.print("Temp 2    ");

  Serial.println(temperature2);

  //Serial.print ("Batt 3 temp    ");

  //Serial.println(temperature3);

  //Serial.print ("Batt 4 temp    ");

  //Serial.println(temperature4);

  //Serial.print ("Batt 5 temp    ");

  //Serial.println(temperature5);

  //Serial.print ("Batt 6 temp    ");

  //Serial.println(temperature6);

  //Serial.print ("Cabin temp     ");

  //Serial.println(temperature7);

  //Serial.print ("Heat temp      ");

  //Serial.println(temperature8);

  //Serial.print ("Outside temp   ");

  //Serial.println(temperature9);

  Serial.println(" ");





  //-------------------------------------------------

  // Cooling fan relay 1

  if (temperature1 > relay1_temp_above) {

    //Serial.println("Fan :     ON");

    digitalWrite(relay_1, HIGH);

   

  }

  if (temperature1 < relay1_temp_below) {

    //Serial.println("Fan :     OFF");

    digitalWrite(relay_1, LOW);

    

  }

  //---------------------------------------------

  if (relay_1 == HIGH) {

     Serial.println("Fan :     ON");

      

    }

   

    else {Serial.println("Fan :   OFF");

    }

//----------------------------------------------

  /*

  //relay 2




  if (temperature2 > relay2_temp_above) {

    Serial.println("Heat Relay:    ON");





    digitalWrite(relay_4, HIGH);

    //delay(1000);

  }

  if (temperature2 < relay2_temp_below) {

    Serial.println("Heat Relay:    OFF");





    digitalWrite(relay_2, LOW);

    //delay(1000);

  }

*/

  Serial.println();

  Serial.println("*********************");

  Serial.println();





  //refresh interval

  delay(2500);

}

What am I doing wrong?

We cannot tell what lines you are referring to. No line numbers here. BUT you can always write the “else “ code as a separate “if”.

relay_1 is a pin. If you want to see whether it’s HIGH or LOW, you need to digitalRead() it…

translates to

if (7 == 1) {

Which will always be false.

If you want to test pin 7's (relay_1) state, you need to read it and test against that value, as @Brazilino said.

if (digitalRead(relay_1) == HIGH )
1 Like

Your code needs better formatting, all those extra blank lines are not helping. Since there are no line numbers, you need to //comment the affected lines.
Try a CMD-T AutoFormat as well.

IDE on Windows also uses ALT-SHIFT-F for formatting (I think CTRL-T was or will be New Tab)

My bad, I drive a Mac and I forgot to add the Win keystrokes, I think it might be Ctl-T but the direct route is IDE/Tools/Auto Format

Both work in the IDE on Windows.

Thank you for your explanation and help