While (function == true ) instead of while (expression == true)?

Hello! I am making a line follower and I would like some help.
I already made some functions, such as turnRight and turnLeft.
So, I want my robot to make a 90° degree turn using something like this:

 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.

I know "while" is meant to be used with an expression [e.g. while (something == true) ]. But I want to use it with a function instead, as "while (function == true). Something like: while (turnLeft == true). So, would that work?

// Lv = left sensor value,    Rv = right sensor value
     // 1 = black       0 = white
    
        if ((Lv == 1) && (Rv == 0))
  {
    turnLeft(255);
//255 is the turning speed
    while (turnLeft == true)
    {
      if (Rv==!1)
    {
      turnLeft;
    }
   }
  }]

If your function return a bool then yes of course it will work

while ( turnLeft(255) == true )

K0ld:
But I mean, would it work as "while turning left { do something }"?

while (turnLeft()){
// do some stuff
}

It depends on what your turnLeft function does. But basically yes.

There's no difference between putting a variable or putting a function that returns a value in an if statement or while statement. If it evaluates to true then it runs.

Just remember how code runs, one line at a time, and beware of blocking code. This will hit the line with "while" in it and there it will run the entire turnLeft function until it hits a return statement. Depending on whether that return statement returns true or false it will run what's in the while loop once and then come back around and run the turnLeft function again.

If they're good non-blocking functions then they'll interleave just fine and it will work the way I think you envision. If they are blocking with for loops or while loops or delay statements sitting and waiting for something to happen for a long time then it will seem to alternate.

If I understand correctly, you want the unit to make a turn and whiles in process of the turn do something else without losing control of the turn?

And you are asking if you can achieve a task switch by using a do while loop?

What uController are you using?

You need to add "bool" to indicate that this function will return boolean and to add "return" condition.
Code below will always return "1", so you need to add proper logic to return "0".

bool turnLeft(int speed)
{
  digitalWrite(dirA, LOW); // spin the right motor counter clockwise
  digitalWrite(dirB, HIGH); //spin the left motor clockwise
  analogWrite(motorA, 255); 
  analogWrite(motorB, speed);
  delay(2);
  return 1;
}

[/quote]

Also just to look smart ass – you don't have to "== true" part in here: "while ( turnLeft(255) == true )"
"while ( turnLeft(255) )" is fine.

K0ld:
I'm sorry, my first post is kinda confusing. Just to make it clear it is a line follower. The line is black and the background/floor is white.
Basically I want the unit to make a left turn when the left sensor detects black and the right sensor detects white (if left sensor detect a line, turn left).
So, while it is turning, I want it to keep turning left as long as the right sensor doesn't detects black.
Because when it complete the 90 degrees turn, the right sensor will eventually catch the line, detecting black. So it will stop turning left.
That's what I want to do.

If a "turnLeft" function you need to check if you still need to turn left, if so – return 1(true), else return 0(false) :wink:

P.S. in the code 1 = true = HIGH, 0 = false = LOW

I use uController as an indicator as to the device being used to do the thing and you did, actually, answer the question correctly.

K0ld:
Thank you for the responses.
I just don't understand how do I make it return 1, else return 0 (otherwise it will aways return 1 as you said). It needs an "if" but I don't know where to put it.

What do you envision the true and false to mean? Under what conditions should it return true? Under what conditions should it return false?

K0ld:
If the unit is turning left, return true. If the unit is not turning left, return false.

That function only turns the bot left. So when exactly would it return false?

Maybe what you want is a boolean variable called turningLeft that you can set to true whenever you call the turnLeft function and set to false when you call turnRight function.

above setup so it's at global scope...

boolean turningLeft = false;

in the turnLeft function:

void turnLeft(int speed)
{
  digitalWrite(dirA, LOW); // spin the right motor counter clockwise
  digitalWrite(dirB, HIGH); //spin the left motor clockwise
  analogWrite(motorA, 255); 
  analogWrite(motorB, speed);
  delay(2);
  turningLeft = true;
}

and same in turnRight except set it to false.

  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.

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;
    }
  }
}

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?

It's really confusing to create multiple variables with the same name, in different scope ("Rightvalue").

Hey everyone! It is me again. Sorry, I haven't posted anything in a long time and I forgot my K0ld account password. I can't remember it at all. Anyways, what I did was, instead of using the boolean alternative, I just made some modifications to the commands that tell the robot when to turn.
I wanted to do something that checks when the function is true, but I ended up just using some do ... while loops.
Here it is:

else if ((Leftvalue == black) && (Rightvalue == white)) //if the left sensor detects black and the right sensor detects white, the robot needs to turn left
    {
    int Rightvalue;
    Rightvalue = digitalRead(2); //the Rightvalue will be assigned whatever the right sensor reads (white or black). Basically this is function means "read X sensor".
    do
    {
      turnLeft(255);
      Rightvalue = digitalRead(2); //the robot will keep turning left and the sensor will keep reading what color it is...
    }
    while (Rightvalue == white); [color=teal]//until the right detects black.
  }

Basically: This will make the robot keep turning left until it finds the line again with the right sensor. The same happens for when it should turn right, but with the opposite directions.
This worked for the most part, but there were some problems which I fixed by adding more sensors.
Sorry for just leaving this post without saying anything. I just left to try to think of a way to implement my idea into the robot and forgot about this post. But thanks everyone for your help and suggestions.