Servo Angle Help

I am extremely confused on how to get a servo to move the way that I want it to. I know that by default 90 is the rest position of the servo. Everything I have read indicates that you just need to put the angle you want the servo to go through and it will do it. I cant seem to support that with my experiments though. I tried putting 45 and a huge delay in the loop (like 10s). I thought that it would slowly go to position 45 and then stop because of the delay but it didnt it just kept moving the whole time as though it had completely skipped over the delay. I dont understand the example Servo Sweep function with the for loop. Does the servo. write accept an angle and moves through that angle or does it receive an angle and rotate until it reaches that position? Then regarding the delay, what does this control, if I have a huge delay will it move through or too the same angle slower??? As you can tell im really confused. I have put my code below. I want the servo to go from 90 to 180 and then back from 180 to 90 but it just moves in the same direction constantly. Its annoying.

#include <Servo.h>

Servo myservo;

int pos = 0;

void setup()
{
myservo.attach(9);
}

void loop()
{
for(pos = 90; pos < 179; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}

Yes that sketch will go back and forth :slight_smile:
The basic code just to put it some place would be like
Edited from the Library
the 50 is the placement number

#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(A5);  // attaches the servo on pin Analog 5 
} 
 
 
void loop() 
{ 
     
  {                                
    myservo.write(50);              // tell servo to go  a s spot
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

Some basic servo test code to test the servo.

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

Nice :slight_smile:

Woody, the sketch does not go back and fourth when uploaded to the arduino. It just moves in one direction.

void loop()
{
for(pos = 90; pos < 179; pos += 1)
{
myservo.write(pos);
delay(30);
}
for(pos = 179; pos >= 90; pos -= 1)
{
myservo.write(pos);
delay(30);
}
}

This is what the code should be. sorry. it should go back and fourth starting and stopping in the same place.

for(pos = 179; pos >= 90; pos -= 1)

that one wil run it from Middle (90 Deg) to End (179 deg )
i uploaded it to mine _ love having a breadboard in front of me _
Ow , is yours a continuous servo :slight_smile: ?
Never mind i reread your post :slight_smile:

ya it is.i want it to go from middle to end and then back. does it work with your breadboard? it doesnt on mine and its if annoying.

ya - its going from one position to the next stop,, the rotate back stop, so on so on so forth :slight_smile:

lol why doesnt mine work :frowning:
it rotates, stops, and rotates in same direction CW. doesnt go back and fourth

Hm did u mess with zoomkat Neat little Code ? where u type in a value into Serial monitor and it goes to that position?

peter_zkr:
lol why doesnt mine work :frowning:
it rotates, stops, and rotates in same direction CW. doesnt go back and fourth

Is your servo perhaps an ex-servo, meaning one that has been modified for continous rotation?

Lefty

i was thinking that but i thought he was saying it goes in on direction and stops...

I want the servo to go from 90 to 180 and then back from 180 to 90 but it just moves in the same direction constantly. Its annoying.

Sounds like you have a continous rotation servo, which cannot be specifically sent to an angle position. Perhaps you can supply more info on the servo you have.

I have this servo:

is there something wrong with it, for the my intended purpose?

That is a continuous-rotation servo. It has been modified to not do what servos are supposed to do. You need a proper servo.

peter_zkr:
I have this servo:
Servo - Generic High Torque Continuous Rotation (Standard Size) - ROB-09347 - SparkFun Electronics
is there something wrong with it, for the my intended purpose?

As I said a continuous rotation servo is an ex-servo, it cannot be commanded to go to a certain position and stop. Instead it is capable of rotating in one or the other direction at a variable speed or to stop. It becomes just a variable speed, bidirectional, geared motor drive. And while it is controlled using the servo library commands it operates much different then a standard non-modified servo. Suppliers do buyers a big disservice by calling them servos and you are not the first to buy one by accident thinking you were getting a servo with some additional feature, rather then getting something that once was a servo, but has had a electronic lobotomy to convert it to a variable speed/direction geared motor.

Lefty