¿How to slow down servo speed?

I'm trying to use an obstacle sensor to activate the servo. That's been accomplished.
However, servo speed is kinda fast fot what I'm using it (a rack and pinion). I tried delay but its not working.

This is the code I'm currently using:

#include <Servo.h>

Servo servo;

int sensor_pin = 7; //pin on ta el sensor de obstaculos
int servo_pin = 2; //pin on ta el servo
int val; //si algo se acerca al sensor

void setup() {
pinMode (sensor_pin, INPUT); //sensor como input
servo.attach(servo_pin); //attacha el servo a si pin
}

void loop() {
val = digitalRead(sensor_pin); //lee el sensor

if (val==0) //nada cerca del sensor
{
servo.write(0);
delay (200);
}

if (val==1) //cerca del sensor
{
servo.write(90);// grados a girar
delay (100);
}

}

Use smaller increments until you reach 90°

1 Like

What do you mean by increments?

Move one degree, wait, move one degree, wait, move one degree.....
The IDE has a servo>sweep example that shows you how to do that.
Leo..

1 Like

Do a search for the varSpeedServo library.

1 Like
#include <Servo.h>

Servo servo;

bool servoMoving      = false;

const byte sensor_pin = 7;      //+5V---[Sensor]---GND
const byte servo_pin  = 2;      //servo pin

byte moveTo           = 0;
byte currentLocation  = 0;
byte servoSpeed       = 10;     //10ms between increments
byte servoIncrement   = 2;      //degree between servo movement

int val;                        //sensor
int incDec            = 1;

unsigned long servoMillis;


//*******************************************************************
void setup()
{
  pinMode (sensor_pin, INPUT_PULLUP); //sensor

  servo.attach(servo_pin);

} //END of setup()


//*******************************************************************
void loop()
{
  val = digitalRead(sensor_pin);

  //*********************
  if (servoMoving == false && val == 0)
  {
    moveTo = 0;

    if (currentLocation >= moveTo)
    {
      incDec = -1;
    }

    else
    {
      incDec = 1;
    }

    //the servo is now going to move
    servoMoving = true;

    //start the TIMER
    servoMillis = millis();
  }

  //*********************
  if (servoMoving == false && val == HIGH)
  {
    moveTo = 90;

    if (currentLocation >= moveTo)
    {
      incDec = -1;
    }

    else
    {
      incDec = 1;
    }

    //the servo is now going to move
    servoMoving = true;

    //start the TIMER
    servoMillis = millis();
  }

  //*********************
  //if enabled, is it time to move the servo ?
  if (servoMoving == true && millis() - servoMillis > servoSpeed)
  {
    //restart the TIMER
    servoMillis = millis();

    moveServo();
  }

} //END of loop()


//*******************************************************************
void moveServo()
{
  if (currentLocation == moveTo)
  {
    //we have arrived
    servoMoving = false;

    return;
  }

  currentLocation = currentLocation + incDec;

  servo.write(currentLocation);

} //END of moveServo()

1 Like

Thank you so very much!

I see! The servo sweep uses the delay (I think?) and it wasn't working for some reson in my code.
Still, thank you :slight_smile:

Had a small mistake in the posted code. :pleading_face:


Here is version 2.

Points if you can tell what was wrong with the sketch in post #7.

//Version two           

#include <Servo.h>

Servo servo;

bool servoMoving      = false;

const byte sensor_pin = 7;      //+5V---[Sensor]---GND
const byte servo_pin  = 2;      //servo pin

byte moveTo           = 0;
byte currentLocation  = 0;
byte servoSpeed       = 10;     //10ms between increments

int val;                        //sensor
int incDec            = 1;

unsigned long servoMillis;


//*******************************************************************
void setup()
{
  pinMode (sensor_pin, INPUT_PULLUP); //sensor

  servo.attach(servo_pin);

} //END of setup()


//*******************************************************************
void loop()
{
  val = digitalRead(sensor_pin);

  //*********************
  if (servoMoving == false && val == 0)
  {
    moveTo = 0;

    if (currentLocation >= moveTo)
    {
      incDec = -1;
    }

    else
    {
      incDec = 1;
    }

    //the servo is now going to move
    servoMoving = true;

    //start the TIMER
    servoMillis = millis();
  }

  //*********************
  if (servoMoving == false && val == HIGH)
  {
    moveTo = 90;

    if (currentLocation >= moveTo)
    {
      incDec = -1;
    }

    else
    {
      incDec = 1;
    }

    //the servo is now going to move
    servoMoving = true;

    //start the TIMER
    servoMillis = millis();
  }

  //*********************
  //if enabled, is it time to move the servo ?
  if (servoMoving == true && millis() - servoMillis > servoSpeed)
  {
    //restart the TIMER
    servoMillis = millis();

    moveServo();
  }

} //END of loop()


//*******************************************************************
void moveServo()
{
  if (currentLocation == moveTo)
  {
    //we have arrived
    servoMoving = false;

    return;
  }

  currentLocation = currentLocation + incDec;

  servo.write(currentLocation);

} //END of moveServo()

1 Like

You did only have one delay, after the full movement.

There must have a small delay between each small movement.
Leo..

1 Like

Is it that "byte servoIncrement" was not in use? :thinking:

Yes, we could have used an increment variable but in this case we just needed an increment value of one (1).
The servoSpeed variable which is the time between incrementing adjusts our servo speed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.