Slowing Servo While reading data from a photo resistor (light sensor)

I am having trouble controlling the speed of the servo.
I have written the code in 2 ways to control the motor in various ways, and a slow moving version of either would be amazing. I am using delicate parts to attach to the motor so i need them to move more delicately.

The first version just reads the analog light sensor and continuously adjusts to the levels.

The second version sets the motor at 0 or 180 degrees. It successfully slows while climbing to 180 but then ratchets back down to 0 at full speed.

1st Version:

#include <Servo.h>
Servo myservo;
int potpin = 0;
int val;
void setup()
{
myservo.attach(9);
}
void loop()
{
val = analogRead(potpin);
val=map(val,400,900,0,9);
val=val*20;
myservo.write(val);
delay(50);
}

2nd Version

#include <Servo.h>
Servo myservo;
int potpin = 0;
int val;
int pos;
void setup()
{
myservo.attach(9);
}
void loop()
{
val = analogRead(potpin);
val=map(val,400,900,0,9);
if (val > 7)
{
for(pos; pos < 180; pos += 1)
{
myservo.write(pos);
delay(30);
}}

else if (val <= 7)
{
for(pos; pos > 5; pos -= 1)
myservo.write(pos);
delay(30);
}
delay(100);
}

THEBGeezy:
for(pos; pos < 180; pos += 1)

variable pos was not assign with any value, so it start with random numbers, your result is unpredictable.

check this out c-tutorial-for-loop

THEBGeezy:

else if (val <= 7)

{
      for(pos; pos > 5; pos -= 1)
      myservo.write(pos);
      delay(30);
    }



but then ratchets back down to 0 at full speed.

Because you for loop need to be Compound statement form else only the myservo.write(pos); is run therefore it at full speed.

Compound statement
A compound statement or block is a brace-enclosed sequence of statements.
{ statement...(optional) } (1)
When one statement is expected, but multiple statements need to be executed in sequence (for example, in an if statement or a loop), a compound statement may be used:

"pos" is a global, initialised to zero, so it won't be random, but an assignment is preferable.

Please remember to use code tags when posting code.

 if (val > 7)
...
...
   else if (val <= 7)

What else could it be?
The test on the else clause is pointless.

Apart from the various coding problems that have been identified you have a logical error because as soon as the servo runs through its movements loop() repeats instantly and it starts all over again. That's why it flies back.

You need a variable to keep track of the state of the servo and a variable to keep track of where you want it to move to. When they are both the same it won't move. When you press your button it should cause the moveTo position to change.

Something like this

void loop() {
   readButtons();
   moveServo();
}

void readButtons() {
    // if one button is pressed change servoTarget  to NNN
   //  if the other button is pressed change servoTarget to GGG
}

void moveServo() {
  if (servoPos > servoTarget) {
     servoPos = servoPos - 5;
  }
  else {
     servoPos = servoPos + 5;
  }
    myservo.write(servoPos);
   delay(30);
  }

}

And it might be a good idea to replace the use of delay() with the technique in the Blink Without Delay example sketch.

...R