# Mixing Boolean expressions

Hi there,
Is is possible to use logic and (&&) and logic or (||) in a same sentence?
actually I’m struggling with the same project for so long.

SR_0063_Digital_Code.ino (1.16 KB)

``````int hallPin =A0;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
int LED7 = 8;

int val;
int M;

void setup() {

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(A0, INPUT);

}

void loop() {

val /=4;

if (val == 2.5)
{

digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(500); // half a second for stability
}

if (val >> 2.5 && val <= 2.8)|| (val >= 2.31 && val << 2.5) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if (val >= 2.81 && val <= 3.1) || (val >= 2.0 && val <= 2.3) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if (val >= 3.11 && val <= 3.4) || (val >= 1.71 && val <= 1.99){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if (val >= 3.41 && val <= 3.7) || (val >= 1.21 && val <= 1.7) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if (val >= 3.71 && val <= 4.0) || (val >= 0.91 && val <= 1.2){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if (val >= 4.1 && val <= 4.5)|| (val >= 0.51 && val <= 0.9){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, LOW);

delay(100);
}

if (val >= 4.5111) || (val <= 0.5){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);

delay(100);
}
}
``````

if (val >> 2.5 && val <= 2.8 ) || ( val >= 2.31 && val << 2.5 ) {

should be

if ( (val > 2.5 && val <= 2.8 ) || ( val >= 2.31 && val < 2.5 ) ) {

I often write these in “order”

if ( ( 2.31 <= val && val << 2.5) || (2.5 < val && val <= 2.8 ) ) {

a solution could be writing a macro

// a <= x < b x is element of [a, b>
#define between(x, a, b) (((a) <= (x)) && ( (x) < (b)))

than that line becomes

if ( between(val, 2.31, 2.5) || between (val, 2.5, 2.8 )) {

or better

#define between(a, x, b) (((a) <= (x)) && ((x) < (b)))

if ( between(2.31, val, 2.5) || between (2.5, val, 2.8 )) {

Thanks friend.
what type of array that I can use to shorten this cord?

Keep in mind that C / C++ evaluates "from left to right " and terminates evaluation of compound condition when it "fails or is true " AKA A && B does no evaluate B if A fails. Similar with A || B - does not evaluate B when A is true.
Avoid testing floats with “=” always use “=>” or “=<”.

Compound conditions are prone to user errors, “shortening” them would be counterproductive.

Keep it readable and let the compiler do its work.

Your variable val is an int. You should probably hit up the reference section and see what types of values an int can hold. Most of your if conditions are ridiculous given that variable type. I see several that can never possibly be true.

SamSiriratne:
Thanks friend.
what type of array that I can use to shorten this cord?

You don't necessarily need an array. When a comparison evaluates as

``````true
``````

call a function which sets each digital output in turn either ON or OFF based on the bits in a (byte) parameter you have passed to it. See bitRead. The function could operate either with a

``````for()
``````

loop or by brute force., ie. one at a time in series, the way you're doing it now.

``````// pseudocode

if(comparisonA is true){
setLEDs(ob0110001);
}

if(comparisonB is true){ // and so on
setLEDs(ob0000010);
}

void setLEDS(byte bitControl){

set LED0 on
}
else set LED0 off

// and so on
}
``````

Thank you all of you.

232:
Avoid testing floats with “=” always use “=>” or “=<”.

You should avoid testing any datatype for equality with ‘=’ - always use ‘==’ ``````int hallPin =A0;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
int LED7 = 8;

void setup() {

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(A0, INPUT);

}

void loop() {

float voltage = sensorvalue* (5.0/1023.0);

if (voltage == 512) {

digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(500); // half a second for stability
}

if (480<= voltage <= 544) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if ((448<= voltage && voltage < 480) || (544 < voltage && voltage<= 576)){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if ((400<= voltage && voltage < 448) || (576 < voltage && voltage<= 624)){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if ((350 <= voltage && voltage < 400) || (624 < voltage && voltage <= 674)){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if ((300<= voltage && voltage < 350) || (674 < voltage && voltage<= 724)){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

delay(100);
}

if ((200<= voltage && voltage < 300) || (724 < voltage && voltage<= 823)){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, LOW);

delay(100);
}

if ((voltage <= 200)||(voltage >= 823)){
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);

delay(100);
}
}
``````

followed your advice and changed the code. hope I’ve got it right this time. thank you.

`if (480<= voltage <= 544) `Sadly no, not right.
Two comparisons, two variable references required.

``````int sensorvalue =analogRead(hallPin);
float voltage = sensorvalue* (5.0/1023.0);

if (voltage == 512) {
``````

There is 0 possibility that voltage is as high as 512. As written voltage will always be between 0 and 5.0.