Generally you don't control the speed of a servo. You can adjust the voltage slightly to make it run faster or slower but generally this is not an option.
Could you give me some example, how does it look like?
Well, at its simplest, it is just a "for" loop with a delay.
for (int i = startAngle; i < endAngle; ++i) [
myServo.write (i);
delay (someDelayTime);
}
where someDelayTime is something like (totalTransitTime / (endAngle - startAngle)),
though this simple method doesn't always work well for multiple servos.
And I did it slightly differently.
I calculate where I want the servo to be every 20ms.
I tell the servo to go there, and then wait for the next 20ms slick to come by.
This timing matches the frequency of the signal being sent to the servo, so it is about as smooth as a typical hobby servo can be.
Let me push a bit of code through and connect a servo, and I will show you what I mean.
I recently finished a project to control 7 RC servos to open and close model railroad turnouts (slowly, instead of snapping open and close) with this library as the core of the code.
I just wrote this after cobbling together several different sketches I found on the web and it seems to work fairly well:
// 4 button 2 servo controller w pot slew v 3.d
#include <Servo.h>
Servo servo1; // define servo aliases
Servo servo2;
#define leftPin 2 // define buttons attached to pins
#define rightPin 3
#define upPin 4
#define downPin 5
int pos1 = 90; // set angle when inialitized
int pos2 = 135;
#define POT 3 // pot to analog pin 3
int potValue = 0; // variable for pot value
void setup()
{
servo1.write(pos1); // Put servo1 at home position
servo2.write(pos2); // Put servo1 at home position
servo1.attach(10); // attaches the servo on pin 9 to the servo object
servo2.attach(9); // attaches the servo on pin 10 to the servo object
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(upPin, INPUT);
pinMode(downPin, INPUT);
}
void loop()
{
//pot control
{
potValue = analogRead(potValue); // reads the value of the potentiometer
// (value between 0 and 1023)
potValue = potValue/4; // convert from 0-1023 to 0-255
}
// left/right servo control
if(digitalRead(leftPin) == HIGH) // left button instructions
{
// in steps of 1 degree
if( pos1 > 0)
--pos1;
servo1.write(pos1); // tell servo to go to position in variable 'pos1'
delay(potValue);
}
if(digitalRead(rightPin) == HIGH) // right button instructions
{
if( pos1 < 180)
++pos1;
servo1.write(pos1); // tell servo to go to position in variable 'pos1'
delay(potValue);
}
// up/down servo control
if(digitalRead(upPin) == HIGH) // up button instructions
{
// in steps of 1 degree
if( pos2 > 0)
--pos2;
servo2.write(pos2); // tell servo to go to position in variable 'pos2'
delay(potValue);
}
if(digitalRead(downPin) == HIGH) // down button instructions
{
if( pos2 < 180)
++pos2;
servo2.write(pos2); // tell servo to go to position in variable 'pos2'
delay(potValue);
}
}
At first it was written so a numerical parameter was inserted in the int potValue = 0; section but my need requires me to adjust the speed on-the-fly.
I can send you the numerical sketch is you wish.
This sketch controls two servos but it can be pared down if needed.
Hi everyone!!!
Currently i am trying to use RC transmitter to control the speed of my servo from moving from left to right...
Is this possible? & how would i going to write my code?
Hope any kind soul will reply my question...
Thanks!!!