Servo returns to position for no apparent reason

Hello, I am new to Arduino and am having issues with the servo. Using a mega 2560 btw. So in this code I want the servo to rotate however much I put in the serial monitor and that is it, but the issue is after it rotates to that position, it rotates back to 0 after about a half second.

Here is the code:

#include <Servo.h>
int servoPin=27;
int servoPos=0;
Servo myServo;

void setup() {
  Serial.begin(9600);
  myServo.attach(servoPin);
  myServo.write(servoPos);
}

void loop() {
  Serial.println("What Angle for the Servo? ");
  while (Serial.available()==0){
  }
  servoPos=Serial.parseInt();
  myServo.write(servoPos);
}

Thanks in advance for any help!

Add a line to print servoPos, and see if there's any other stuff arriving. Might have something to do with the line ending setting in the monitor?

It seems to be running the whole loop again but reading an input value of 0. Here is what is shown:

What Angle for the Servo?
90
What Angle for the Servo?
0
What Angle for the Servo?

Although all I did was put in "90" once.
Thanks

Try

while (Serial.available()!=0)`

That should watch for an incoming char and only parseInt when you've started to send something. Right now, you try to parseInt all the time, and therefore if you don't type anything, parseInt times out and returns zero.
C

Hi,
how the Serial monitor is set.
Select the option "no line ending" and run your code again.

Serial_Monitor

1 Like

N'utilisez pas les broches 0 et 1 Sur l'Arduino pour les connecter !
Pour un code aussi simple, vous pouvez utiliser simplement comprendre le fonctionnement du servomoteur det utiliser vos fonctions ! C'est simple ! Ils utilisent un certain signal PWM. Renseignez-vous ! Bonne chance ! :+1:

You got it!
C

So I tried it but when I first start the program I get:

What Angle for the Servo?
0
What Angle for the Servo?
0
What Angle for the Servo?
0

repeating (I added Serial.println(servoPos); after the parseint so that is the number being returned)

After I then type a number it moves to that position but then it doesn't move to any future input that I give. So let's say I put in 90 it goes to 90 but then if I type 45 it doesn't move.

I did some playing around and if I put: while (Serial.available()==1){
then I get the same repeating issue at the start but then it works fine how I want it afterwards.

Thanks anyway

Hello,

I tried it out and is the best solution so far with the only issue being there is about a 1 second delay from when I put in the number to when the servo finally moves. The original code has basically no delay so I know it's possible for it to go faster. Do you know if there is any way to get rid of the delay?

Thanks!

Not been following very closely, so this might be nonsense, but isn't that the timeout associated with Serial.parseInt() which defaults to1000? (And could the 0 that you're getting be the 0 that's returned if there's no valid digit?)

parseInt() man page
setTimeout() man page

What works for me, no delay, is below:
But first, note that I set the Timeout to 5000 so it would be obvious, and set the Monitor to no line ending. When entering a number, do the following: Type the number, a space character, then hit enter. What that does is the space char convinces the parsing routine that the number has ended.
C

#include <Servo.h>
int servoPin = 27;
int servoPos = 0;
Servo myServo;

void setup() {
  Serial.begin(115200);
  myServo.attach(servoPin);
  Serial.setTimeout(5000);
  myServo.write(servoPos);
}

void loop() {
  Serial.println("What Angle for the Servo? ");
  while (Serial.available() == 0) {
  }
  Serial.println(servoPos);
  servoPos = Serial.parseInt();
  myServo.write(servoPos);
  Serial.println(servoPos);
}

Be aware that this will always return 0 if you fail to enter a number before timeout; one way to deal with that is to simply make 0 a disallowed angle - for example, run from 01 to 91. That's kludgy, but you're just trying to make 0 a special case.
Hope that helps. There are better ways to do this simple input, but I tried to stay close to what you had tried. Others will likely throw in the throw-it-all-away-and-do-it-this-way examples.
C

Thanks to everyone who replied! I seem to have figured out the solution (combining what everyone here said) being the following code as well as no line ending for the serial monitor:

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

void setup() {
  Serial.begin(9600);
  myServo.attach(servoPin);
  Serial.setTimeout(10);
  myServo.write(servoPos);
}

void loop() {
  Serial.println("What Angle for the Servo? ");
  while (Serial.available() == 0) {
  }
  servoPos = Serial.parseInt();
  myServo.write(servoPos);
}
1 Like

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