If statement problem does not go to else

Hi I am looking for help with an If statement.

The problem seems to be that when executing the program the else part writing 22 low does not work unless timed Heating is true.
I have searched for a while and tried many variations however the code does not seem to work correctly when timed heating (Byte) is 1 or anything but 0. I would expect a low on pin 22? what am I doing wrong?

if((Manual == true)or

((DHT.temperature>= TempSP) && 

(TimedHeating == 0))) //conditions for heating to come on time heating problem as if not true does not do else
  {
    digitalWrite(22, HIGH); // relay A0 on digital pin 22   
  }
  else
  {
    digitalWrite(22, LOW); // relay A0 on digital pin 22
  }
}

what am I doing wrong?

Check out this link:

What is this 'or'? Do you mean '||' ?

I'd start by simplifying the statement losing the clutter of unneeded paraentheses and
redundant '== true's

#define relay_A0 22
...

  if (Manual || (DHT.temperature >= TempSP && TimedHeating == 0))
    digitalWrite(relay_A0, HIGH);
  else
    digitalWrite(relay_A0, LOW);

If TimedHeating is non-zero, then the && clause is false, so Manual must be true to yield true overall.

Serial.println() is really useful for debugging complex conditions like this, print out all the
relevant variables before the if, and print in the then and else parts to see what happens.

Watch out for = and == confusion, | v. ||, etc etc - one character wrong can scupper everything.

You have set pin 22 as an output?

You have 3 conditions associated in à boolean/truth statement this way (A OR (B AND C))

You say you guarantee C is false... but Why would C being false means this expression should be false... if A is true you are never going in your else...

so my bet is Manual is true

As we can’t see your code...

MarkT:
What is this 'or'? Do you mean '||' ?

This is legit keyword in most C and C++ (look for “Alternative operator representations”)

(I disagree with the ‘un-needed’ parenthesis comment, they help with reading without asking oneself about prioritization and cause no side penalty to the code)

J-M-L:
(I disagree with the ‘un-needed’ parenthesis comment, they help with reading without asking oneself about prioritization and cause no side penalty to the code)

+1

Here I am confused why you would turn the heat on if your “above” the set point..

