Trouble With Servo Sweep While Using the VarSpeedServo Library

Hello All,

After much forum hunting, Googling, and fiddling I’m hoping the community might have some thoughts on how to solve a problem I am having. I’m building a robotic dinosaur head that uses a servo to sweep the neck left to right and a second servo to open and close the jaw. I have my servos running off of a 10,000mAh USB battery and my board running on an independent power supply.

The trouble is that the jaw servo is running to a fully open position when it attaches and then only closes to the 90 degree position. What has me stumped is that if I run the default servo sweep sketch with the standard servo library I am able to position the servo at 10 degrees (open) and 0 degrees (closed) without any trouble. This leads me to think that the issue is in the code I’m using.

#include <VarSpeedServo.h>          //include the VarSpeedServo library

int EYES = 12;                      //LED eyes on pin 12  
VarSpeedServo neckServo;            //declares the neck servo
VarSpeedServo jawServo;             //declares the jaw servo
                                
void setup() 
{
Serial.begin(9600);                 //start serial communication
pinMode(EYES, OUTPUT);              //eyes are an output
digitalWrite(EYES, HIGH);           //eyes high 
delay(5000);                        //5 second delay
neckServo.attach (9, 1, 127);       //attach neck on pin 9 min speed 1 max 127  127
jawServo.attach (10, 1, 127);       //attach jaw servo pin 10 min speed 1 max 127

//The line above is where the jawServo goes wonky

/*
 * the for loop is meant to run the raw through a single iteration
 * of opening and closing to test the jaw servo the eventual plan
 * is for the jaw to answer to Voice Recognition which will be covered in the void loop
 */

  for (int j=1; j<2; j=j+1)             
   {                                    
      jawServo.write (10, 30, true);    //open jaw
      jawServo.write (0, 30, true);   //close jaw
    }
}

void loop() {
neckServo.write (35, 7, true);       //neck rotation is working fine
neckServo.write (95, 7, true);

}

Tech Specs:
ArduinoUno Rev3
IDE 1.6.12
VarSpeedServo Library (version attached)
jawServo
Radio Shack Micro Servo
Cat. No. 2730765
4.8 - 6.0 V
1.5/1.8 kg/cm

Any and all help is greatly appreciated!

Cheers,
Fenian

VarSpeedServo-master.zip (23.9 KB)

void loop() {
neckServo.write (35, 7, true);       //neck rotation is working fine
neckServo.write (95, 7, true);

}

Are you sure you want to tell the servo to oscillate between these two positions thousands and thousands of times every second?

EDIT: nevermind, I did not realise, that you are not using the Servo.h library. Have you tried swapping the servo pins?

lg, couka

Hey Couka,

I had not tried swapping pins. Based on your suggestion I did go in and swap the pin assignment and the wiring which gave me the same trouble as before. Just swapping the wiring causes the neckServo to swing hard to about 140 degrees and the jaw tries to slowly sweep (but in the wrong positions obviously).

Also, I realize I didn’t attach the library I’m working out of. I’m including that here.

Cheers,
Fenian

VarSpeedServo-master.zip (23.9 KB)

I believe you did attach it, I just didn't read carefully enough, my fault :D

So is it still Pin 10 which misbehaves or did the problem move with the servo?

lg, couka

The problem moved with the swapping of pins. The neck servo exhibited the same behavior the jaw was.

Cheers, Fenian

Just to update after more reading and tinkering with the code I’ve solved the issue. The default norm is for the servos to move to their center position when they attach. I was able to reset the default by servo.writeMicroseconds() where the value is the initial position I want the servo to attach to. Then I’ve used servo.write with either microseconds or degrees as well as a speed and a truth condition to delay the program moving forward until the servo has completed its range of motion.

The working code is below for anyone who it might help.

#include <VarSpeedServo.h>          //include the VarSpeedServo library

int EYES = 12;                      //LED eyes on pin 12  
VarSpeedServo neckServo;            //declares the neck servo
VarSpeedServo jawServo;             //declares the jaw servo

const int neckServopin = 9;
const int jawServopin = 10;
                                
void setup() 
{
Serial.begin(9600);                 //start serial communication
pinMode(EYES, OUTPUT);              //eyes are an output
digitalWrite(EYES, HIGH);           //eyes high 
delay(5000);                        //5 second delay
neckServo.attach (9);       //attach neck on pin 9 
jawServo.attach (10);       //attach jaw servo pin 10
neckServo.writeMicroseconds(1500);    //sets neck to midpoint
jawServo.writeMicroseconds(1000);     //sets jaw fully clockwise to zero position
/*
 * the for loop is meant to run the raw through a single iteration
 * of opening and closing to test the jaw servo the eventual plan
 * is for the jaw to answer to Voice Recognition
 */
  for (int j=1; j<2; j=j+1)             
   {                                    
      jawServo.write(1200, 7, true);    //open jaw (microseconds, speed, must finish)
      jawServo.write(1000, 7, true);   //close jaw (same as above)
    }
}

void loop() {
neckServo.write (35, 7, true);       //rotate neck to 35 degree position
neckServo.write (95, 7, true);      // rotate neck to 95 degree position

}