Programing Servo

Hello everybody!

I have a question you may help me with.
I want to write a program which reads a value from a potentiometer and then a servo spins at a certain speed depending on the value of the potentiometer.

This si the code:

#include <Servo.h>

Servo servo;
int posicion; //variable que define la posición del servo
int velocidad; //variable que define la velocidad de giro del servo
int pot = 0; //variable que tomará el valor del potenciómetro

void setup() {
  servo.attach(3);
  servo.write(90);
  posicion = 90;
  delay(1500);
  pinMode(A0, INPUT);
  
}

void loop() {
  int pot0 = pot; //definimos una variable que tenga el mismo valor que pot y que empiece valiendo 0 para detectar cualquier cambio en la variable pot
  pot = analogRead(A0); //la variable pot toma el valor leído en A0
  velocidad = map(pot, 0, 1023, -90, 90); //se asigna un rango de valores que correspoderán a los valores de velocidad de giro del servo
  int vel0 = velocidad;//definimos una variable que tenga el mismo valor que velocidad para detectar cualquier cambio en dicha variable

  if (pot > pot0){     //si se gira el potenciómetro hacia la derecha
    
    if (velocidad > 0){      //si el valor de la velocidad es positivo
      while (posicion < 180){  //hasta que el servo llegue a la posición máxima (180º)
        posicion++;     //aumentamos en 1 el valor de posicion
        servo.write(posicion); //el servo gira una unidad
        delay(500/velocidad); //cuanto mayor sea el valor de velocidad el bucle se repetirá más rápido
        velocidad = map(pot, 0, 1023, -90, 90); //leer el valor de la velocidad
        if(velocidad != vel0){  //si el valor de la velocidad ha cambiado
          break;  //salir del bucle
        }
      }
    }

    if (velocidad < 0){      //si el valor de la velocidad es negativo
      while (posicion > 0){  //hasta que el servo llegue a la posición mínima (0º)
        int vel0 = velocidad;//definimos una variable que tenga el mismo valor que velocidad para detectar cualquier cambio en dicha variable
        posicion--;     //disminuímos en 1 el valor de posicion
        servo.write(posicion); //el servo gira una unidad
        delay(-500/velocidad); //cuanto mayor sea el valor de velocidad el bucle se repetirá más rápido
        velocidad = map(pot, 0, 1023, -90, 90); //leer el valor de la velocidad
        if(velocidad != vel0){  //si el valor de la velocidad ha cambiado
          break;  //salir del bucle
        }
      }
    }
  }
  if (pot < pot0){     //si se gira el potenciómetro hacia la izquierda
    
    if (velocidad > 0){      //si el valor de la velocidad es positivo
      while (posicion < 180){  //hasta que el servo llegue a la posición máxima (180º)
        int vel0 = velocidad;//definimos una variable que tenga el mismo valor que velocidad para detectar cualquier cambio en dicha variable
        posicion++;     //aumentamos en 1 el valor de posicion
        servo.write(posicion); //el servo gira una unidad
        delay(500/velocidad); //cuanto mayor sea el valor de velocidad el bucle se repetirá más rápido
        velocidad = map(pot, 0, 1023, -90, 90); //leer el valor de la velocidad
        if(velocidad != vel0){  //si el valor de la velocidad ha cambiado
          break;  //salir del bucle
        }
      }
    }

    if (velocidad < 0){      //si el valor de la velocidad es negativo
      while (posicion > 0){  //hasta que el servo llegue a la posición mínima (0º)
        int vel0 = velocidad;//definimos una variable que tenga el mismo valor que velocidad para detectar cualquier cambio en dicha variable
        posicion--;     //disminuímos en 1 el valor de posicion
        servo.write(posicion); //el servo gira una unidad
        delay(-500/velocidad); //cuanto mayor sea el valor de velocidad el bucle se repetirá más rápido
        velocidad = map(pot, 0, 1023, -90, 90); //leer el valor de la velocidad
        if(velocidad != vel0){  //si el valor de la velocidad ha cambiado
          break;  //salir del bucle
        }
      }
    }
  }
}

I don't know why it doesn't work. It might be because of the "break"s (I had never used them in a code).

Thank you for your help!

P.S: Excuse my English :smiley:

juli29piano:
I want to write a program which reads a value from a potentiometer and then a servo spins at a certain speed depending on the value of the potentiometer.

Maybe this will work. I assume writing approx 90 to the servo causes it to stop

int potVal = analogRead(potPin);
byte servoVal = potVal / 6 + 5; // gives range of 5 to 175 - should be good enough for testing
servo.write(servoVal);

...R