Servo position problem

Hi everyone,
im playing with Paul Mcworther's lessons but struggel with lesson. An angle must be put in to the serial monitor then the servo must move to that position, my servo does that but goes back to 0 after a second.

Here is the code. Please assist.

#include <Servo.h>
int servoPin=9;
int servoPos=180;
Servo myServo;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myServo.attach(servoPin);

}

void loop() {
// put your main code here, to run repeatedly:
Serial.println("Angle");
while (Serial.available()==0);{

}
servoPos=Serial.parseInt();

myServo.write(servoPos);

}

Thanks

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Your topic has been moved to the Programming category of the forum. Please take more care when choosing a category in which to start a topic

At a guess, change the Line ending setting of the Serial monitor to "No Line Ending" otherwise the line ending character(s) will be interpreted as user input

My guess too.

I wonder if the Iced Coffee guy made a point of that in his video, or maybe he said in a previous lesson that he was going to set it that way for all time and so should the students.

I've only watched a handful of his lessons, and TBH I hit the skip-ahead button quite a bit - I prolly miss quite a few little things.

  servoPos = Serial.parseInt();

  Serial.print("moving servo to "); Serial.println(servoPos);

  myServo.write(servoPos);

When strange things happen, sometimes throwing a print statement or two on in there can provide evidence and help point your investigations in one direction or another.

Try it before and after you fix the problem, if indeed we turn out to have identified what is the problem.

a7