Use value from a function in if else..

Hi

Trying to program a rc car. I want it to steer left and right if there are any obstacles on the route. Im having troubble using values from the left right and front sonarsensors in an if else. Hope somebody can help me.. For example, if the right sonar is less than 10 cm from any obstacle it should turn left. The functions works well, but the if else things Im having troubble with.

Here is the code:
#include <VarSpeedServo.h>

const int trigPinFront = 2;
const int echoPinFront = 3;

const int trigPinLeft = 4;
const int echoPinLeft = 5;

const int trigPinRight = 6;
const int echoPinRight = 7;

VarSpeedServo SteeringServo;

void setup() {

Serial.begin(9600);
SteeringServo.attach(8);

}

void loop()
{

if (Right >10 && Left > 10 && Front > 10) //SERVO MID POSITION WHEELS STRAIGHT FOREWARD AND START ENGINE FOREWARD
{
SteeringServo.write(90, 25, false);
// HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
}

else if (Right < 10) //TURN Left
{
SteeringServo.write(0, 25, false);
// HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
}

else if (Left < 10) //TURN RIGHT
{
SteeringServo.write(180, 25, false);
// HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
}

else if (Left < 10 && Right < 10) //SERVO MID POSITION WHEELS STRAIGHT FOREWARD AND START ENGINE REVERSE
{
SteeringServo.write(90, 25, false);
// HERE I WOULD ALSO SET THE MOTOR TO GO BACKWARDS
}

}

void Front()
{

long duration, cm;

pinMode(trigPinFront, OUTPUT);
digitalWrite(trigPinFront, LOW);
delayMicroseconds(2);
digitalWrite(trigPinFront, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinFront, LOW);

pinMode(echoPinFront, INPUT);
duration = pulseIn(echoPinFront, HIGH);

cm = microsecondsToCentimetersFront(duration);

Serial.print(cm);
Serial.print(" cm foran");
Serial.println();
delay(2000);
}

long microsecondsToCentimetersFront(long microseconds)
{
return microseconds / 29 / 2;
}

void Left()
{

long duration, cm;

pinMode(trigPinLeft, OUTPUT);
digitalWrite(trigPinLeft, LOW);
delayMicroseconds(2);
digitalWrite(trigPinLeft, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinLeft, LOW);

pinMode(echoPinLeft, INPUT);
duration = pulseIn(echoPinLeft, HIGH);

cm = microsecondsToCentimetersLeft(duration);

Serial.print(cm);
Serial.print(" cm Left");
Serial.println();
delay(2000);
}

long microsecondsToCentimetersLeft(long microseconds)
{
return microseconds / 29 / 2;
}

void Right()
{

long duration, cm;

pinMode(trigPinRight, OUTPUT);
digitalWrite(trigPinRight, LOW);
delayMicroseconds(2);
digitalWrite(trigPinRight, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRight, LOW);

pinMode(echoPinRight, INPUT);
duration = pulseIn(echoPinRight, HIGH);

cm = microsecondsToCentimetersRight(duration);

Serial.print(cm);
Serial.print(" cm Right");
Serial.println();
delay(2000);
}

long microsecondsToCentimetersRight(long microseconds)
{
return microseconds / 29 / 2;
}

