Go Down

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

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.

luisilva

Try this:
Code: [Select]
#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;
}

Delta_G

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.

|| | ||| | || | ||  ~Woodstock

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

Delta_G

Try this:
Code: [Select]
#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:

Code: [Select]
if(Front() > 10){ .. do something}
|| | ||| | || | ||  ~Woodstock

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

arduinofan90

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

Go Up