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:

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!!