if (Right >10What value do you think this is testing ?

thanks for your answer.
The value from the three functions.
I have Front as a function, Left as a function and Right as a function.
When I just run the program without the if else thing I get numbers on my screen measuring the distance from the three sonar sensors I have (left, right, front).
And I want to use theese values.
So if the right sonar sensor measures 8 cm it should go in a else if and say:

"There is less than 10 cm to an obstacle on my right side, I have to turn left"

Sorry for my bad english

Best regards, and thank you again!

A function is a value, its a pointer to the code. You seem to be comparing pointers
to code to numbers.... You need to call a function to get its result, by passing
it its arguments (even if it has zero arguments you must pass them):

 if (Right() >10 && Left() > 10 && Front() > 10)

You can avoid calling the same function multiple times by caching the result
in a local variable:

 int left = Left () ;
 int right = Right () ;
 int front = Front () ;
 if (right >10 && left > 10 && front > 10)  
 ....
 ....

BTW the last arm of your test:

 else if (Left() < 10 && Right() < 10)                      //SERVO MID POSITION WHEELS STRAIGHT

cannot possibly happen as the previous two arms cover its condition. You need to
test for the more specific cases first if they overlap.

"Right" is a pointer to a function.
It is almost certainly always going to be greater than 10 ( if you were allowed to compare a literal integer with a pointer).
Did you, perhaps mean to call the function "Right()"

BTW, having two three virtually identical functions is a recipe for hair loss, which is why function parameters were invented.

Please remember to use code tags when posting code.

Thanks. I agree that the last else if test is wrong, deleted it and tried to paste this in:
int left = Left () ;
int right = Right () ;
int front = Front () ;
It didn`t work.
If I understand you right AWOL, I want to call the value of the function Right and compare it to 10. And same with the otherones left and front.

Thanks

It didn`t work

Whatever that means.

I tried to copy the thing MarkT wroted
(int Left = Left () ;
int Right = Right () ;
int Front = Front () :wink:

and paste it in to my code over Void.Setup. But i still got an error message.

But you're not going to share the error message or the code and allow us to help you?

arduinofan90:
I tried to copy the thing MarkT wroted
(int Left = Left () ;
int Right = Right () ;
int Front = Front () :wink:

That is not what @MarkT proposed.

All you need to do is change

if (Right >10 && Left > 10 && Front > 10)

to

if (Right() >10 && Left() > 10 && Front() > 10)

etc.

...R

okay I see, I misunderstood.
But stil I go some error messages...

Radiostyrt_1.ino:29:31: error: invalid operands of types 'void' and 'int' to binary 'operator>'
Radiostyrt_1.ino:29:47: error: invalid operands of types 'void' and 'int' to binary 'operator>'
Radiostyrt_1.ino:37:22: error: invalid operands of types 'void' and 'int' to binary 'operator<'
Radiostyrt_1.ino:44:21: error: invalid operands of types 'void' and 'int' to binary 'operator<'

Yeah, you told the compiler that those functions don't return anything (the void). And they don't have any return statements anyway.

void Front()

I'd suggest googling for a tutorial on functions in C++. You seem to be missing the point of them.

I'm thinking these parts should be part of the function above them and those functions should probably return long and not void.

long microsecondsToCentimetersFront(long microseconds)
{
  return microseconds / 29 / 2;
}

I tried to:

define DISTANCE 10

and then say:

if (Right >DISTANCE && Left > DISTANCE && Front > DISTANCE) .
Didnt work...

And I know how functions work, Its just that I havent used them in this case before.

I only have used them in things like this:

void loop()

{
if (This is true) //Get the functionfun
{
functionfun()
}

}

void functionfun()
{
Here is the function..
}

}

arduinofan90:
if (Right >DISTANCE && Left > DISTANCE && Front > DISTANCE) .
Didnt work...

And I know how functions work,

Apparently not because you are still leaving the parenthesis off and trying to compare a value coming from a function that doesn't return one.

Yes I understand I`m doing something wrong, thats why I asked the question.
But for me the way I see it, before I began with the if else thing and just had Front() Left() Right() in the loop it came out with how many cm it was from the sensors.

Now I think I have to return only cm and not the hole function. And rename cm to cmRight cmLeft cmFront... Just return that value because its only a number not any text. maybe that will do it.

Try this:

#include <VarSpeedServo.h> 

const int trigPinFront = 2;
const int echoPinFront = 3;

const int trigPinLeft = 4;
const int echoPinLeft = 5;

const int trigPinRight = 6;
const int echoPinRight = 7; 

VarSpeedServo SteeringServo; 

void setup() {

  Serial.begin(9600);
  SteeringServo.attach(8);

}

void loop()
{


  long right, left, front;
  
  right = Right();
  left = Left();
  front = Front();
  
  if (right >10 && left > 10 && front > 10)         //SERVO MID POSITION WHEELS STRAIGHT FOREWARD AND START ENGINE FOREWARD
  {
    SteeringServo.write(90, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD 
  }



  else if (right < 10)                      //TURN Left
  {
    SteeringServo.write(0, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
  }


  else if (left < 10)                      //TURN RIGHT
  {
    SteeringServo.write(180, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
  }


  else if (left < 10 && right < 10)                      //SERVO MID POSITION WHEELS STRAIGHT FOREWARD AND START ENGINE REVERSE
  {
    SteeringServo.write(90, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO BACKWARDS
  }



}



long microseconds2cm(long microseconds)
{
  return microseconds / 29 / 2;
}


long Front()
{

  long duration, cm;

  pinMode(trigPinFront, OUTPUT);
  digitalWrite(trigPinFront, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinFront, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinFront, LOW);

  pinMode(echoPinFront, INPUT);
  duration = pulseIn(echoPinFront, HIGH);

  cm = microseconds2cm(duration);

  Serial.print(cm);
  Serial.print(" cm foran");
  Serial.println();
  delay(2000);
  
  return cm;
}

long Left()
{

  long duration, cm;

  pinMode(trigPinLeft, OUTPUT);
  digitalWrite(trigPinLeft, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinLeft, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinLeft, LOW);

  pinMode(echoPinLeft, INPUT);
  duration = pulseIn(echoPinLeft, HIGH);

  cm = microseconds2cm(duration);

  Serial.print(cm);
  Serial.print(" cm Left");
  Serial.println();
  delay(2000);
  
  return cm;
}

long Right()
{

  long duration, cm;

  pinMode(trigPinRight, OUTPUT);
  digitalWrite(trigPinRight, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinRight, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinRight, LOW);

  pinMode(echoPinRight, INPUT);
  duration = pulseIn(echoPinRight, HIGH);

  cm = microseconds2cm(duration);

  Serial.print(cm);
  Serial.print(" cm Right");
  Serial.println();
  delay(2000);
  
  return cm;
}

arduinofan90:
Yes I understand I`m doing something wrong, thats why I asked the question.
But for me the way I see it, before I began with the if else thing and just had Front() Left() Right() in the loop it came out with how many cm it was from the sensors.

Now I think I have to return only cm and not the hole function. And rename cm to cmRight cmLeft cmFront... Just return that value because its only a number not any text. maybe that will do it.

A whole function is never returned, only a value is returned from a function.

luisilva:
Try this:

#include <VarSpeedServo.h> 

const int trigPinFront = 2;
const int echoPinFront = 3;

const int trigPinLeft = 4;
const int echoPinLeft = 5;

const int trigPinRight = 6;
const int echoPinRight = 7;

VarSpeedServo SteeringServo;

void setup() {

Serial.begin(9600);
  SteeringServo.attach(8);

}

void loop()
{

long right, left, front;
 
  right = Right();
  left = Left();
  front = Front();
 
  if (right >10 && left > 10 && front > 10)        //SERVO MID POSITION WHEELS STRAIGHT FOREWARD AND START ENGINE FOREWARD
  {
    SteeringServo.write(90, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
  }

else if (right < 10)                      //TURN Left
  {
    SteeringServo.write(0, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
  }

else if (left < 10)                      //TURN RIGHT
  {
    SteeringServo.write(180, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO FOREWARD
  }

else if (left < 10 && right < 10)                      //SERVO MID POSITION WHEELS STRAIGHT FOREWARD AND START ENGINE REVERSE
  {
    SteeringServo.write(90, 25, false);
    // HERE I WOULD ALSO SET THE MOTOR TO GO BACKWARDS
  }

}

long microseconds2cm(long microseconds)
{
  return microseconds / 29 / 2;
}

long Front()
{

long duration, cm;

pinMode(trigPinFront, OUTPUT);
  digitalWrite(trigPinFront, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinFront, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinFront, LOW);

pinMode(echoPinFront, INPUT);
  duration = pulseIn(echoPinFront, HIGH);

cm = microseconds2cm(duration);

Serial.print(cm);
  Serial.print(" cm foran");
  Serial.println();
  delay(2000);
 
  return cm;
}

long Left()
{

long duration, cm;

pinMode(trigPinLeft, OUTPUT);
  digitalWrite(trigPinLeft, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinLeft, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinLeft, LOW);

pinMode(echoPinLeft, INPUT);
  duration = pulseIn(echoPinLeft, HIGH);

cm = microseconds2cm(duration);

Serial.print(cm);
  Serial.print(" cm Left");
  Serial.println();
  delay(2000);
 
  return cm;
}

long Right()
{

long duration, cm;

pinMode(trigPinRight, OUTPUT);
  digitalWrite(trigPinRight, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPinRight, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinRight, LOW);

pinMode(echoPinRight, INPUT);
  duration = pulseIn(echoPinRight, HIGH);

cm = microseconds2cm(duration);

Serial.print(cm);
  Serial.print(" cm Right");
  Serial.println();
  delay(2000);
 
  return cm;
}

See how in this code the Front, Left, and Right functions return a value? So now you can use them, and you don't have to do it this way with the intervening variables. If the Front, Left, and Right functions are written this way you can write:

if(Front() > 10){ .. do something}

Sweet! Thank you so much guys!
I know a function can only return one value not a whole function, I just didnt know how in this case.

You learn something new everyday;)