Maker Pro
Maker Pro

Slowing Servos

Rushmoore

Jun 21, 2015
26
Joined
Jun 21, 2015
Messages
26
Hi there,

I have a servo connected to a joystick such that when I move the joystick handle up the servo horn moves up and vice versa. The code for this is running though the arduino. At the moment the servo is moving far too fast, how can I do this?
Is there some code I can use as I can't seem to find any suggestions on the nets.
Here is the part of my code which controls the servo:
ServoVal = analogRead(joystick);
ServoVal = map(ServoVal, 0, 1023, 80, 0);
Myservo.write(ServoVal);

Thank you!
:)
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,727
Joined
Nov 17, 2011
Messages
13,727
When you set the servo position with these three lines, the servo will move to the new position as fast as it can.
To slow it down increment the position slowly e.g. within a loop. I'm not too familiar with arduino code, but surely you see what I mean:
Code:
OldServoVal = NewServoVal; // remember current position
NewServoVal = analogRead(joystick); // get new position
NewServoVal = map(NewServoVal, 0, 1023, 80, 0);

// this loop requires correct definition of pos...
if (NewServoVal > OldServoVal) // have to ramp up
   for (pos=OldServoVal; pos<NewServoVal;pos++) {
      Myservo.write(pos);
      delay(some_meaningful_delay); // fit to your needs
   }
else // have to ramp down
   for (pos=OldServoVal; pos>NewServoVal;pos--) {
      Myservo.write(pos);
      delay(some_meaningful_delay); // fit to your needs
   }

The code can surely be improved, it's just a quick shot from the hip.
 
Last edited:

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,727
Joined
Nov 17, 2011
Messages
13,727
Let us (me) know if it works, please.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Let us (me) know if it works, please.
Harald, this amazing. I've never coded in any form of C but after years of reading code snippets like yours I think I'm beginning to learn it through osmosis! :)

Chris
 
Top