Servo Motor keeps going back to zero after the loop ends.

Hi,
So I know there are similar issues in forum like this but I still couldn't resolve my issue.

Objective:

-Enter Servo Angle in Serial monitor (e.g. 90 deg)
-Servo motor should go to that angle incrementally
-Enter another angle after loop ends (e.g. 45 deg)
-Servo motor should go back to 45 incrementally

Circuit setup pic is attached. So do I need to supply even more current for this motor to work properly or is my code causing an issue?

Code is here:

#include <SoftwareSerial.h>
#include <Servo.h>

Servo servo01;

int servo1Pos;
int servo1PPos;

int speedDelay = 20;

void setup() {
Serial.begin(9600);
Serial.println("Hello");
servo01.attach(5);  
 
}

void loop() {
 
  Serial.println("Servo Angle?");
  while (Serial.available()==0){
    
  }
  servo1Pos=Serial.parseInt(); //e.g.90
   
  Serial.println(servo1Pos); 
  
  if (servo1PPos > servo1Pos){ 
    for (int j = servo1PPos; j >= servo1Pos; j--){
      servo01.write(j);
      delay(100);
    }
  }
  if (servo1PPos < servo1Pos){
    for (int j = servo1PPos; j >= servo1Pos; j++){
      servo01.write(j);
      delay(100);
    }
  }
  servo1PPos = servo1Pos; // 90
   
}

The following is what I get in sequence when I start Serial monitor: (refer to attached image)

  1. After I start serial monitor
  2. After I type in 90
  3. After servo motor physically reaches 90 deg (Code asks for servo angle)
  4. I type in 45, but physically the servo motor goes back to zero position and then goes to 45 deg.

So if anyone can help me out here, I am pretty new to arduino so probably doing some basic mistake or sth?

Extra Note:
Added Hello to test if board resets, it doesn't.

Image 28.png

Image 28.png

What is the serial monitor line-ending?

Sorry just saw your reply, not sure why I didnt get notification.
Anyways I am not sure what your question is?
You mean the end line? The last line is "Servo Angle?" because it waits for the next angle input

At the bottom of the serial monitor window is a control labelled “line ending”.
What is it set to?

It is set to "Newline"

Set it to “no line ending”

Retest

The motor does not move with "no line ending"
If I change back to New line then it works again.

Ah this one post per 5 min is really delaying me :confused:

sleepelite:
If I change back to New line then it works again.

Great.
Glad it’s fixed.

No sorry, didnt mean it got fixed. If I set to New line then motor is running. But we still have the issue of motor going back to zero after the second angle is inputed.

When setting to No line ending, the motor does not respond to any sort of input.

What do the debug prints show?
(With line ending set to “none”, of course)

Nothing out of the ordinary:
"Sketch uses 4816 bytes (1%) of program storage space. Maximum is 253952 bytes.
Global variables use 364 bytes (4%) of dynamic memory, leaving 7828 bytes for local variables. Maximum is 8192 bytes."

Those are not debug prints.
What does the serial monitor show when you set “No line ending”, then enter a value, followed by the “send” control?

It just shows:
"90
Servo Angle?"
after I put in 90 as input.

So, it doesn’t then print zero after that, like you had before?

True, it doesn't print 0 like before.

Great.
Now you can start debugging your code.

Goodnight.

Okay, will try and see.

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