 # if statement not working

Okay, I have been frigging around with this stupid if statement which should not be a problem but cannot get it to work.

I am printing the variable out below and I get 179.00 at one point but the if statement does not work.

So what should happen is angle starts at zero and goes up by 17.9 until it reaches 179 at which point the flag flips the equation so that the system then counts down by 17.9 until it reaches zero however in one of my tries it went to -0.00 and kept going. what am I missing here?

After Calc: 179.00 Angle: 179.00 522 833 - 861 = -28 After Calc: 196.90 Angle: 196.90

``````      angle = angle + (17.9 * looking);
Serial.print("After Calc:  ");
Serial.println(angle);

if(angle == 179.00){  //If turned fully to the right, switch to looking left
looking = -1;
Serial.print("Looking Left");
}
else if(angle == 0.00){  //If turned fully to the left, switch to looking right
looking = 1;
Serial.print("Looking right");
}
``````

Do not use == for floating point comparison, use something like abs(value-179.00)<0.1.

Thank you, that fixed that. I would not have figured that out. That is some funky logic that I cannot say I have ever seen before.

``````   angle = angle + (17.9 * looking);
Serial.print("After Calc:  ");
Serial.println(angle);

if(abs(angle-179)<0.1){  //If turned fully to the right, switch to looking left
looking = -1;
Serial.print("Looking Left");
}
else if(abs(angle)<0.1){  //If turned fully to the left, switch to looking right
looking = 1;
Serial.print("Looking right");
}
``````

It's a problem of floating point. There is rarely an equality of computed values to constants, but the values are very close.