Ultrasonic sensor mounted on a servo

So I’m trying to figure out what’s going on here. I mounted a hc-sr04 ultrasonic sensor on a servo motor. Now, I wrote a simple program as a test to see if this idea of mine will work before I implement it on the whole robot. So it basically just scans for an obstacle, once something is less than 15cm from it, it turns to the left, scans, saves the value of the distance to the left, then it turns right, scans, saves the value of the distance to the right, and then prints to the serial monitor the one which is greater then 15cm.

This is the code:

#include <Servo.h>

#define echoPin A0
#define trigPin A1
long duration, distance, frontDistance, leftDistance, rightDistance;
Servo servo;

void setup(){
  servo.attach(7);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  servo.write(105); //turn straight
  scan();
  frontDistance = distance;
  
  if(frontDistance < 15){
    
    servo.write(30); //turn left
    delay(50);
    scan();
    leftDistance = distance;
    delay(500);
        
    servo.write(179); //turn right
    delay(50);
    scan();
    rightDistance = distance;
    delay(500);
    
    if(leftDistance > 15 || rightDistance < 15){
      Serial.println("There's more room to the left.");
    }
    else if(rightDistance > 15 || leftDistance < 15){
      Serial.println("There's more room to the right.");
    }
    else if(rightDistance < 15 || leftDistance < 15){
      Serial.println("There's no room anywhere");
    }
    else if(rightDistance > 15 || leftDistance > 15){
      Serial.println("There's room in both directions");
    }
  }
  delay(500);
  scan();
}


//Functions
int scan(){
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 distance = duration/58.2;
 delay(50);
}

Here is what I don’t understand. Two things happen.
1st: If I put an obstacle, like my hand, in front of the sensor, it first turns left and then it turns right. Then it returns to the forward position but it still prints that there’s more room to the right, which is not true because nothing is obstructing either left or right.

2nd: If I also put an obstacle to the left, it will print there’s more room to the left, which should be the opposite, and if there’s nothing in front of it returns to the starting position. But if I put an obstacle to the right, it’ll continue sweeping back and forth from left to right and won’t return to the starting position, and it’ll keep printing there’s more room to the right which again is not true. It’ll return to the starting position once I remove the obstacle from the right.

I have absolutely no clue what’s going on here. Can someone please help me?

Try this, instead of using the using || use &&, this way the code knows to compare the ranges from Left AND Right, as opposed to Left OR Right.

See if that works, if not it could be another issue somewhere else.

int scan(){

You’ve promised that you’re going to pass back an “int”, but you don’t. Instead you pass it back via a global.
pulesIn doesn’t do that, why do you?