Go Down

Topic: IF, OR Statements (Read 283 times) previous topic - next topic

ibokhari

Hi All

I have done a little searching on this and haven't found exactly what I'm looking for, so please excuse me if this has already been discussed.

I am working on a controlled environment for reptiles and want to have a function that will switch off the heat lamps if one of two sensors is above a set temperature (placed in different locations). This is my first project using micro controllers and coding so it's been a learning curve the past few months.

The issue is the lamps do not turn off if one of the sensors is greater than or equal to the upper limit. Is what I have the best way to do this, and if not what is the correct/more reliable way? If there is any information missing or you want more detail please let me know.

Desired Response: IF Sensor1 AND Sensor2 is below 89 and 90 respectively, while both relays are off (HIGH) turn on (LOW). IF Sensor1 OR Sensor2 is above 94 and 96 respectively, while both are turned on (LOW) than turn off (HIGH).

Actual Response: The heat lamps turn on when below the lower limit, although do not turn off when Sensor1 OR Sensor2 reach the upper limit. I do not think the OR statement is written right.

Code: [Select]

if ((Sensor1 <= 89) && (Sensor2 <= 90) && (RELAY2STATUS == RELAY3STATUS == HIGH)) {
  RELAY2STATUS = LOW;
  RELAY3STATUS = LOW;
  }
if ((Sensor1>= 94) || (Sensor2 >= 96) && (RELAY2STATUS == RELAY3STATUS == LOW))  {
  RELAY2STATUS = HIGH;
  RELAY3STATUS = HIGH;
  }

JCA34F

#1
Aug 06, 2020, 05:54 am Last Edit: Aug 06, 2020, 06:13 am by JCA34F
Code: [Select]

if ((Sensor1 <= 89 && Sensor2 <= 90) && (RELAY2STATUS == HIGH && RELAY3STATUS == HIGH)) {
  RELAY2STATUS = LOW;
  RELAY3STATUS = LOW;
  }
if (Sensor1 >= 94) || Sensor2 >= 96) {
  RELAY2STATUS = HIGH;
  RELAY3STATUS = HIGH;
  }

Simpler:
Code: [Select]

if ((Sensor1 <= 89 && Sensor2 <= 90) && (RELAY2STATUS && RELAY3STATUS)) {
  RELAY2STATUS = LOW;
  RELAY3STATUS = LOW;
  }
if (Sensor1 >= 94 || Sensor2 >= 96)  {
  RELAY2STATUS = HIGH;
  RELAY3STATUS = HIGH;
  }



MarkT

#2
Aug 06, 2020, 09:00 pm Last Edit: Aug 06, 2020, 09:01 pm by MarkT
(RELAY2STATUS == RELAY3STATUS == HIGH)
Is incorrect, that's valid Python but is incorrect C/C++.

Use
(RELAY2STATUS && RELAY3STATUS)
(HIGH evaluates as true, as its non-zero).

(RELAY2STATUS == RELAY3STATUS == LOW)
Is similarly broken, you must write:
(!RELAY2STATUS && !RELAY3STATUS)
or
(RELAY2STATUS == LOW && RELAY3STATUS == LOW)
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up