Control multiple rc servos with multiple buttons

Hi everyone!
I want to control 5 rc servos with 5 corresponding buttons. For example, if button 1 is pressed the head of servo 1 rotates 180 degrees. If the same button is pressed again then the head of the same servo rotates back 180 degrees. I am not sure though whether I need to include a sweep function in order to get this to work. I have written 2 lots of code (one has a sweep function, the other doesn’t) was wondering if anyone could have a look at both of them and provide some feedback on which is closest to what I need in order to get it to work, and what code I still need to change or add in.

I also don’t know whether my circuit I have created for it is correct and I realise that this circuit would require a lot of power - but I am unsure how much power it would need. Would you mind giving feedback on not only whether my circuit is correct? but also how much power I would need to power it? and how to attach it to the circuit? - I have attached an image of the circuit to this post.

Thanks in advance to anyone who replies!

Code with Sweep function-

#include <Servo.h> // make sure Servo library is included
Servo myServo; // create servo object to control a servo

int pos = 0;
int pushPin1 = 3;
int pushPin2 = 4;
int pushPin3 = 5;
int pushPin4 = 6;
int pushPin5 = 7;

void setup() {

  pinMode(pushPin1, INPUT_PULLUP);
  myServo.attach(8);
  digitalWrite(3, HIGH);

  pinMode(pushPin2, INPUT_PULLUP);
  myServo.attach(9);
  digitalWrite(4, HIGH);

  pinMode(pushPin3, INPUT_PULLUP);
  myServo.attach(10);
  digitalWrite(5, HIGH);

  pinMode(pushPin4, INPUT_PULLUP);
  myServo.attach(11);
  digitalWrite(6, HIGH);

  pinMode(pushPin5, INPUT_PULLUP);
  myServo.attach(12);
  digitalWrite(7, HIGH);

  Serial.begin(9600);

}

void loop()
{

  if (digitalRead(pushPin1) == LOW); 
  {
  //the first button has been pushed and the first servo turns 180 degrees
  pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5);  
  }
  
  else {
  //the first button has been pushed again and the first servo turns back 180 degrees
  pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5); 
  }

  if (digitalRead(pushPin2) == LOW); 
  {
  //the second button has been pushed and the second servo turns 180 degrees
  pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5);  
  }
  else {
  //the second button has been pushed again and the second servo turns back 180 degrees
  }
  pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5); 
  }

  if (digitalRead(pushPin3) == LOW); 
  {
  //the third button has been pushed and the third servo turns 180 degrees
  pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5);  
  }
  else {
  //the third button has been pushed again and the third servo turns back 180 degrees
  }
  pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5); 
  }

  if (digitalRead(pushPin4) == LOW); 
  {
  //the fourth button has been pushed and the fourth servo turns 180 degrees
  pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5);
  else {
  //the fourth button has been pushed again and the fourth servo turns back 180 degrees
  }
  pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5); 
  }

  if (digitalRead(pushPin5) == LOW); 
  {
  //the fifth button has been pushed and the fifth servo turns 180 degrees
  pos = 0; pos <= 180; pos += 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5); 
  else {
  //the fifth button has been pushed again and the fifth servo turns back 180 degrees
  }
  pos = 180; pos >= 0; pos -= 1 { // goes from 0 degrees to 180 degrees
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
  delay(5); 
  }

}

Code without sweep function-

#include <Servo.h> // make sure Servo library is included
Servo myServo; // create servo object to control a servo

int pushPin1 = 3;
int button1Reading = 0;
int pushPin2 = 4;
int button2Reading = 0;
int pushPin3 = 5;
int button3Reading = 0;
int pushPin4 = 6;
int button4Reading = 0;
int pushPin5 = 7;
int button5Reading = 0;

void setup() {

  pinMode(pushPin1, INPUT_PULLUP);
  myServo.attach(8);
  digitalWrite(3, HIGH);

  pinMode(pushPin2, INPUT_PULLUP);
  myServo.attach(9);
  digitalWrite(4, HIGH);

  pinMode(pushPin3, INPUT_PULLUP);
  myServo.attach(10);
  digitalWrite(5, HIGH);

  pinMode(pushPin4, INPUT_PULLUP);
  myServo.attach(11);
  digitalWrite(6, HIGH);

  pinMode(pushPin5, INPUT_PULLUP);
  myServo.attach(12);
  digitalWrite(7, HIGH);

  Serial.begin(9600);

}

void loop()
{
  button1Reading = digitalRead(pushPin1);
  if (button1Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    myServo.write(0);
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    myServo.write(180);
  }

  button2Reading = digitalRead(pushPin2);
  if (button2Reading == LOW) 
  //the second button has been pushed and the second servo turns 180 degrees
  {
    myServo.write(0);
  } else {
    //the second button has been pushed again and the second servo turns back 180 degrees
    myServo.write(180);
  }

  button3Reading = digitalRead(pushPin3);
  if (button3Reading == LOW)
    //the third button has been pushed and the third servo turns 180 degrees
  {
    myServo.write(0);
  } else {
    //the third button has been pushed again and the third servo turns back 180 degrees
    myServo.write(180);
  }

  button4Reading = digitalRead(pushPin4);
  if (button4Reading == LOW)
    //the fourth button has been pushed and the fourth servo turns 180 degrees
  {
    myServo.write(0);
  } else {
    //the fourth button has been pushed again and the fourth servo turns back 180 degrees
    myServo.write(180);
  } 

  button5Reading = digitalRead(pushPin5);
  if (button5Reading == LOW)
    //the fifth button has been pushed and the fifth servo turns 180 degrees
  {
    myServo.write(0);
  } else {
    //the fifth button has been pushed again and the fifth servo turns back 180 degrees
    myServo.write(180);
  }

}

am not sure though whether I need to include a sweep function in order to get this to work

How fast do you want the servos to move ? If slowly then you need to move them in steps with a delay (not a delay()) between moves. If you want them to move to their new position as quickly as possible then just write the new position to the servo.

There are, however, fundamental problems with your code. For instance

  button1Reading = digitalRead(pushPin1);
  if (button1Reading == LOW)
    //the first button has been pushed and the first servo turns 180 degrees
  {
    myServo.write(0);
  } else {
    //the first button has been pushed again and the first servo turns back 180 degrees
    myServo.write(180);
  }

How does the program know that the button has become pressed then pressed again ? Look at the StateChangeDetection example in the IDE to see how to detect this.

There are many improvements that could be made to the code such as using arrays to avoid code duplication and millis() for timing is slow servo movement is required. See Several things at the same time for examples of the latter.

Ok so I have improved the code, but I am still having trouble letting the program know when a button has become pressed and then pressed again. I had a look at statechangedirection and buttonstate, and I tried to incorporate this into the code - but I still don’t think it is right.

Would anyone mind having a look at my code as of now? Any kind of feedback would be appreciated!

Code-

#include <Servo.h> // make sure Servo library is included
Servo myservoA, myservoB, myservoC, myservoD, myservoE; // create servo object to control a servo

int pushPin1 = 2;
int pushPin2 = 4;
int pushPin3 = 7;
int pushPin4 = 8;
int pushPin5 = 12;
int btn1State = 0;
int btn2State = 0;
int btn3State = 0;
int btn4State = 0;
int btn5State = 0;

void setup() {

  pinMode(2, INPUT_PULLUP);
  myservoA.attach(5);
  digitalWrite(2, HIGH);

  pinMode(4, INPUT_PULLUP);
  myservoB.attach(6);
  digitalWrite(4, HIGH);

  pinMode(7, INPUT_PULLUP);
  myservoC.attach(9);
  digitalWrite(7, HIGH);

  pinMode(8, INPUT_PULLUP);
  myservoD.attach(10);
  digitalWrite(8, HIGH);

  pinMode(12, INPUT_PULLUP);
  myservoE.attach(11);
  digitalWrite(12, HIGH);

  Serial.begin(9600);

}

void loop() {

  if(digitalRead(2)==LOW) {
    btn1State = !btn1State;
    myservoA.write(180);
    delay(20);     // simple debounce
  }

  if(btn1State==1) {
    myservoA.write(0);
    //the first button has been pushed and 
    //the first servo turns 180 degrees
  } else {
    myservoA.write(180);
    //the first button has been pushed again and 
    //the first servo turns back 180 degrees 
  }
  

  if(digitalRead(4)==LOW) {
    btn2State = !btn2State;
    myservoB.write(180);
    delay(20);     // simple debounce
  }

  if(btn2State==1) {
    myservoB.write(0);
    //the second button has been pushed and 
    //the second servo turns 180 degrees
  } else {
    myservoB.write(180);
    //the second button has been pushed again and 
    //the second servo turns back 180 degrees
  }


  if(digitalRead(7)==LOW) {
    btn3State = !btn3State;
    myservoC.write(180);
    delay(20);     // simple debounce
  }

  if(btn3State==1) {
    myservoC.write(0);
    //the third button has been pushed and 
    //the third servo turns 180 degrees
  } else {
    myservoC.write(180);
    //the third button has been pushed again and 
    //the third servo turns back 180 degrees
  }


  if(digitalRead(8)==LOW) {
    btn4State = !btn4State;
    myservoD.write(180);
    delay(20);     // simple debounce
  }

  if(btn4State==1) {
    myservoD.write(0);
    //the fourth button has been pushed and 
    //the fourth servo turns 180 degrees
  } else {
    myservoD.write(180);
    //the fourth button has been pushed again and 
    //the fourth servo turns back 180 degrees
  }


  if(digitalRead(12)==LOW) {
    btn5State = !btn5State;
    myservoE.write(180);
    delay(20);     // simple debounce
  }

  if(btn5State==1) {
    myservoE.write(0);
    //the fifth button has been pushed and 
    //the fifth servo turns 180 degrees
  } else {
    myservoE.write(180);
    //the fifth button has been pushed again and 
    //the fifth servo turns back 180 degrees
  }
  
}

Try this example

#include <Servo.h>
const byte servoPin = 9;
const byte buttonPin = A1;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
byte servoPos = 0;

Servo aServo;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  aServo.attach(servoPin);
}

void loop()
{
  previousButtonState = currentButtonState;
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == LOW)  //button has become pressed
  {
    if (servoPos == 0)
    {
      servoPos = 180;
    }
    else
    {
      servoPos = 0;
    }
    aServo.write(servoPos);
    Serial.println(servoPos);
  }
}

You will need to adjust pin numbers and the button reading logic for your hardware.

NOTE 1 - as it stands the button input is not debounced
NOTE 2 - if you add more servos and inputs use arrays to hold the states and positions to avoid repeating the code.