Go Down

Topic: Use value from a function in if else.. (Read 2708 times) previous topic - next topic

arduinofan90

May 03, 2015, 11:50 am Last Edit: May 03, 2015, 12:22 pm by arduinofan90
Hi

Trying to program a rc car. I want it to steer left and right if there are any obstacles on the route. I`m 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 I`m 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;
}

UKHeliBob

Code: [Select]
if (Right >10What value do you think this is testing ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

arduinofan90

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!

MarkT

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):
Code: [Select]

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

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

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




BTW the last arm of your test:
Code: [Select]
 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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

AWOL

#4
May 03, 2015, 12:27 pm Last Edit: May 03, 2015, 12:32 pm by AWOL
"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.

arduinofan90

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

AWOL

Quote
It didn`t work
Whatever that means.

arduinofan90

#7
May 03, 2015, 12:55 pm Last Edit: May 03, 2015, 12:55 pm by arduinofan90
I tried to copy the thing MarkT wroted
(int Left = Left () ;
 int Right = Right () ;
 int Front = Front () ;)

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



AWOL

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

Robin2

I tried to copy the thing MarkT wroted
(int Left = Left () ;
 int Right = Right () ;
 int Front = Front () ;)
That is not what @MarkT proposed.

All you need to do is change
 
Code: [Select]
if (Right >10 && Left > 10 && Front > 10)  

to
 
Code: [Select]
if (Right() >10 && Left() > 10 && Front() > 10)  
etc.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

arduinofan90

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<'

Delta_G

#11
May 03, 2015, 01:30 pm Last Edit: May 03, 2015, 01:31 pm by Delta_G
Yeah, you told the compiler that those functions don't return anything (the void).  And they don't have any return statements anyway.

Code: [Select]
void Front()



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

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Delta_G

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

Code: [Select]

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

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

arduinofan90

I tried to:

# define DISTANCE 10

and then say:

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

And I know how functions work, It`s just that I haven`t 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..
}


}

Delta_G

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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up