Servo shifts to last position of other pwm servo

An anomaly I am having is each servo works independently but when servo1 is at any point past angle 90 and servo2 buttons are pressed - servo2 quickly moves to the last position angle of servo1 is and works fine back and forth afterwards. The serial plotter reflects this movement too. When using servo1 buttons, it too will move to the last angle of servo2 and start from that point. On power up each goes to the assigned angle 90 which is perfect for my project.

Any suggestions as to what I am overlooking? Thanks!

Edit: corrected post / guidelines and updated code to working copy adding additional angle as recommended to share - issue resolved.

#include <Servo.h>

Servo myservo1;
Servo myservo2;

int angle = 90;
int angleStep = 1;
int angle2 = 90;
int angleStep2 = 1;

#define LEFT 12
#define RIGHT  2
#define UP 8
#define DOWN  4

void setup() {
  Serial.begin(9600);
  myservo1.attach(9);
  pinMode(LEFT, INPUT_PULLUP);
  pinMode(RIGHT, INPUT_PULLUP);
  myservo1.write(angle);
  myservo2.attach(10);
  pinMode(UP, INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  myservo2.write(angle);
  Serial.println("Servo Button ");
}

void loop() {
  while (digitalRead(RIGHT) == LOW) {

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
      if (angle < 0) {
        angle = 0;
      } else {
        myservo1.write(angle);
        Serial.print("Moved to: ");
        Serial.print(angle);
        Serial.println(" degree");
      }
    }

    delay(100);
  }// while

  while (digitalRead(LEFT) == LOW) {

    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if (angle > 180) {
        angle = 180;
      } else {
        myservo1.write(angle);
        Serial.print("Moved to: ");
        Serial.print(angle);
        Serial.println(" degree");
      }
    }

    delay(100);
  }//

  while (digitalRead(UP) == LOW) {

    if (angle2 > 0 && angle2 <= 180) {
      angle2 = angle2 - angleStep2;
      if (angle2 < 0) {
        angle2 = 0;
      } else {
        myservo2.write(angle2);
        Serial.print("Moved to: ");
        Serial.print(angle2);
        Serial.println(" degree");
      }
    }

    delay(100);
  }

  while (digitalRead(DOWN) == LOW) {

    if (angle2 >= 0 && angle2 <= 180) {
      angle2 = angle2 + angleStep2;
      if (angle2 > 180) {
        angle2 = 180;
      } else {
        myservo2.write(angle2);
        Serial.print("Moved to: ");
        Serial.print(angle2);
        Serial.println(" degree");
      }
    }

    delay(100);
  }//

}

Read the forum guidelines. Post your test code in code tags. Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

Post a schematic of your wring.

I suspect that you need two angle variables, one for each servo.

Sorry, don’t understand your servo relationship criteria.

Please reword.

while( ) is a blocking function, try to avoid it for long duration events.

Give some examples of servo1 to servo2 positional relationship including timing.


Do your servos actually move to 180° ?

Good point and I will try working with that suggestion. Thanks

Yes, servos do move independently 180. An example I am experiencing:

  • power up and both servos set to 90
  • move servo1 to 45
  • servo2 button is pressed to move towards 135 or 30, servo2 jumps to the position servo1 is
  • either servo will go to the current position of the other when either of it's buttons are pressed and then work from that position as a starting point.

wildbill - adding another angle variable worked. Thank you!

Issue resolved.

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