"OR " , "AND" logic

Gents

I`m having a issue understanding something.

The logic is:

"While sensor x AND sensor y is connected OR sensor w AND sensor z is connected" stay high

But that scenario did not work, however if i do:

"While sensor x OR sensor y is connected AND sensor w OR sensor z is connected" stay high

seems to do what i want. Basically to stop "While" loop i need sens x,y or w,x to disconnect together and so on.

``````/* This program is to drive 8 PNP proxy sensors interfaced into the arduino to control "BiviOut1" and "BiviOut2" outputs.
All 4 inner sensors must be registered to set both outputs HIGH.
To set both outputs LOW inner and outer senors must disconnect together.
In this scenario if any of the 2 sensors disconnect together (6 and 7, or 9 and 8, or 5 and 4, or 2 and 3) biviOut1 and biviOut2 will also be LOW.
*/

//Outputs
int biviOut1 = A2;
int biviOut2 = A3;
//Inputs
// *oS = outer Sensors
int oSFL = 6; //Front Left
int oSBL = 9; //Back Left
int oSFR = 5; //Front right
int oSBR = 2; //Back Right
// *iS = inner Sensors
int iSFL = 7; //Front Left
int iSBL = 8; //Back Left
int iSFR = 4; //Front Right
int iSBR = 3; //Back Right

void setup() {
Serial.begin(9600);

pinMode(oSFL, INPUT_PULLUP);
pinMode(oSBL, INPUT_PULLUP);
pinMode(oSFR, INPUT_PULLUP);
pinMode(oSBR, INPUT_PULLUP);
pinMode(iSFL, INPUT_PULLUP);
pinMode(iSBL, INPUT_PULLUP);
pinMode(iSFR, INPUT_PULLUP);
pinMode(iSBR, INPUT_PULLUP);

pinMode(biviOut1, OUTPUT);
pinMode(biviOut2, OUTPUT);
digitalWrite(biviOut1, LOW);
digitalWrite(biviOut2, LOW);
}

void loop() {

if ((digitalRead(iSFL) == LOW) && (digitalRead(iSBL) == LOW) && (digitalRead(iSFR) == LOW) && (digitalRead(iSBR) == LOW)) // check if all 4 inner sensors sensed metal
{
Serial.println("REGISTERED");
onTrack(); // go to the onTrack and "while" loop
}
else
{
Serial.println("ALL DISCONECTED");
digitalWrite(biviOut1, LOW);
digitalWrite(biviOut2, LOW);
}
delay(100);
}

void onTrack() {

while //loop until some of the 2 sensors together disconnect and return to the main loop
&&
&&
&&
{
digitalWrite (biviOut1, HIGH);
digitalWrite (biviOut2, HIGH);
Serial.println("While Loop");
delay(100);
}

}
``````

I think your while statement is way too complicated. Basically what you want to do is break out of the while loop if any pair of sensors reads correctly.

``````   while(1) // forever
{
if(thisOne && thatOne)
break;
else if(thisTwo && thatTwo)
break;
else if(thisThree && thatThree)
break;

// None of the conditions necessary to exit the loop are true
}
``````

let's start with saying this is a blocking code. Most of the time it's nicer to create non-blocking code so you're able to do other stuff while waiting.

Next up:

mOskit:
[...] stay high

To let something stay high you simple don't need to do anything. Just don't make it low

mOskit:
"While sensor x AND sensor y is connected OR sensor w AND sensor z is connected"

That doesn't work because the statements "sensor x" and "sensor w" don't mean anything. Only "sensor y is connected" and "sensor z is connected" mean anything Dit you mean:
"(While sensor x is connected AND sensor y is connected) OR (sensor w is connected AND sensor z is connected)"?

Thanks guys. That helped a lot.
PaulS what does the (1) next to While in your example.

Septillion, this is exactly what I was trying to say.
"(While sensor x is connected AND sensor y is connected) OR (sensor w is connected AND sensor z is connected)"?

while(1) makes it loop forever (since 1 - or any other number that's not zero - evaluates to true), and he exits it using a break; statement. His code is much easier to read, and easier to put debug statements into to get some insight into what's going on.

That said, I don't see why your code doesn't work; I suspect that one of those pins you're reading doesn't have the voltage level on it that you think it does.

The code in my first post works just fine. Does exactly what I want. But the statement OR, AND are swapped over and when you read it.it doesn't make sense but is working fine.

Doesn't the While itself run forever even without the (1) next to it?

I tried to look over it but in the while I see not 4 but 8 inputs, correct?

But let's have
(A || B) && (C || D)
That's the same as
(A && C) || (A && D) || (B && C) || (B && D)

Not that A && B && C && D is also a valid state but both equations will short circuit after evaluating A && C.

Doesn't the While itself run forever even without the (1) next to it?

No. The while statement iterates while the condition in the parentheses is true. Without parentheses, there is a syntax error. With empty parentheses, there is a syntax error.

So, you need something in the parentheses that is always true to create an endless loop, or one where the body of the statement determines when the statement ends. 1 is always true. The way I wrote the body of the statement, the body decides when looping stops, not the while condition.

Septitilion, yes there are 8 inputs. 4 inputs in main loop to initiate While loop.

Writing the way you guys do.

In While Inputs A,B,C,D,E,F,G,H.

condition: &&=AND, ||=OR.
If any of the pairs disconnect = exit loop

(A&&B) || (C&&D) || (E&&F) || (G&&H) -> does weird thing. Can stop loop with single sensor disconnected.

(A||B) && (C||D) && (E||F) && (G||H) -> works as i want. Loop exits if any pairs in brackets disconnect.

However I will use the method PaulS suggested.

Thanks

P.S. Post edited.

So A and B, B and C etc form pairs? And you want at least one pair to be connected?

So I assume you mean (A&&B) || (C&&D) || (E&&F) || (G&&H)? Small difference

Shit. Yeah you right. Post edited
They bust behave as individual bodies. So what is in bracket is a body. they can all be disconnected or 3 pairs can stay on and only one pair disconnect. regardless to that, If any pair goes dark, While must stop.

``````while((A&&B) && (C&&D) && (E&&F) && (G&&H))
{
// do stuff
}
``````

mOskit:
If any pair goes dark, While must stop.

That's the inverse of the first statement. Just to recap:

• A and B, C and D etc form pairs?
• If one of the pins from a pair is connected you're good?
• Stop if one (or more) pairs loose both connections?

Apart from that, because all those inputs have a close relation I would say an array and looping over it would and break on a disconnect would make more sens. Easier to write, extend and view.

Pseudocode

``````bool pairs[4][2];

while(true){
for(byte i = 0; i < NrPairs; i++){
if( !( pairs[i][0] || pairs[i][1] ) ){
break;
}
}
}
``````
• A and B, C and D etc form pairs?

Yes

• If one of the pins from a pair is connected you're good?

Yes, If one within the pair is ON this mean that"Device" still see rail. (additional sensor is to cover double width).

• Stop if one (or more) pairs loose both connections?

Yes

``````bool pairs[4][2];

while(true){
for(byte i = 0; i < NrPairs; i++){
if( !( pairs[i][0] || pairs[i][1] ) ){
break;
}
}
}
``````

High Ride for me still ↑ , I don`t even know how to read it

First tip, quote-tags != code-tags And that piece of code isn't that complicated...

(A||B) && (C||D) && (E||F) && (G||H)
is what you try to do. And you just made a thinking mistake It read as, "all four pairs have at least one pin connected".

(A || B) => At least one of the first pair
&& (C || D) => and at least one of the second pair
&& (E || F) => and at least one of the third pair
&& (G || H) => and at least one of the fourth pair

(A&&B) || (C&&D) || (E&&F) || (G&&H)
That read as, "at least one pair (out of four) is connected with both connections".

septillion:
First tip, quote-tags != code-tags (-> previous post now edited) And that piece of code isn't that complicated...