Go Down

Topic: Slowing down servos. How to write it into the program. (Read 1 time) previous topic - next topic

PaulS

Quote
It just waved the servo about like a crazy thing.

Time to put up or shut up. Where IS your code?

PeterH

#6
Jan 30, 2013, 02:16 pm Last Edit: Jan 30, 2013, 02:23 pm by PeterH Reason: 1

that has not appeared to have worked. I have tinkered gto the best of my knowledge.


Post your new code.


I there a way to just slow down the PWM change to the servo so that all of it's movements are slowed to the same rate? It just waved the servo about like a crazy thing.



Yes there is. It'd need some changes to your code, though. The approach I'd take is to add a variable holding the current position of the servo, and a variable that holds the target position. At the point in your code where you currently move the servo, just update the target position instead.

Add some code that runs at regular intervals - every 10 ms would be a reasonable starting point. Look at the 'blink without delay' example sketch to see how to do things at regular intervals. Every interval, compare the current and target position of the servo. If they're different then move the servo one unit towards the target position. You will need to experiment with the interval to get the speed of movement that you require.

This technique could be extended easily to support multiple servos - just convert the variables involved to arrays and add code to apply the logic above to each element in the array.

If you were looking to make a very modular and reusable solution, you might go one step further and create a class that contains this logic (i.e. a speed-limited servo).
I only provide help via the forum - please do not contact me for private consultancy.

AWOL

There is a variable speed servo library somewhere.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

briggsy

My apologies. My aim is not to upset or annoy. I tinkered some more and got it to stop waving about by removing the time interval bit off the 90 degree part of the sketch. But it still moves quickly to 45 degrees with it in on that part. Here is how it currently stands.
Code: [Select]

//Upper Quadrant servo sketch 1
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
               
int yellowState;
int greenState;
int angle = 0;   
// constants won't change. They're used here to
// set pin numbers:

const int yellowPin = 3;
const int greenPin = 4;

void setup()
{
 
// initialize the trigger pins as an input:
   
pinMode(yellowPin, INPUT);
pinMode(greenPin, INPUT);
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop(){
// read the state of the trigger value:
yellowState = digitalRead(yellowPin);
greenState = digitalRead(greenPin);

  // check if either of the triggers are HIGH
  // if it is, the relavant State is HIGH:
                     
if (yellowState == HIGH) 
                                   
    for (int angle = 1; angle < 45; angle += 1)
  myservo.write (angle);
  delay (100);
       //sends servo to 45 degrees         
   
if (greenState == HIGH)

 
  myservo.write (90);
 
        //sends servo to 90 degrees
   
if ((yellowState == LOW) && (greenState == LOW))
   
  myservo.write (0);
 




AWOL that sounds promising.

Thanks again for all the help,
Briggsy

AWOL

#9
Jan 30, 2013, 02:25 pm Last Edit: Jan 30, 2013, 02:30 pm by AWOL Reason: 1
Code: [Select]
if (yellowState == HIGH)  
                                 
   for (int angle = 1; angle < 45; angle += 1)
 myservo.write (angle);
 delay (100);

When I wrote the example in reply #3, I very carefully put in a set of  { } braces.
They're important, and they're missing from your code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up