Im getting stuck in a while loop because Im having trouble running a calculation on some sensor values. When I read my four sensors values and compare the ratio of the first two to the second two, I keep getting 0.00, no matter what the values are. Here's my code:

``````while ( ( (frontleft + frontright) / (backleft + backright) <= 0.8 ) || ( (frontleft + frontright) / (backleft + backright) > 1.2) ) {
if (backleft + backright > frontleft + frontright) {
myStepper2.step(10);
}
if (backleft + backright < frontleft + frontright) {
myStepper2.step(-10);
}

Serial.println(backleft);
Serial.print("\n");
Serial.println(backright);
Serial.print("\n");
Serial.println(frontleft);
Serial.print("\n");
Serial.println(frontright);
Serial.print("\n");
float ratio = (frontleft + frontright) / (backleft + backright);
float math = (5 + 6) / (4 + 5);
Serial.print("Math: ");
Serial.println(math);
Serial.print("Ratio: ");
Serial.println(ratio);
Serial.print("\n");
delay(500);

}
``````

Im getting values for my sensors (backleft, backright, frontleft, frontright), but 'ratio' always comes out 0.00. Because of that, Im getting trapped in the loop I believe. What's going on here?

Also, just to check, I added 'math' float as that quick calculation, but I'm getting a value of 1.00 in Serial Monitor. Why? It should be 1.222.

Read this: Integer arithmetic and overflow.

``````float math = (5 + 6) / (4 + 5);
Serial.print("Math: ");
Serial.println(math);
``````

The compiler is treating your expression (the RH side of the "=" sign) as integers. Thus it is working out:

``````11 / 9 = 1
``````

Then you assign that `1` to a float. Too late to make it a float! It is already truncated.

Try:

``````float math = float (5 + 6) / float (4 + 5);
Serial.print("Math: ");
Serial.println(math, 4);  // 4 decimal places
``````

You get:

``````Math: 1.2222
``````

This also looks wrong:

``````while ( ( (frontleft + frontright) / (backleft + backright) <= 0.8 ) || ( (frontleft + frontright) / (backleft + backright) > 1.2) ) {
if (backleft + backright > frontleft + frontright) {
myStepper2.step(10);
}
if (backleft + backright < frontleft + frontright) {
myStepper2.step(-10);
}
How many `backleft` variables do you have? The one in the `while` instruction will not be the one you are doing the analogRead on. It doesn't "forward read" variables. I bet you have a different `frontleft` / `backleft` etc. set of variables declared earlier. These will not be updated inside the loop.
(To make them get updated drop the word `int` from inside the loop).