Servo: how to set specific angle?

Hello,

I'm trying to gain control over my first servo (TowerPro SG-90) via my Leonardo.

Example scripts (Sweep and Knob) works OK.

I wrote my first own code for servo which I hoped to turn my servo to specific angle, however after first "servo.write(angle)" my servo start to rotate like crazy.

I provided sufficient power (external) to the servo.

What’s wrong with this code?

#include <Servo.h>

int servoPin = 10;
Servo servo;

int angle = 0;

void setup() {
  servo.attach(servoPin);

  Serial.begin(115200);

  while (!Serial);
  Serial.println("Enter angle (int): 0 - 180");
}

void loop() {

  if (Serial.available() > 0) {

    angle = Serial.parseInt();
    Serial.println(angle);

    servo.write(angle);
    delay(1000);
  }
}

I would be grateful for help.

Greetings,
ML

macle7400:
Hello,

I'm trying to gain control over my first servo (TowerPro SG-90) via my Leonardo.

Example scripts (Sweep and Knob) works OK.

I wrote my first own code for servo which I hoped to turn my servo to specific angle, however after first "servo.write(angle)" my servo start to rotate like crazy.

I provided sufficient power (external) to the servo.

What’s wrong with this code?

#include <Servo.h>

int servoPin = 10;
Servo servo;

int angle = 0;

void setup() {
  servo.attach(servoPin);

Serial.begin(115200);

while (!Serial);
  Serial.println("Enter angle (int): 0 - 180");
}

void loop() {

if (Serial.available() > 0) {

angle = Serial.parseInt();
    Serial.println(angle);

servo.write(angle);
    delay(1000);
  }
}




I would be grateful for help.

Greetings,
ML

make sure that the grounds are all tied together.
What you describe is classic grounding issues.

Sounds like the recent outbreak of what seem like normal servos until a low value (0) is sent then the servo "spins" like 360 servo.

Try changing the initial position in the sketch from 0 to say 90 and see what that does.

What number did you type in? Try this test sketch:

/*
 Try this test sketch with the Servo library to see how your
 servo responds to different settings, type a position
 (0 to 180) or if you type a number greater than 200 it will be
 interpreted as microseconds(544 to 2400), in the top of serial
 monitor and hit [ENTER], start at 90 (or 1472) and work your
 way toward zero (544) 5 degrees (or 50 micros) at a time, then
 toward 180 (2400). 
*/
#include <Servo.h>
Servo servo;

void setup() {
  // initialize serial:
  Serial.begin(115200); //set serial monitor baud rate to match
  servo.write(90);
  servo.attach(10);
  prntIt();
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int pos = Serial.parseInt();
    pos = constrain(pos, 0, 2400);
    servo.write(pos);
    prntIt();
  }
}
void prntIt()
{
  Serial.print("  degrees = "); 
  Serial.print(servo.read());
  Serial.print("\t");
  Serial.print("microseconds =  ");
  Serial.println(servo.readMicroseconds());

Yes, my guess would be sending a value too low for the servo.
If you modify the servo.attach to give it non-default values, the problem might go away.

Servo.Attach(10,1000,2000);

vinceherman:
Yes, my guess would be sending a value too low for the servo.
If you modify the servo.attach to give it non-default values, the problem might go away.

Servo.Attach(10,1000,2000);

Hello,

Definitelly helped.. :wink:
Thank you.

However I have one more problem - after setting desired angle servo returns to 0 angle.

It looks like pure code error in sketch.

I have added one more print for debugging and.. I realized that after every repeat of main “if-clause” I receive another unintended integer: “0” as a input.

The code:

#include <Servo.h>

int servoPin = 10;
Servo servo;

int angle = 0;

void setup() {
 //servo.attach(servoPin);
 servo.attach(servoPin, 1000, 2000);

 Serial.begin(115200);

 while (!Serial);
 Serial.println("Enter angle (int): 0 - 180");
}

void loop() {

 if (Serial.available() > 0) {

   angle = Serial.parseInt();

   Serial.print("Setting angle:");  Serial.println(angle);

   servo.write(angle);
   delay(500);
 }
}

Results - serial output - with my tries: 10.. 20.. (0 is unintended side effect)

Enter angle (int): 0 - 180
Setting angle:10
Setting angle:0
Setting angle:20
Setting angle:0

Why?

Greetings,
M

I get your problem when I have the monitor line ending as newline, but not with No line ending