Servos not responding to code

Hello, I am VERY new to coding with arduino and I am trying to make a robotic arm with 5 servos and they are controlled with buttons. I copied some code from a youtube video and the code is to make a servo go clockwise or counter clockwise from pressing 2 different buttons and I tried to add more buttons so I could control all 5 with 10 buttons. All the code uploads to the board with no problem but the only button that works is the L2 button and when I press it the servo locks up and it stops working. I dont know what to look for in the code to solve this so help would be very much appreciated.

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;

int angle =90;    // initial angle  for servo
int angleStep =5;

#define L2 A0   // pin 12 is connected to left button
#define R2  A1  // pin 2 is connected to right button
#define L3 A2
#define R3 A3
#define L4 A4
#define R4 A5
void setup() {
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo1.attach(2);  // attaches the servo on pin 9 to the servo object
  pinMode(L2,INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(R2,INPUT_PULLUP);// assing pin 2 as input for right button
  myservo1.write(angle);// send servo to the middle at 90 degrees
 Serial.println("Robojax Servo Button ");

 Serial.begin(9600);          //  setup serial
  myservo2.attach(3);  // attaches the servo on pin 9 to the servo object
  pinMode(L3,INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(R3,INPUT_PULLUP);// assing pin 2 as input for right button
  myservo2.write(angle);// send servo to the middle at 90 degrees
 Serial.println("Robojax Servo Button ");

 Serial.begin(9600);          //  setup serial
  myservo3.attach(4);  // attaches the servo on pin 9 to the servo object
  pinMode(L3,INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(R3,INPUT_PULLUP);// assing pin 2 as input for right button
  myservo3.write(angle);// send servo to the middle at 90 degrees
 Serial.println("Robojax Servo Button ");
}

void loop() {
  // Servo button demo by Robojax.com
  while(digitalRead(R2) == LOW){

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

      while(digitalRead(R3) == LOW){

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
       if(angle < 0){
        angle = 0;
       }else{
      myservo2.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");
     
      while(digitalRead(R4) == LOW){

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
       if(angle < 0){
        angle = 0;
       }else{
      myservo3.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");
       }
    }
    
  delay(100); // waits for the servo to get there
  }// while
 // Servo button demo by Robojax.com

  while(digitalRead(L2) == LOW){

    // Servo button demo by Robojax.com
    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
       }else{
      myservo1.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");

       while(digitalRead(L3) == LOW){

    // Servo button demo by Robojax.com
    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
       }else{
      myservo2.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");

       while(digitalRead(L4) == LOW){

    // Servo button demo by Robojax.com
    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
       }else{
      myservo3.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");
       }
    }
    
  delay(100); // waits for the servo to get there
  }// 

  
}
    }
       }
       }
    }
  }
       }
    }
      }
       }
    }
  }
}

buttons_with_arm_2_3.ino (4.07 KB)

Have you tried a set of simple, minimal sketches to exercise each servo individually?

yes I did. at first I tested only one servo and two buttons and it worked fine. it only stopped working when I uploaded the code where I added more buttons and servos.

noahklopf:
yes I did. at first I tested only one servo and two buttons and it worked fine. it only stopped working when I uploaded the code where I added more buttons and servos.

Sigh... that is contradictory. Did you test the other servos individually or not?

sorry, yes I did test them all and they all work fine.

Okay. I see this and ask, how do you know that the logic is nested properly?

 }
    }
       }
       }
    }
  }
       }
    }
      }
       }
    }
  }
}

So I really don't know if that is correct. when i first added the buttons and servos it sai "expected '}' at end of input" so I kept adding them until it didn't give me that error message anymore

Most servo problems reported on this forum result from inadequate servo power supplies. The Arduino cannot power any servos.

For five servos, you need a 5-6V, 5 Ampere (minimum) power supply. Please tell us what you are using.

I was originally using my computer and powering it through the arduino but im not doing that anymore, I have it hooked up to a 9V battery.

9V block batteries are for smoke alarms, and can't power ANY servos. For ONE to TWO servos, a 4xAA battery pack will work. Don't forget to connect the battery pack negative terminal to Arduino GND.

For five servos, you need a 5-6V, 5 Ampere (minimum) power supply.

Servos are extremely power hungry. Budget 1 Ampere for each small hobby servo. For powerful servos like MG966R, budget 2 Amperes per servo.