How do we make servo keep last position after resetting arduino?

I have managed to control a servo for my project,but i have the following problem. When i power off arduino and for example my servo’s position is at 50 degrees and then i power arduino on again, my servo moves very fast to the default position (in my case this position is the 0 degrees). How can i make my servo to keep the position that had before powering off the arduino (50 degrees) (and not to move to the default position) when i power it on again? The code is the following

#include <Servo.h>
int button1 = 4;
int press1 = 0;
int button2 = 5;
int press2 = 0;
Servo servo1;
int pos = 0;

void setup()
{
pinMode (button1, INPUT);
pinMode (button2, INPUT);
servo1.attach (9);
Serial.begin(9600);
servo1.write (0);
}

void loop()
{
if ((digitalRead(button1)) == HIGH && (pos == 0)) // 0 to 50 rotation
{
for (int i = 0; i < 50; i ++)
{
pos ++;
servo1.write(pos);
Serial.println(pos);
delay(30);
}
}
if ((digitalRead(button2)) == HIGH && (pos == 50))
{
for (int i = 50; i > 0; i --)
{
pos --;
servo1.write(pos);
Serial.println(pos);
delay(30);
}
}
}

How can i make my servo to keep the position that had before powering off the arduino (50 degrees) (and not to move to the default position) when i power it on again?

You might be able to store the position in the arduino non volatile memory, but that might have issues. The best thing to do is park the servo in the desired position before shutdown, then start the arduino up with that position selected in the servo attach function.

Thank you very much for the immediate reply. Have you any idea how the code will be if i want to store the last position in the arduino's memory?

geoptic: Thank you very much for the immediate reply. Have you any idea how the code will be if i want to store the last position in the arduino's memory?

The EEPROM library.

As well as knowing the last position of the servo you will probably need to set that position before you attach the servo. Like this

myServo.write(lastPosition);
mySeervo.attach(servoPin);

I agree with @Zoomkat about sending the servo to the desired position before shutdown.

...R