Pages: [1]   Go Down
Author Topic: Setting a servo to a position  (Read 853 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

#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?
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5572
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9562
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
// 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
  }
}


Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
#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
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9562
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!   
Logged

Pages: [1]   Go Up
Jump to: