Problem Using Single axis gyro sensor with arduino

Hai guys;
I have a project on balancing the robot using single axis gyro sensor.
The sensor works great but the servo motor cannot lock it position even
angle of the surface is change.Here is the code:

#include <Servo.h>

Servo myservo;
int potpin = 0;
int val;

void setup()
{
myservo.attach(9);
}

void loop()
{
int val = analogRead(potpin);
val = map(val, -512, 512, -270, 270); //map gyro value
myservo.write(val); //move servo using map value
delay(100);
}

Now you see that, i have map the gyro analog value from -512 to 512 to -270(servo -270 degree) to 270(servo 270 degree) rotation.This code works but the servo only rotates once i moves the plane surface only and it did not lock the position at a surface i move.seems it return to initial position.Any suggestions?

With many thanks;
Khai

As soon as the gyro stops rotating, its output will return to the null, mid-way point, so your servo will also return to the centre position.
BTW, your "map" is incorrect; because an analogRead returns values 0..1023, it can never return -512.

So how to make the servo lock at the required position without moving the gyro?i mean i need to lock the servo position when the angular surface is change.