Dual condition "if" statement

My robot uses a Ping ultrasonic sensor and works great at avoiding things, but i want to add a servo to spin the sensor and choose which way to go. I need to create a dual condition "if" statement to accomplish this. here is my current attempt, but the compiler rejects it.

if (inches < 12){
    Stop();
    servo.write(2);
    for(int position=0; position<=180; position+=1) {
      servo.write(position);
    delay(10);
    }
    servo.write(180);
    for(int position=180; position>=0; position-=1) {
      servo.write(position);
    delay(10);
    }
    servo.write(90);
    delay(50);
    
    if((servo.write(position) < 90) && (inches > 12)){
      Reverse();
      TurnLeft();
    }
    if(servo.write(position) > 91 && inches > 12){
      Reverse();
      TurnRight();
    }
    
  }

In what way does it reject it? Does it give an error message? What is the error message?

What error do you get?

I think servo.write(position) in you IF statement doesnt return a value as it just gives the servo a command.

Instead of

 if((servo.write(position) < 90) && (inches > 12))

Try

 if((position < 90) && (inches > 12))

(I'm just making a suggestion as I'm a beginner in arduino)

You never update inches between here:

if (inches < 12)

and here:

if((servo.write(position) < 90) && (inches > 12)){

So I don't see how this statement will ever fire. In addition, does servo.write even return a value?

What you you mean by "the compiler rejects it"? I assume it means you get a compilation error? If so, post the error and ALL of your code.

Hi, since your code is only a fragment it would not compile, so I can't see the compiler message. What is the error message?

By the way, are you sure you want to test this:

if((servo.write(position) < 90) && (inches > 12)){

or this

if(servo.write(position) > 91 && inches > 12){

Apart from the differences in the usage of parentheses, the code checks that the number of bytes written to servo is lower/greater than 90/91. It's not a test on the value of position. And then, position is defined in a for loop which is already closed, so when you test for it it's already out of scope. Tools - autoformat would have shown that.

    if((servo.write(position) < 90) && (inches > 12)){

Reverse();
     TurnLeft();
   }
   if(servo.write(position) > 91 && inches > 12){
     Reverse();
     TurnRight();
   }

First this above code is only executed IF inches < 12 so not sure if that is causing you some problems or not.
Second, I'm not that familiar with the servo library but I don't believe servo.write() returns anything for you to test on in your last two IF statements.

Here is my entire code. I want the robot to decipher, at a certain servo position, which distance is longer, and from there to turn towards and drive in that direction. thanks for any help. and the error message was in my dual "if" statement where i said

if((servo.write(position) < 90) && (inches > 12)){
      Reverse();
      TurnLeft();
    }

the error message said "name lookup of 'position' changed for new ISO 'for' scoping"

#include <Servo.h>
# define maxspeed 250
# define halfspeed 125
# define drivespeed 200
Servo servo;
const int pingPin = 7;
int dirB = 13;
int dirA = 12;
int speedB = 11;
int speedA = 3;
int brakeB = 8;
int brakeA = 9;
int servopin = 10;
void setup() {
  
  Serial.begin(9600);
  pinMode(dirB,OUTPUT); //direction of ch. B
  pinMode(dirA,OUTPUT); //direction of ch. A
  pinMode(speedB,OUTPUT); //PWM ch. B
  pinMode(speedA,OUTPUT); //PWM ch. A
  pinMode(brakeB,OUTPUT); //Brake B
  pinMode(brakeA,OUTPUT); //Brake A
  servo.attach(servopin);
}

void loop()
{
  
  long duration, inches, cm;
  Drive();
 
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

 
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH); 
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
 if (inches < 12){
    Stop();
    servo.write(2);
    for(int position=0; position<=180; position+=1) {
      servo.write(position);
    delay(10);
    }
    servo.write(180);
    for(int position=180; position>=0; position-=1) {
      servo.write(position);
    delay(10);
    }
    servo.write(90);
    delay(50);
    
    if((servo.write(position) < 90) && (inches > 12)){
      Reverse();
      TurnLeft();
    }
    if(servo.write(position) > 91 && inches > 12){
      Reverse();
      TurnRight();
    }
    
  }
  
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);


long microsecondsToInches(long microseconds)
{
   return microseconds / 73.746 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
void Drive() {
  ReleaseBrakes();
  digitalWrite(dirA,0);
  analogWrite(speedA, drivespeed);
  digitalWrite(dirB,255);
  analogWrite(speedB, drivespeed);
}
  
void Stop() {
  delay(50);
  digitalWrite(brakeB,HIGH);
  digitalWrite(brakeA,HIGH);
  delay(500);
 }
void ReleaseBrakes() {
  delay(10);
  digitalWrite(brakeB,LOW);
  digitalWrite(brakeA,LOW);
}
void Reverse() {
  delay(50);
  ReleaseBrakes();
  digitalWrite(dirA,255);
  analogWrite(speedA, halfspeed);
  digitalWrite(dirB,0);
  analogWrite(speedB, halfspeed);
  delay(1500);
}
void TurnRight() {
  delay(10);
  digitalWrite(dirA,0);
  analogWrite(speedA, maxspeed);
  digitalWrite(dirB,0);
  analogWrite(speedB, maxspeed);
  delay(775);
}
void TurnLeft() {
  delay(10);
  digitalWrite(dirA,255);
  analogWrite(speedA,maxspeed);
  digitalWrite(dirB,255);
  analogWrite(speedB,maxspeed);
  delay(775);
}

You declared position inside a for loop, therefore, you cannot use it outside the for loop.

I see how that makes sense. how could i fix this?

You can fix it by changing the entire approach you are taking with your code.

Even if you declared position in the parent scope it still wouldn't work. Position will always be -1 in your if tests, as they are after a loop which ends when position >= 0 becomes false, which means it must be -1.

So your if statements are meaningless with the program in the form it is at the moment.

Step back and look at the program from a distance and think about exactly what you want to achieve, then break it down into steps.