Scanning Ping Sensor

I am trying to make a ‘scanner’ that will rotate around to determine which direction has the closest obstacle. I have mounted a HC-SR04 Ping sensor on a servo, and have them attached to an arduino running this program:

#include <Servo.h>
#include<NewPing.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //setting up newping

Servo pan; //create a servo object

const int button = 7;
int left, center, right;
int result;

void setup(){
  pinMode(button, INPUT);
  pan.attach(9);
  pan.write(90);
  Serial.begin(9600);
}
void loop(){
  if(digitalRead(button) == HIGH){
    pan.write(0);
    delay(500);
    left = sonar.ping();
    pan.write(90);
    delay(500);
    center = sonar.ping();
    pan.write(180);
    delay(500);
    right = sonar.ping();
    pan.write(90);
    compareDistance();
    Serial.print(result);
  }
 else{
  pan.write(90);
 }
}
  void compareDistance(){
    if(right>left){
      if(right>center){
        result = 1;
      }
      else{
        result = 2;
      }
    }
    if(left>right){
      if(left>center){
        result = 3;
      }
      else{
       result = 2;
      }
    }
  }

The idea is this:
the servo rotates left, scans, ans stores the distance. Then it does the same for ‘center’ and ‘right.’ These values are compared and in the serial monitor, it prints the direction that extends the least.
However, this isn’t working. The system only returns a ‘zero’ in the serial monitor. This is my first ping project, so I have no idea where to go. Does anyone know whats wrong?

so I have no idea where to go.

One obvious problem is that you are overusing global variables. You have a function that computes a value. Why does that function mess with a global variable? It should return a value that is stored in a LOCAL variable.

What values are you getting for right, center, and left (which should NOT be global)?

Don't right, left, and center have to be global, as they are used in both the loop and compareDistance?

The only way 'result' can be 0 is if 'right' is always equal to 'left'. Sounds like there is a problem with your sensor. Start with a sketch that senses the distance and reports it. Once you get that working you can try to try moving the sensor.

Don't right, left, and center have to be global, as they are used in both the loop and compareDistance?

No. You can pass data to functions. How do you suppose that digitalRead() knows what pin to read?