Setting a servo to a position

I have been trying to get a servo to move to a specific angle and stay there. I have been trying this…

#include <Servo.h>

Servo servoBase;
Servo servoLowerarm;
Servo servoUpperarm;
// a maximum of eight servo objects can be created
void setup()
{

servoBase.attach(9); // attaches the servo on pin 9 to the servo object
servoLowerarm.attach(10);
servoUpperarm.attach(11);
}

void loop()
{
servoLowerarm.write(180);
servoBase.write(10);
servoUpperarm.write(30);
}

The servos simply driving to their maximum in one direction and stay there. If I change the values to anything between 0 and 180 it changes nothing, they continue to drive in the same direction. Any ideas?

Without knowing anything about your servos, it will probably be difficult for anyone to suggest anything. So - maybe you should provide a link or something about your servo's specs/model/etc...

If I change the values to anything between 0 and 180 it changes nothing, they continue to drive in the same direction. Any ideas?

It does not sound like you are using a conventional servo. A conventional servo has hard stops that prevent the arm from rotating beyond defined positions.

If the servos have had those stops removed, to make them continuous rotation (not-really-)servos, then they will behave in just the manner you describe. As such, they are useless for your project.

Standard servos that rotatate in one direction to the travel stop may be receiving malformed control pulses, or the seperate servo power supply (don’t power servos from the arduino) is not grounded to the arduino ground. Below is some simple servo test code you can try with your servo setup.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

zoomkat: Standard servos that rotatate in one direction to the travel stop may be receiving malformed control pulses, or the seperate servo power supply (don't power servos from the arduino) is not grounded to the arduino ground. Below is some simple servo test code you can try with your servo setup.

I'll try this as soon as I can.

The servos are Hitec HS422 and Futaba S3004. I have used these servos before and they have worked fine. Just to clarify, they dont move 360 degrees. they do infact stop at their hard stops.

Ok, so 2 of my 3 servos are now working beautifully (not entirely sure what I did). However the last servo (servoBase) is still driving into its leftmost stop, regardless of what its being told to do. When the code is running, if you grab the servo and manually turn it to the right, it will try to drive back to the left. Here is the code I am using.

#include <Servo.h> 
 
Servo servoBase;   
Servo servoLowerarm;
Servo servoUp;
                
  void setup() 
  { 

    servoBase.attach(9);  // attaches the servo on pin 9 to the servo object
    servoLowerarm.attach(13);
    servoUp.attach(11);
  } 
 
 
void loop() 
{
   servoBase.write(170);   //0 left
   servoUp.write(180); //180 all up lowest servo
   servoLowerarm.write(180);  //180 all up higherservo
   delay(2000);
   servoBase.write(120);
   delay(1000);
   servoUp.write(125);
   delay(900);
   servoBase.write(180);
   servoUp.write(120);
   servoLowerarm.write(180);
   delay(3000);
   servoUp.write(180);
   delay(1000);
   servoLowerarm.write(90);
   delay(2000);
}

The servo is a HItec HS-422 and it does work fine on other programs. I have tried changing the angle and the pin it is assigned to, but so far nothing has worked. I have no Idea what is wrong. I am new at this and may simply be over looking a simple error.

Thanks

I would suspect you have a bad ground connection between the servo and the arduino. If the other servos just started working for no reason (maybe you bumped their wiring or similar), then wiring/connection for that servo is probably bad. Try jiggling its wiring.

NickB: The servo is a HItec HS-422 and it does work fine on other programs.

Do you mean that if you leave the hardware alone and upload a different sketch, you can get it working? If so that's significant because it points to a software problem, whereas all the other evidence seems to suggest a wiring fault.

zoomkat: I would suspect you have a bad ground connection between the servo and the arduino. If the other servos just started working for no reason (maybe you bumped their wiring or similar), then wiring/connection for that servo is probably bad. Try jiggling its wiring.

This was the issue. Thank you everybody for the help!

Hello guys.
I have a problem with my micro servo (Tower Pro SG90).
I want it to start always from the default/initial position.

My code is this:

#include <Servo.h>

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

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(90);    // direct servo to initial position - 90 deg
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

I have also tryed:

void setup() {
   myservo.write(90);    // direct servo to initial position - 90 deg
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

Potentiometer controling works like charm. But it always starts from the last position.
I have tryed it on Uno and Mega2560, and it doesn’t work.

What am I doing wrong?

myservo.write(90);

The library does this by default, what position do you want to start at? How are you powering the servo? If you wanted to start at a particular position, say, 30 degrees, do this in setup();

myservo.write(30);
myservo.attach(9);

You know that as soon as setup() is complete, the servo is going to jump to the pot position, try a 5 second delay as the first line in loop().

Thank you for your help. I discovered the problem. I didn't put delay. So it didn't have time to react.