Go Down

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

Berni86

Hi,
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()
{
  Serial.begin(115200);     

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


Regards,

wildbill

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.

zoomkat

Quote
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() {
  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
  }
}

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

TomGeorge

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

Go Up