How to modify the servo sweep code to different angles?

The link to the code is:

The code is:

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#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(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 
  } 
}

This makes the servo motor go from 0 to 180 degrees and 180 to 0. If I wanted it to go from 0 to 30 and back, do I just change it to:

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#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(9);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 30; 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 = 30; 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 
  } 
}

What happened when you tried?

I compiled the second code and all it seems to do is make it turn one way.

all it seems to do is make it turn one way.

Is it really a servo? Not one of those continuous rotation no-longer-a-servo things?

Yeah, it’s this one http://www.spektrumrc.com/Products/Default.aspx?ProdID=SPMSS8000.

The code makes the servo turn clockwise until it can’t be turned anymore and it just stays there. When I try to bend it CCW, I can feel it forcing clockwise.

I don't see anything wrong in the original code or you modified version. Did the original code work correctly? If not, how is the servo powered ans what connections do you have to the Arduino?

pyroknife:
Yeah, it's this one Spektrum - The Leader in Spread Spectrum Technology.

The code makes the servo turn clockwise until it can't be turned anymore and it just stays there. When I try to bend it CCW, I can feel it forcing clockwise.

That is most likely because the servo library assumes and applies a default values assuming your servo can handle microsecond pulse lengths of:

default min is 544, max is 2400

And it maps those pulse width values to assume 0 to 180 if using the servo.write(angle_value) command.
The original R/C servo standard was a range of 1000 to 2000 microseconds and all servos will work with that range. Most servos do have 'overrange' below and above 1000 to 2000 but how much varies from specific servo brand and model you have. So you are most likely forcing your servo up against a mechanical stop and that is not a good thing. You need to either limit the range of values in your your servo.write commands to less then arduino 0-180 angle degrees or use the servo.writeMicrosecond command with actual pulse width values that your specific servo can handle safely. All servos cannot safely respond to 544 to 2400 microsecond range and all servos do not have a 0-180 degree mechanical travel range.

The arduino abstracted servo 'angle' positioning method is a flawed concept (it works on assumptions that may or may not to true for any specific servo) and helps prevent beginners to servos from understanding how they actual work.

Lefty

Servo test code you can try with your 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, 500, 2500);  //the pin for the servo control, and range if desired
  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
  } 
}

Hi,

Thanks for the above information, I’m trying to build a powder dispenser and this servo sweep should work perfectly for pulling the handle.

Have anyone got a wiring diagram for the above set ups please?

Which Arduino (there are about a dozen different kinds)? Which servo (there are about ten thousand different kinds)? :slight_smile: