Variables resetting after each servo loop

Hello there!
I'm fairly new to Arduino and was using a simple program to move and locate a servo motor of mine. What it's intended to do is wait for a serial input, allocate it to the newpos variable, then simply move the motor to that position. Works fine, except for that for each time I input a position, the code somehow returns a 0 for the newpos value after the initial input. Here's a simple example:

#include <Servo.h>
Servo myservo;
int newpos;


void setup() {
  // put your setup code here, to run once:
  myservo.attach(A0);
  Serial.begin(9600);
  Serial.print("angle : ");
  Serial.println(myservo.read());
}


void loop() {
  // Wait for message to arrive
  Serial.println("Enter new angle");
  while(Serial.available() == 0){
  }
  newpos = Serial.parseInt();

  // Verify newpos has updated
  Serial.print("newpos = ");
  Serial.println(newpos);

  // Ensure it is not greater than servo allows
  if(newpos<0 || newpos >180){
    Serial.println("");
    Serial.println("ERROR: Position exceeds bounds of motor");
    newpos = Serial.parseInt();
  }
  
  myservo.write(newpos);
}

now, if I input ONLY 10 followed by a 30, here's what is returned:
image

and each time I input the position, the motor goes there, then returns to the 0 position despite not entering that. Any guidance would be really appreciated. Thanks!!

Set your serial monitor to no line endings.

-_-
Thanks lol
That fixed it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.