using returned funtions in if conditions

#include <AFMotor.h>
AF_DCMotor motor(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
#define trig_pin A0
#define echo_pin A1
#define trig_pin2 4
#define echo_pin2 3
#define trig_pin3 A2
#define echo_pin3 A3
#define trig_pin4 A4
#define echo_pin4 A5
void setup() {
Serial.begin(9600);
Serial.println(“Motor party!”);
motor.setSpeed(200);
motor.run(RELEASE);
motor2.setSpeed(200);
motor2.run(RELEASE);
//turn on motor #2
motor3.setSpeed(200);
motor3.run(RELEASE);
// turn on motor #2
motor4.setSpeed(200);
motor4.run(RELEASE);
}
void avoid_obs(int val1)
{
float dist,dur;
analogWrite(trig_pin,LOW);
delay(2);
analogWrite(trig_pin,HIGH);
delay(9);
analogWrite(trig_pin,LOW);
delay(2);
dur=pulseIn(echo_pin,HIGH);
dist=(dur/2)/29.1;
val1=dist;
return val1;
}
void avoid_obs2(int val2)
{
float dist,dur ;
digitalWrite(trig_pin2,LOW);
delay(2);
digitalWrite(trig_pin2,HIGH);
delay(9);
digitalWrite(trig_pin2,LOW);
delay(2);
dur=pulseIn(echo_pin2,HIGH);
dist=(dur/2)/29.1;
val2=dist;
return val2;
}
void avoid_obs3(int val3)
{
float dist,dur;
analogWrite(trig_pin3,LOW);
delay(2);
analogWrite(trig_pin3,HIGH);
delay(9);
analogWrite(trig_pin3,LOW);
delay(2);
dur=pulseIn(echo_pin3,HIGH);
dist=(dur/2)/29.1;
val3=dist;
return val3;
}
void avoid_obs4(int val4)
{
float dist,dur;
analogWrite(trig_pin4,LOW);
delay(2);
analogWrite(trig_pin4,HIGH);
delay(9);
analogWrite(trig_pin4,LOW);
delay(2);
dur=pulseIn(echo_pin4,HIGH);
dist=(dur/2)/29.1;
val4=dist;
return val4;
}

void movingf()
{
motor.run(FORWARD);
motor.setSpeed(200);
motor2.run(FORWARD);
motor2.setSpeed(200);
motor3.run(FORWARD);
motor3.setSpeed(200);
motor4.run(FORWARD);
motor4.setSpeed(200);
delay(50);
}
void movingb()
{
motor.run(BACKWARD);
motor.setSpeed(200);
motor2.run(BACKWARD);
motor2.setSpeed(200);
motor3.run(BACKWARD);
motor3.setSpeed(200);
motor4.run(BACKWARD);
motor4.setSpeed(200);
delay(50);
}
void movingr()
{
motor2.run(FORWARD);
motor2.setSpeed(200);
motor3.run(FORWARD);
motor3.setSpeed(200);
delay(50);
}
void movingl()
{
motor.run(FORWARD);
motor.setSpeed(200);
motor4.run(FORWARD);
motor4.setSpeed(200);
delay(50);
}
void allstop()
{
motor.run(RELEASE);
motor.setSpeed(0);
motor2.run(RELEASE);
motor2.setSpeed(0);
motor3.run(RELEASE);
motor3.setSpeed(0);
motor4.run(RELEASE);
motor4.setSpeed(0);
delay(50);
}
void loop()
{
if(avoid_obs(val1)>5)
{
movingf();
Serial.println("nothing is in range as obstacle!! ");
Serial.print(“moving for good”);
}
if(avoid_obs1(val1)<=5)
{
allstop();
Serial.print(“have to find new way”);
delay(6);
}
if(avoid_obs3(val3)>5)
{
movingr();
Serial.print(“moving right because its clear”);
delay(6);
}
if(avoid_obs(val4)>5)
{
movingl();
Serial.print(“moving right left”);
delay(6);
}else if(avoid_obs2(val2)>5) {
movingb();
Serial.print(“backing myself”);
delay(6);
}
Serial.print(“everything is stopped”);
Serial.println(“checking for gap”);
}

A void function cannot return a value.

Please remember to use code tags when posting code

AWOL:
A void function cannot return a value.

Please remember to use code tags when posting code

Geez! And, if you have a question, howzabout actually ASKING a question, and not simply posting code with no explanation whatsoever of what your problem is....

When your car is not running right, do you simply take it to the mechanic, park it and walk away, without telling him what the problem is?

Regards,
Ray L.

hello
sorry for not posting tags
and thanks for the advice could u please help me in this code i’m getting errors like this
please help me if you like …thank you.

proj_wheel_test:139: error: ‘val1’ was not declared in this scope

if(avoid_obs(val1)>5)

^

proj_wheel_test:145: error: ‘val1’ was not declared in this scope

if(avoid_obs1(val1)<=5)

^

proj_wheel_test:145: error: ‘avoid_obs1’ was not declared in this scope

if(avoid_obs1(val1)<=5)

^

proj_wheel_test:151: error: ‘val3’ was not declared in this scope

if(avoid_obs3(val3)>5)

^

proj_wheel_test:157: error: ‘val4’ was not declared in this scope

if(avoid_obs(val4)>5)

^

proj_wheel_test:162: error: ‘val2’ was not declared in this scope

}else if(avoid_obs2(val2)>5) {

^

exit status 1
‘val1’ was not declared in this scope

from this code :

#include <AFMotor.h>
AF_DCMotor motor(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
#define trig_pin A0
#define echo_pin A1
#define trig_pin2 4
#define echo_pin2 3
#define trig_pin3 A2
#define echo_pin3 A3
#define trig_pin4 A4
#define echo_pin4 A5
void setup() {
Serial.begin(9600);
Serial.println(“Motor party!”);
motor.setSpeed(200);
motor.run(RELEASE);
motor2.setSpeed(200);
motor2.run(RELEASE);
//turn on motor #2
motor3.setSpeed(200);
motor3.run(RELEASE);
// turn on motor #2
motor4.setSpeed(200);
motor4.run(RELEASE);
}
int avoid_obs(int val1)
{
float dist,dur;
analogWrite(trig_pin,LOW);
delay(2);
analogWrite(trig_pin,HIGH);
delay(9);
analogWrite(trig_pin,LOW);
delay(2);
dur=pulseIn(echo_pin,HIGH);
dist=(dur/2)/29.1;
val1=dist;
return val1;
}
int avoid_obs2(int val2)
{
float dist,dur ;
digitalWrite(trig_pin2,LOW);
delay(2);
digitalWrite(trig_pin2,HIGH);
delay(9);
digitalWrite(trig_pin2,LOW);
delay(2);
dur=pulseIn(echo_pin2,HIGH);
dist=(dur/2)/29.1;
val2=dist;
return val2;
}
int avoid_obs3(int val3)
{
float dist,dur;
analogWrite(trig_pin3,LOW);
delay(2);
analogWrite(trig_pin3,HIGH);
delay(9);
analogWrite(trig_pin3,LOW);
delay(2);
dur=pulseIn(echo_pin3,HIGH);
dist=(dur/2)/29.1;
val3=dist;
return val3;
}
int avoid_obs4(int val4)
{
float dist,dur;
analogWrite(trig_pin4,LOW);
delay(2);
analogWrite(trig_pin4,HIGH);
delay(9);
analogWrite(trig_pin4,LOW);
delay(2);
dur=pulseIn(echo_pin4,HIGH);
dist=(dur/2)/29.1;
val4=dist;
return val4;
}

void movingf()
{
motor.run(FORWARD);
motor.setSpeed(200);
motor2.run(FORWARD);
motor2.setSpeed(200);
motor3.run(FORWARD);
motor3.setSpeed(200);
motor4.run(FORWARD);
motor4.setSpeed(200);
delay(50);
}
void movingb()
{
motor.run(BACKWARD);
motor.setSpeed(200);
motor2.run(BACKWARD);
motor2.setSpeed(200);
motor3.run(BACKWARD);
motor3.setSpeed(200);
motor4.run(BACKWARD);
motor4.setSpeed(200);
delay(50);
}
void movingr()
{
motor2.run(FORWARD);
motor2.setSpeed(200);
motor3.run(FORWARD);
motor3.setSpeed(200);
delay(50);
}
void movingl()
{
motor.run(FORWARD);
motor.setSpeed(200);
motor4.run(FORWARD);
motor4.setSpeed(200);
delay(50);
}
void allstop()
{
motor.run(RELEASE);
motor.setSpeed(0);
motor2.run(RELEASE);
motor2.setSpeed(0);
motor3.run(RELEASE);
motor3.setSpeed(0);
motor4.run(RELEASE);
motor4.setSpeed(0);
delay(50);
}
void loop()
{
if(avoid_obs(val1)>5)
{
movingf();
Serial.println("nothing is in range as obstacle!! ");
Serial.print(“moving for good”);
}
if(avoid_obs1(val1)<=5)
{
allstop();
Serial.print(“have to find new way”);
delay(6);
}
if(avoid_obs3(val3)>5)
{
movingr();
Serial.print(“moving right because its clear”);
delay(6);
}
if(avoid_obs(val4)>5)
{
movingl();
Serial.print(“moving right left”);
delay(6);
}else if(avoid_obs2(val2)>5) {
movingb();
Serial.print(“backing myself”);
delay(6);
}
Serial.print(“everything is stopped”);
Serial.println(“checking for gap”);
}

hello
sorry for not posting tags

not all that sorry, it seems.

i'm really sorry for being rude forgive me!!

please help me with errors or if you can please tell me if my code works or doesn't gonna work

i'll be thankful for your help...

Edit your posts

type [code] before the code
type [/code] after the code

if you can please tell me if my code works or doesn't gonna work

Until the code compiles, it isn't going to run. Until it runs, it isn't possible to say if it'll work or not work.
If you want to find out the correct way to return a value from a function, look at some of the examples provided in the IDE.
Also check out the "how to use the forum" threads posted at the top of most of the major sections of the forum to find out how to get the best answers.

error: 'val1' was not declared in this scope is because that variable isn't declared, or declared in another scope

void means a function return nothing. If it is supposed to return a value use that type, for example
int MyFunction () returns an integer

Looks like the OP could gain a LOT by studying arrays. That could eliminate about half of this code and seriously simplify the rest.

Any time you catch yourself putting numbers on the ends of the names of your variables, an array is what you really want. That way the compiler can work with those numbers too and you don't need a bunch of functions that are identical except for the subscripts on the numbers.

OK...less time playing Batman, more time reading about scope. Look at the post at the top by Nick Gammon titled Useful Links, then scroll down to C++ Programming and then read about Scope. All of the "val" variables are not defined at those places where you call them:

if(avoid_obs(val1)>5)

There is no val1 that is currently in scope for that statement. Ask yourself this: What do you think the value of val1 should be at that point in the program? If you give a number, show me the code where it got that value.

Finally, start using code tags or people are going to stop answering your questions.