SC90 Micro servo returns to 0 before going to the point specified in the serial monitor

#include <Servo.h>
int servoPIN=9;
int servoPOS=165;
Servo(Servo);

void setup() {
Serial.begin(9600);
Servo.attach(servoPIN);
}

void loop() {
Serial.println("input servo pos");
while (Serial.available()==0) {
}
servoPOS=Serial.parseInt();
Servo.write(servoPOS);
}

here is my code, im quite confused about this, everything seems fine and im following a youtube tutorial because im not familar with the commands for servos. on their end it works fine?

when i input the position i want my servo motor to go, it goes exactly there, then returns back to 0?

any and all help appreciated :slight_smile:

Check the line ending in the serial monitor.
It should be "none"

#include <Servo.h>
int servoPIN=9;
int servoPOS=165;
Servo(Servo);

void setup() {
Serial.begin(9600);
Servo.attach(servoPIN);
}

void loop() {
  Serial.println("input servo pos");
  while (Serial.available()==0) ;

  servoPOS=Serial.parseInt();

  while (Serial.available()>0) Serial.read(); // any endings allowed

  Servo.write(servoPOS);
}

i believe parseInt() returns after a delay

consider

#include <Servo.h>
int servoPIN=9;
int servoPOS=165;
Servo(Servo);

void setup() {
    Serial.begin(9600);
    Servo.attach(servoPIN);
}

void loop()
{
    if (Serial.available()) {
        char buf [20];
        Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        Serial.println (atoi (buf));
    }
}

Hi, @clauzed

See if changing this;

  Serial.begin(9600);

to;

  Serial.begin(115200);

and changing the baud rate in the IDE monitor to 115200 as well improves things.

Tom.. :grinning: :+1: :coffee: :australia: