#include <Servo.h>
#define SERVO_PIN   8
 int servoPos;

Servo myServo;

void setup() {
  Serial.begin(9600);
  myServo.attach(SERVO_PIN);
}

void loop() {
  servoControl(180,20,'f');
  delay(1000);
  servoControl(180,20,'r');
}

void servoControl(int angle, int delayTime, char dir){
  int position;
  if(dir == 'f'){
    for(int x=0; x<angle;x++){
      myServo.write(x);
      delay(delayTime);
      position = myServo.read();
      Serial.print("Pos = ");
      Serial.println(position);
    }
  }else if(dir == 'r'){
    for(int x=angle; x>0;x--){
      myServo.write(x);
      delay(delayTime);
      position = myServo.read();
      Serial.print("Pos = ");
      Serial.println(position);
    }
  }
}