servo sweep problem

Hi,

I am trying to do the servo sweep program found on the website. However, the servo does not sweep. I have the library and do not get any error messages. However, the servo does not sweep. This is the program I am trying:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(7); // attaches the servo on pin 7 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
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

am I supposed to be entering a number in place of pos on both of the
myservo.write(pos) commands ? If I use the code below to tell the servo to go to a specific angle, the servo moves there, so I take that as meaning the servo works and it’s wired right.

int servo1 = 7;
int myAngle1;
int pulseWidth1;

void servoPulse1 (int servo1, int myAngle1) {
pulseWidth1 = (myAngle1 * 11) + 500; // Converts angle to microseconds
digitalWrite(servo1, HIGH); // Set servo high (turns it on)
delayMicroseconds(pulseWidth1); // Wait a very very small amount
digitalWrite(servo1, LOW); // Set servo low (turns it off)
delay(20); // Typical Refresh cycle of servo (20 ms)
}
void setup() {
pinMode(servo1, OUTPUT);
}
void loop() {

for (myAngle1=0; myAngle1<=180; myAngle1++) {
myAngle1 = 180;
servoPulse1(servo1, myAngle1);
}

}

I appreciate any and all help. Thanks.

Try it with the unmodified servo sweep sketch with the servo connected to pin 9. It's usually a good idea to run example code unmodified, particularly if a modified sketch doesn't work. There is a comment in the sweep sketch that says the servo is attached to pin 9.

If you do need to alter an example for an application (or if you are having problems getting the example to work) RTFM is always a good idea.

The documentation for the servo library is here: http://arduino.cc/en/Reference/Servo The first line mentions that the library allows an Arduino board to control one or two servos on pins 9 and 10.

thanks,

I had already tried the unmodified code. Also, if I try the servo write example, unmodified, it does not work. Any ideas? I am using a traxxas 2055 servo, and the arduino diecimila.

how are you powering the servo?

I had already tried the unmodified code.

when you tried it, was the servo wired to pin 7 or pin 9?

Why did you change from pin 9 to pin 7?

Your first bit of code uses an output pin that isn't supported by the library. Your second bit of code doesn't use the library, it drives the servo with simple timing loops, and could use any pin.

If you use the example unmodified, and your servo is not connected to either pin 9 or pin 10, then the unmodified code will not work - just move the servo from pin 7 to pin 9 and all should be OK.

If you want to use the library, you must use either pin 9 or pin 10

Hi,

I started off with the servo on pin nine. It didn’t work so I tried changing the pin to 10. I also tried pin 7 arbitrarily. It could’ve been any pin. Just for the sake of clarity, I have loaded the code below, which is exactly as found on the arduino page, and the servo does not work.

// Sweep
// by BARRAGAN http://barraganstudio.com

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

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

Therefore, I’m wondering if the library might not be loaded right, or the servo and the signal generated from the servo library are not in sync. As for the library, I loaded it into the hardware/libraries folder, which made hardware/libraries/servo. I do not get any error messages while compiling or uploading. I tried to hold off on posting for help until I felt I exhausted what little I could find on this subject. Thanks.

Some servos draw more power than the Arduino power supply can provide, you could try powering the servo from a separate 5v supply (4 AA batteries would also be suitable). Don't forget to connect the Arduino ground to the servo ground and to the negative side of the battery. Tthe positive side of the battery only goes to the center (red) wire on the servo.

If that doesn't work, the servo may be faulty. Do you have some way of checking it with standard Radio Control gear?

I also tried pin 7 arbitrarily. It could've been any pin.

no, that library will not work on any pins other than 9 and 10

Good luck!

Hi,

This was going nowhere fast. I figured there must be something wrong with library or something. I checked the version of the arduino software versus mine. I had the 0011 version and the latest is the 0013 version. I downloaded the 0013 version and it works on pin 9. Yay! Thanks for all of the help and suggestions.

I am a little curious how the version of the Servo library released with version 0012 found its way into a directory with version 0011. Where did the your version of the Servo library come from?

anyway, good to hear you have it working. have fun!