Go Down

Topic: while (function == true ) instead of while (expression == true)? (Read 224 times) previous topic - next topic

PaulMurrayCbr

Code: [Select]

  if the left sensor detects black and the right sensor detects white, turn left.
         while turning left, as long as the right sensor doesn't detect black, keep turning left.


In arduino programming, rather than using while loops, you use variables holding the current state.
Code: [Select]

boolean weAreCurrentlyTurningLeft = false;

void loop() {
  if(weAreCurrentlyTurningLeft) {
    if the right sensor isn't detecting black {
      straighten up
      weAreCurrentlyTurningLeft = false;
    }
  }
  else {
    if the left sensor detects black and the right sensor detects white {
      turn left
      weAreCurrentlyTurningLeft = true;
    }
  }
}
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Slumpert

When do you actually go straight?
How is your "turn" code going to know to stop at 90degrees?
What does your complier do with "==!" as an operator?

Why are you using "while"'s in the first place?



Go Up