I'm trying to understand the servo example code. I want the servo to start at a specific point, move at a pre-defined step size to another location, and then move back to the start position. Then repeat the same motion but faster.
My first for loop works exactly as intended. In my second for loop I tried to make the servo move to the same locations but faster. I defined the step size as 10 degrees, but the result is that the servo moves faster but the range of motion is about a third the distance traveled as when the step size is 1. Why would increasing the step size decrease the servo range of motion and how might I fix this? Any advice appreciated!
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 180; // variable to store the servo position
int loop1 = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
//120
//pos = 180;
//myservo.write(pos); // tell servo to go to position in variable 'pos'
//delay(15);
for (pos = 80; pos <= 180; pos += 1) // goes from 80 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
}
loop1++;
delay(2000);
for (pos = 80; pos <= 180; pos += 10) // goes from 80 degrees to 180 degrees
{ // in steps of 10 degrees
myservo.write(pos);
delay(15);
}
delay(5000);
loop1=0;
}
I tried to make the servo move to the same locations but faster
How? There are only two ways to move a servo faster. One is to provide more current. The other way is to delay a shorter amount between steps. You are not providing more current in the second loop, nor are you delaying less.
Thanks for the help! I thought speeding up the servo could be done by increasing the step size. I think you understand me but basically telling the servo to start at 80 and then move to 90, 100, 110, instead of starting at 80 and moving to 81, 82, 83.
I'm attempting to make a useless machine to turn itself off with a servo when you turn it on. I've seen the servo speed change in videos and assumed it was changed by adjusting the step size, to change servo speed I have to change the current supplied? That would be a shame because I'm using the 5V regulator off the arduino and not sure how I would vary the current.
You can't change the current supplied, you can only ensure you have Enough current available.
If you want the servo to move faster, decreaese the delay between move commands:
delay(15); // waits 15ms for the servo to reach the position
The step size is a property of the motor, not your code. Stepper motors are constructed to turn a fixed number of steps per revolution. This will be the specs for your motor (sometimes printed on the motor itself).
As others have said, the only ways to speed it up are to feed it more juice or shorten the amount of time it sits waiting between steps. Both of which will have limits to what they can achieve.
Damn. Looks like I may have been stupid. Read step size and assumed stepper motor. You are talking servo, not stepper, yes? How does step size factor in?
Not a lot of time for reading, thinking, & posting. It's a temptation to poot out a quick reaction when something seems clear at first glance. Of course first glance is seldom definitive...
Look at the for loop. In one he changes the angle by one degree every 15ms and the other changes the angle by 10 degrees every 15ms.
Yeah, well, I missed that. It's not the end of the world.
Changing the position in steps of 10, instead of steps of 1, SHOULD make the servo move faster, UNLESS the servo is moving as fast as it can.
One test would be to increase the delay() value in each loop, to see what affect that has. If increasing the delay time between steps has a visible impact, then we can assume that with the current delay, the servo is moving as fast as it can.