Servo Speed Control

Hi I am trying to implement a speed control in my servo sweep movement. My aim is to use two digital switches to call two different delays during the sweep loop. This is a sample of the code:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position

int firstswitch = 3;  // position of switch 1
int secondswitch = 4;  // position of switch 2

 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode (firstswitch, INPUT);
  pinMode (secondswitch, INPUT);
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  if (firstswitch == HIGH) {
    myservo.write(pos);
    delay (20);
  }
    if (secondswitch == HIGH) {
      myservo.write(pos);
      delay (40);
    }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position
    
  }
  if (firstswitch == HIGH) {
    myservo.write(pos);
    delay (20);
  }
    if (secondswitch == HIGH) {
      myservo.write(pos);
      delay (40);
    } 
}
/code]

The code was able to compile but i didn't notice any change in the servo speed so i think there is a problem in my for loop.

The delay is meant to reduce the speed. If theres any one with an idea of how this can be fixed? Thanks

kelvin8:
Hi I am trying to implement a speed control in my servo sweep movement. My aim is to use two digital switches to call two different delays during the sweep loop. This is a sample of the code:

Comments in the code, with ***

void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  if (firstswitch == HIGH) {  
    myservo.write(pos);                  // *** sends the servo to 180, but it's already there
    delay (20);
  }
    if (secondswitch == HIGH) {
      myservo.write(pos);                 // *** sends the servo to 180, but it's already there
      delay (40);
    }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position
    
  }
  if (firstswitch == HIGH) {
    myservo.write(pos);                // *** sends the servo to 0, but it's already there
    delay (20);
  }
    if (secondswitch == HIGH) {
      myservo.write(pos);                 // *** sends the servo to 0, but it's already there
      delay (40);
    } 
}

The code was able to compile but i didn’t notice any change in the servo speed so i think there is a problem in my for loop.

The delay is meant to reduce the speed. If theres any one with an idea of how this can be fixed? Thanks

What did you change to try to slow the servo down?
What is the purpose of sending the servo to where it alreay is?

kelvin8:
My aim is to use two digital switches to call two different delays during the sweep loop.

In that case the most obvious approach is to read the appropriate switch position immediately before each delay and determine the delay duration based on the switch position. The only code changes would be around each call to delay() - you wouldn't need to replicate the control loops, as you seem to have done.

A servo can only be slowed down, and that would be by using a delay (preferably using the blink without delay method).

Now looking at your code, I see that you want to change the delay based on which of the two switches are set HIGH. That's fine to have, but your not reading your switches. Technically your comparing the pin number to HIGH, but not the pin's actual state.
You want to use the digitalRead( /* Pin Number */) function, to get the state of the pin, then compare that state to either HIGH or LOW.

I see where there is a mistake as you are right i didnt use the digital read so yes maybe the switch isn't being read.
I will try that now and yes the main aim is to turn on one switch that calls the delay and the other switch calls another delay

i tried using the digitalRead but got an error saying there were too many arguments in the digitalRead part. This is the code:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position

int firstswitch = 3;  // position of switch 1
int secondswitch = 4;  // position of switch 2

 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode (firstswitch, INPUT);
  pinMode (secondswitch, INPUT);
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
 digitalRead(firstswitch, secondswitch); 
  if (firstswitch == HIGH) {
    myservo.write(pos);
    delay (20);
  }
    if (secondswitch == HIGH) {
      myservo.write(pos);
      delay (40);
    }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position
    
  }
  if (firstswitch == HIGH) {
    myservo.write(pos);
    delay (20);
  }
    if (secondswitch == HIGH) {
      myservo.write(pos);
      delay (40);
    } 
}
/code]

digitalRead(firstswitch, secondswitch);

Not quite.

This is the right way.

if (digitalRead(firstswitch) == HIGH) {
myservo.write(pos);
delay (20);
}

Now do the same of SecondSwitch.

And once you have the switches reading correctly, you need to address the things I commented on in your code. After the first loop, and if one or both of the switches is HIGH, you send the servo to where it already is. You then do one of three things,,,

  1. delay 20 ms (if firstswitch is on)
  2. delay 40 ms (if secondswitch is on)
  3. or both 1 and 2 (if both switches are on)

You then do exactly the same thing, but with the servo ending up at 0, and repeat the whole process.

The reason your servos do not move more slowly when one or both switches are on, is because you still delay 15 ms in the for loops. You probably want to set a variable to hold a delay time.

Some very basic servo button test code.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}