Go Down

Topic: Attach servo reset position (Read 723 times) previous topic - next topic


I have 2 servos, and put them manually on X position, for example, servo1 = 20º and servo2 = 160º, but when I turn on arduino, setup() function execute attach() servos, then servos put in 90º, why ?
I need don't move servos when I execute attach in setup(). It's possible?

Code: [Select]

Servo servo1;
Servo servo2;

void setup()

  servo1.attach(5); //These code reset servo position...



When you attach a servo the arduino starts sending position information to it. By default, that starting position is 90. You can change that by doing a servo.Write before you do the attach.

However, you can't set the servo to some random manual position and expect that it won't move to its start position when you attach.


I need don't move servos when I execute attach in setup(). It's possible?

The below servo test code has a line where an initial servo attach position can be specified. Your problem is probably that the servos are in a different position when attached in the code. A possible solution would be to not attach the servos until they need to be sent to a specific positioned in the code.

Code: [Select]

// 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() {
  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.print("writing Angle: ");

    readString=""; //empty for next input

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


I have 2 servos, and put them manually on X position, for example, servo1 = 20º and servo2 = 160º,

Hi, when  you say manually, do you mean  you physically turn the shaft of the servo?
In my opinion I would NEVER force a servo to any position with power ON or OFF, the servo contains a gearbox and motor, the gearbox is usually, especially on cheap servos, nylon and designed ONLY for torque output.
When you force the servo, the gearbox will TRY to spin the motor at a high speed and  you will find you are having to use a lot of torque to do so. If  you overload the gearbox with too much torque then the gears will strip or break.

Tom.....off my soapbox now...     :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131