problem with proximity sensor and servo

Hello,
I am trying to create a program in which the arduino pulls info from a proximity sensor. When the sensor is less than 15 inches away, a servo moves.
The problem occuring is twofold:

  1. the serial monitor only shows 1 number
  2. the servo just moves back and forth

Can anyone help me with this code?
Thanks, Aaron

#include <Servo.h>

Servo myservo; 

const int pingPin = 11;
unsigned int duration, inches;

void setup() {
  myservo.attach(9);
  myservo.write(90);
  Serial.begin(9600);
}

void loop() {
  pinMode(pingPin, OUTPUT);          // Set pin to OUTPUT
  digitalWrite(pingPin, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(pingPin, LOW);        // End ranging
  pinMode(pingPin, INPUT);           // Set pin to INPUT
  duration = pulseIn(pingPin, HIGH); // Read echo pulse
  inches = duration / 74 / 2;        // Convert to inches
  Serial.println(inches);            // Display result
  delay(200);               // Short delay
  if (inches<15)
  {
    myservo.write(180);
  }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Stop cross-posting. All other topics deleted.

Shrink the images.

Can anyone help me with this code?

It isn't the code that is a problem. It is your expectation that you can power the servo from the Arduino. You can't. You need a separate power supply for the servo.

I have a 9volt battery and the thing that you can plug you can plug it into on the arduino, but that is powering the arduino not the servo, could you please explain how I could go about powering the servo?

I have a 9volt battery

Give it up now. That is not going to power the Arduino and the 6V servo for long.

Get a real battery. Connect the + side to the + side of the Arduino connector and to the + side of the servo. Connect the - side to the -side of the Arduino connector and to the - side of the servo.