Hi there,
I feel really dumb asking this, I have about 10 experience in programming/scripting but I am only about a year in with Arduino.
Ok here goes:
I have a while loop that I want to continue until two conditions are met. Here is an example:
int x;
int y;
int xGoal;
int yGoal;
boolean dataComm;
setup(){
Serial.begin(57600);
x=0;
y=0;
dataComm=false;
}
loop(){
getSerialFromData();//gets the values for xGoal and yGoal
startCounting();
}
startCounting(){
while(dataComm==true){
while((x!=xGoal)&&(y!=yGoal)){
if(x!=xGoal){
getSerialFromData(); //allows me to interrupt the counting and give it a new goal
if(xDiff<0){
x--;
}
else{
x++;
}
}
if(y!=yGoal){
getSerialFromData();
if(yDiff<0){
y--;
}
else{
y++;
}
dataComm=false;
}
getSerialFromData(){
//read data from serial to populate xGoal and yGoal;
xDiff=xGoal-x;
yDiff=yGoal-y;
dataComm=true;
}
}
Now, the above while loop will stop if either x==xGoal or y==yGoal but I don't want it to do that. I want it to stop when x==xGoal AND y==yGoal.
From all of my experience with C, C++, and C# && should be a logical AND, and this loop should keep going until both conditions are met.
However, It only works if I change
while((x!=xGoal) && (y!=yGoal))
to
while((x!=xGoal) || (y!=yGoal))
and I have no clue why. Can anyone explain what is going on?
I found the answer after I happened to come across a thread where someone was trying to satisfy two conditions in a while loop and I noticed that he used the double pipe and no one corrected him. Can anyone tell me why this is true?
Disclaimer: This is kind of pseudo code. There is a bunch more going on and I tried to include just the part confuses me. This actually moves some motors and listens to some sensors and gives me output.
Thank you,
Rob