Servo

I’m trying to write a simple program for a servo:

http://www.servocity.com/html/hs-805bb_mega_power.html

But no matter what I try there is some sort of jittery spin that doesn’t go the whole distance.

I tried this basic code:
#include <Servo.h>

Servo servo; // 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

void setup(){
servo.attach(11, 0, 180); // attaches the servo on pin 9 to the servo object
}

void loop(){
servo.write(90);
Serial.println(servo.read());
delay(1000);
servo.write(0);
Serial.println(servo.read());
delay(1000);
servo.write(-90);
Serial.println(servo.read());
delay(1000);
}
It doesn’t even have a delay right now. It doesn’t seem to be paying attention to the code at all.

I tried the sweeper code and nothing worked.

The comment on the attach does not match the given pin. Have you connected the servo ground to the Arduino ground? You also can't write -90 to a servo.

Just tested your code and it runs fine, though it should be 0, 90, 180, not -90, 0, 90. Also you need to include Serial.begin(9600) to allow serial communication.

Are you powering your servo from the board? I've had an issue where the servo draws more than the board can provide, causing near enough what you describe. Also, I'm assuming you have not fiddled with any of the servo libraries at all?

Beyond that, test your Arduino with a non-servo sketch to see if the board is dead. If it's not that, it's either your wiring or the servo that's the problem.

Cynar

servo.attach(11, 0, 180)

The last two parameters are garbage. You need to pass as parameter the time in µs of the minimum and maximum position. Going by the datasheet, that would be 600 and 2400 µs, but that's pretty close to the default. So better do: servo.attach(11)or if you insist on giving the actual values:servo.attach(11, 600, 2400)

Korman

Oh the microseconds thing helped. I should have known that.

I’m getting the same problem I had before with the instruction code.

#include <Servo.h> 
 
Servo servo;  // 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 
 
void setup(){ 
  Serial.begin(9600);
  servo.attach(11, 600, 2400);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop(){   
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    servo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(100);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    servo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(100);                       // waits 15ms for the servo to reach the position 
  } 
}

It goes really fast in one direction and slow in the other.

Well I set it to 100 microseconds in my version to hopefully make the difference more obvious though. Occasionally it locks up and keeps trying to go the wrong direction to. I may know how to fix that though.

Does anyone know why this is happening?

Well I set it to 100 microseconds in my version

No servo is going to move very far in a tenth of a millisecond.

oops I meant milliseconds.

The code shows it as milliseconds

The code shows it as milliseconds

And the comment shows it as 15. How do I know you posted what you (thought) you wrote?

Occasionally it locks up and keeps trying to go the wrong direction to

I'm guessing this is a brown-out - how ae you powering the servo?

I'm just using the Arduino powered by the USB cord.

You shouldn't ever power servos off the Arduino - try using a separate supply like a four pack of AAs and see if that makes a difference.

And don't forget to connect the grounds.

I'm just trying to power 1 servo. I've noticed that the built in LED flashes every time it messes up and speeds backward.

Read replies 12 and 13.

There could be a few things messing you up…
Most servos I have fooled with do not like to be driven to their limits (min, max).
Limit numbers are unique to each servo model and brand
So you have to increase Min number and decrease Max number until your servo behaves.
Heavier servos likely draw down more current (especially if you have a load on the servo)

In any case, I have attached a simple servo sweep code which works fine on my nano. Hopefully it will run on your board… do vary the digital pin you connect the servo to.

Good luck!

PS> never mind the comments… they are a carry over from the original sweep 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

void setup()
{
myservo.attach(12); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 50; pos < 150; pos++) // 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(25); // waits 15ms for the servo to reach the position
}
for(pos = 150; pos>50; pos–) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(25); // waits 15ms for the servo to reach the position
}
}

I'm just trying to power 1 servo. I've noticed that the built in LED flashes every time it messes up and speeds backward.

If you insist on powering your servo off of the arduino power, perhaps you should try holding your breath until you are blue and see if that fixes the problem.

I was thinking that the LED flashing had to do with low power and I just wanted to confirm that.

I powered the servo with a separate power source and it works fine.

I have noticed that when you power off the servo and turn it back on it will revert back to its 0 position rather quickly. Is there anyway to prevent this?

How would I do that?