Go Down

Topic: Two conditions in triggering (Read 68 times) previous topic - next topic


Hi guys i want my controller to trigger at values 150-250 and 1000-1200 i was wondering if i can have 2 conditions in an 'if' statement

Something like this:

if (total > 150 || total <250);
digitalWrite (11,HIGH);
else if ( total < 150 || total > 250);
digitalWrite(11, LOW);
if (total > 1000 || total <1200;
digitalWrite (11,HIGH);
else if ( total < 1000 || total > 1200);
digitalWrite(11, LOW);


That won't work.
No matter the value of total both if statements will evaluate as true, the else if parts are never reached.

What would work:
Code: [Select]

if ((total > 150 && total < 250) || (total > 1000 && total < 1200)) digitalWrite (11, HIGH);
else digitalWrite(11, LOW);

Also instead of using the pin number, better make that a variable. Then you can refer to the pin by that variable and if ever you want to change it you can change it in one place. Using a descriptive variable name also helps you remembering what it is you're referring to.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up