((DHT.temperature>= TempSP) &&

J-M-L:
This is legit keyword in most C and C++ (look for “Alternative operator representations”)

While this may be true, and there are certainly plenty of macro headers out there that define it, GCC is not one.

DKWatson:
While this may be true, and there are certainly plenty of macro headers out there that define it, GCC is not one.

Sure it does. It has and, or, xor, not. Did you try it?

I feel like the use of these keywords would make code more beginner friendly but I didn't know about them when I learned C++ and I have not yet been able to convince myself to start using them in the code I publish.

DKWatson:
While this may be true, and there are certainly plenty of macro headers out there that define it, GCC is not one.

I’m sorry to disagree.. proper scientific practice would be to doubt my saying indeed, but research reference documentation (as I gave you what to look for...) instead of hinting “it may be wrong”... it IS true, period. There is no “may be” and it’s not random “macros headers out there”, it’s part of the definition of the language.

Here - I did the look up for you:

Alternative operator representations
C++ (and C) source code may be written in any non-ASCII 7-bit character set that includes the ISO 646:1983 invariant character set. However, several C++ operators and punctuators require characters that are outside of the ISO 646 codeset: {, }, [, ], #, , ^, |, ~.

To be able to use character encodings where some or all of these symbols do not exist (such as the German DIN 66003), C++ defines the following alternatives composed of ISO 646 compatible characters.

Alternative tokens
There are alternative spellings for several operators and other tokens that use non-ISO646 characters. In all respects of the language, each alternative token behaves exactly the same as its primary token, except for its spelling (the stringification operator can make the spelling visible).

...

Compatibility with C
The same words are defined in the C programming language in the include file <iso646.h> as macros. Because in C++ these are built into the language, the C++ version of <iso646.h>, as well as , does not define anything.[/quote]

So you might have been influenced by a C background in your view but in C++, which is what the IDE and OP is using, but as I said it’s there in most C (through the .h) and in C++ that’s part of the norm.

void setup()
{
    Serial.begin(38400);
    byte a = 1;
    byte b = 1;
    if (a OR b) Serial.println("It works");
}

Arduino: 1.8.6 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

WARNING: Category '' in library ArduinoUnit is not valid. Setting to 'Uncategorized'
C:\Users\Watson\Documents\Arduino\forum\forum.ino: In function 'void setup()':

forum:7:11: error: expected ')' before 'OR'

     if (a OR b) Serial.println("It works");

           ^

exit status 1
expected ')' before 'OR'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

C++ is case sensitive.

Lookup my link above - you’ll see proper spelling/capitalization and all the weird alternate representations like %: instead of #

Primary	Alternative
&&	and
&=	and_eq
&	bitand
|	bitor
~	compl
!	not
!=	not_eq
||	or
|=	or_eq
^	xor
^=	xor_eq
{	<%
}	%>
[	<:
]	:>
#	%:
##	%:%:

PS: we learn something new everyday and the more we learn the more we find about the immensity of what we don’t know... that’s the spice of life :slight_smile:

Sorry. my tests were incomplete. I tested 'OR' not 'or'. Upon further testing I found that GPP works with 'or'. It fails however, with 'OR' and GCC fails with both.

byte a = 1;
byte b = 1;
if (a or b) printf("It works\n");


g++.exe -std=c++11 garbage3.c -Wall -o garbage3.exe
Process started >>>
<<< Process finished. (Exit code 0)
garbage3
Process started >>>
It works
<<< Process finished. (Exit code 0)

gcc -std=c11 garbage3.c -Wall -o garbage3.exe
Process started >>>
garbage3.c: In function 'main':
garbage3.c:542:10: error: expected ')' before 'or'
     if (a or b) printf("It works\n");
        ~ ^~~
          )
garbage3.c:541:10: warning: unused variable 'b' [-Wunused-variable]
     byte b = 1;
          ^
<<< Process finished. (Exit code 1)

if (a OR b) printf("It works\n");

g++.exe -std=c++11 garbage3.c -Wall -o garbage3.exe
Process started >>>
garbage3.c: In function 'int main(int, char**)':
garbage3.c:542:10: error: expected ')' before 'OR'
     if (a OR b) printf("It works\n");
        ~ ^~~
          )
garbage3.c:541:10: warning: unused variable 'b' [-Wunused-variable]
     byte b = 1;
          ^
<<< Process finished. (Exit code 1)

gcc -std=c11 garbage3.c -Wall -o garbage3.exe
Process started >>>
garbage3.c: In function 'main':
garbage3.c:542:10: error: expected ')' before 'OR'
     if (a OR b) printf("It works\n");
        ~ ^~~
          )
garbage3.c:541:10: warning: unused variable 'b' [-Wunused-variable]
     byte b = 1;
          ^
<<< Process finished. (Exit code 1)

I’m not sure if that’s relevant to Arduino prograamin though. The English keywords (and, or) are more readable, and prevent problems when & and | are used by mistake.

Thanks for the tests - I like it !!

I’m on my mobile so can’t test

for C have you tried with modern standard ==> Using -std=c17 instead of c11 (ISO/IEC 9899:2011), just in case the corrections that are supposed to be automatically applied back into c11 are not Implemented in your environment ?

Worth trying also adding the -ansi parameter in C compilation, that would drive support for trigraph and thus possibly also support for digraph and alternate representation

Tested with -std=c17, gcc 8.1.0 and avr-gcc 8.1.0. Both still fail.

Thanks - what about -ansi ? (If I may ask, just curious)

OT: I sometimes despair that software engineering doesn't seem to have even reached the stage of the Whitworth thread.

-ansi brings up all kinds of other errors.