Servo returning to 0 or 90

So I have a MG996R servo.

It is behaving oddly.

I try to give it a position with serial port using this code. But is always goes back to 0 degrees and sometimes even prints “0 degrees” in the serial monitor without me inputting 0. Also sometimes goes to 90 degrees.

#include <Servo.h> 
 
Servo myservo;  
 
int pos;
int servopin=9;
int longDelay=500;

void setup()  {
Serial.begin(9600);  
Serial.print("Angle ");
myservo.attach(servopin);
  
}

void loop()   
{
  while(Serial.available()==0){}
//  myservo.attach(servopin);
  pos=Serial.parseInt();
  myservo.write(pos);
  Serial.print(pos);
  Serial.println(" degrees.");
//  myservo.detach();
  delay(longDelay);

  
}

So I added the detach command that somewhat fixes it. I got it to reset itself without input once by spamming values in 20 or so tries. So problem fixed, but I still don’t know why it happened in the first place.

Then I used this guy’s code. But I couldn’t just give a 180 degree value or anything more than 120, because it would most of the time reset itself too after it reached the input value.

I needed to increase the angle after 120 in 10 or 20 degree increments, but it eventually got to 180 and stayed there.

// zoomkat 12-25-13 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo
int n; //value to write to servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(9, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  Serial.println();
}

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

      // attach or detach servo if desired
    if (readString == "d") {
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(9); //reattach servo to pin 9
      Serial.println("servo attached");
      goto bailout;
    }   

    n = readString.toInt();  //convert readString into a number

    // auto select appropriate value
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

bailout: //reenter code loop
    Serial.print("Last servo command position: ");   
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}

Can someone please explain to me this behavior? It wouldn’t be a problem. It is however annoying that it SOMETIMES resets, but other times does not and I do not know why.

Researching more my problem is I need a dedicated power supply as the servo is probably drawing more current than the arduino can supply causing it to reset itself.

I will acquire one and come back if I still have the problem.

@haclyion

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

I fixed all my problems.

Motors are working fine got their own power source. Though they are still crappy chinese brands that do not like to go their full advertised 180 degrees without a fuss and I cannot expect them to be calibrated the same.

Problem with value of 0 is that the Serial.parseInt(); function in the Newline mode will set off the program to wait for a new value after I input mine and will timeout to default of 0 after 1000ms because Serial.setTimeout(); is by default 1000ms.

Fixed this problem by either including a 'if' statement to exclude a value of '0' in the code or use 'No line ending.'

sigh I expected some feedback or help. I know I am a newbie, but still...
I don't know why I didn't get any?

haclyion:
sigh I expected some feedback or help. I know I am a newbie, but still...
I don't know why I didn't get any?

That would be because shortly after your first post you had found the most likely answer for yourself. And you didn't come back to ask for more help. We like people who do their own research and solve their own problems.

Congratulations.

Steve