#include <Servo.h>

Servo myServo;            // create a servo object
int potPin = A0;          // analog pin connected to the potentiometer
int potValue = 0;         // variable to store the potentiometer value
int targetMicroseconds = 0;  // target position in microseconds based on potentiometer
int currentMicroseconds = 500;  // initial position in microseconds

void setup() {
  myServo.attach(9);        // attaches the servo on pin 9
  myServo.writeMicroseconds(currentMicroseconds);  // set initial position
}

void loop() {
  potValue = analogRead(potPin);   // read the potentiometer value (0-1023)

  // Determine the target microseconds based on potentiometer range


  //if (potValue >= 0 && potValue < 50) {
  //   targetMicroseconds = 500; // 500 chosen arbitrarily
  // } else


  if (potValue >= 50 && potValue <= 100) {
   targetMicroseconds = 685;
  } else
  if (potValue >= 101 && potValue <= 200) {
    targetMicroseconds = 870;
  } else if (potValue >= 201 && potValue <= 300) {
    targetMicroseconds = 1055;
  } else if (potValue >= 301 && potValue <= 400) {
    targetMicroseconds = 1240;
  } else if (potValue >= 401 && potValue <= 500) {
    targetMicroseconds = 1477;
  } else if (potValue >= 501 && potValue <= 600) {
    targetMicroseconds = 1662;
  } else if (potValue >= 601 && potValue <= 700) {
    targetMicroseconds = 1847;
  } else if (potValue >= 701 && potValue <= 800) {
    targetMicroseconds = 2032;
  } else if (potValue >= 801 && potValue <= 900) {
    targetMicroseconds = 2217;
  } else if (potValue >= 901 && potValue <= 1000) {
    targetMicroseconds = 2439;
  }

  // Gently rotate towards target microseconds at quarter speed
  if (currentMicroseconds < targetMicroseconds) {
    currentMicroseconds++;
    myServo.writeMicroseconds(currentMicroseconds);
    delay(1);   // delay to slow down rotation speed
  }
  else if (currentMicroseconds > targetMicroseconds) {
    currentMicroseconds--;
    myServo.writeMicroseconds(currentMicroseconds);
    delay(1);   // delay to slow down rotation speed
  }
}