3 servo+ 3 push button

With every button push corresponding motor should rotate and add to the counter upto 3.
I am unable to get the correct output. with single button push the motor rotates twice. :disappointed_relieved: :disappointed_relieved:

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;

const int  buttonPin1 = 2;    // the pin that the pushbutton is attached to
const int  buttonPin2 = 4;
const int  buttonPin3 = 7;

const int serPin1 = 9;       // the pin that the servo is attached to
const int serPin2 = 10; 
const int serPin3 = 11;

// Variables will change:
int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonPushCounter2 = 0;
int buttonPushCounter3 = 0;
int buttonPushCounter= 0;

int buttonState1 = 0;         // current state of the button
int buttonState2 = 0;         // current state of the button
int buttonState3 = 0;

int lastButtonState1 = 0;     // previous state of the button
int lastButtonState2 = 0; 
int lastButtonState3 = 0;

int val= 180;
int val1= 0;
void setup() {
  
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  
  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  
  // initialize the LED as an output:
  pinMode(serPin1, OUTPUT);
  pinMode(serPin2, OUTPUT);
   pinMode(serPin3, OUTPUT);
   
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
   buttonState3 = digitalRead(buttonPin3);
  
  {if (buttonState1 == HIGH)
  {buttonPushCounter1 ++;
Serial.print("switch1:  ");
      Serial.println(val, DEC);}
  else
  { if (buttonState2 == HIGH)
  {buttonPushCounter2 ++;
  Serial.print("switch2:  ");
      Serial.println(val, DEC);}

  else {
    if(buttonState3 == HIGH)
    {buttonPushCounter3 ++;
  Serial.print("switch3:  ");
      Serial.println(val, DEC);}
}
  }
buttonPushCounter == buttonPushCounter1 + buttonPushCounter2 + buttonPushCounter3;
    Serial.print("buttonPushCounter:  ");
      Serial.println(buttonPushCounter, DEC);
  
    }
 

  
  
 
  
   {if (buttonPushCounter == 1)              // when 1 switch is ON
   
  {
    if (buttonState1 == HIGH)
      {myservo1.write(val);
    }
  else
  { if (buttonState2 == HIGH)
    {myservo2.write(val);
    }
     else
    {myservo3.write(val);
    }
  }
 
  }
   }
   
  
  
     {if (buttonPushCounter == 2)          // when 2 switches are ON
   {
  if (buttonState1 == HIGH && buttonState2 == HIGH)  // switch 1 & 2
  {
    myservo1.write(val1);
    myservo2.write(val1);
  }
  else
  {
    // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
       myservo1.write(val);
       delay(1000);
      buttonPushCounter1 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes1:  ");
      Serial.println(buttonPushCounter1, DEC);
    } 
    else {
    myservo1.write(val);
    delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
  }
  
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      myservo2.write(val);
      delay(1000);
      buttonPushCounter2 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes2:  ");
      Serial.println(buttonPushCounter2, DEC);
    } 
    else {
      myservo2.write(val);
      delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
          }
  }
  }
  
 
 
 {
 if (buttonState2 == HIGH && buttonState3 == HIGH)  // switch 2 & 3
  {
    myservo2.write(0);
    myservo3.write(0);
  }
  else
  { 
     // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
       myservo1.write(val);
       delay(1000);
      buttonPushCounter2 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes2:  ");
      Serial.println(buttonPushCounter2, DEC);
    } 
    else {
    myservo2.write(val);
    delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
  }
    
  if (buttonState3 != lastButtonState3) {
    // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      myservo3.write(val);
      delay(1000);
      buttonPushCounter3 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes3:  ");
      Serial.println(buttonPushCounter3, DEC);
    } 
    else {
      myservo3.write(val);
      delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
          }
  }
  }
  
  { 
  
  if (buttonState3 == HIGH && buttonState1 == HIGH)
  {
    myservo3.write(val1);
    myservo1.write(val1);
  }
  else
  {
    // compare the buttonState to its previous state
  if (buttonState3 != lastButtonState3) {
    // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
       myservo3.write(val);
       delay(1000);
      buttonPushCounter3 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes3:  ");
      Serial.println(buttonPushCounter3, DEC);
    } 
    else {
    myservo3.write(val);
    delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
  }
  
  if (buttonState1 != lastButtonState1) {
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      myservo1.write(val);
      delay(1000);
      buttonPushCounter1 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes1:  ");
      Serial.println(buttonPushCounter1, DEC);
    } 
    else {
      myservo1.write(val);
      delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
          }
  }
  }
  }
 
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;
  lastButtonState3 = buttonState3;
  
  }

 }

     }
   {if (buttonPushCounter == 3)                // when 3 switches are ON

 { if (buttonState1 == HIGH && buttonState2 == HIGH && buttonState3 == HIGH)  // switch 1 & 2 & 3
  {
    myservo1.write(val);
    myservo2.write(val);
    myservo3.write(val);
    buttonPushCounter1 ++;
    buttonPushCounter2 ++;
    buttonPushCounter3 ++;
    Serial.print("S3 val:  ");
      Serial.println(val, DEC);
  }
  else
  {
  }
 }
  }
  }

You are not using the internal pull-up resistors. You are expecting the switches to read HIGH. These two facts imply that you are using external pull-down resistors. Is that true?

  {if (buttonState1 == HIGH)

Why is there an open curly brace in front of this statement?

  {buttonPushCounter1 ++;

The statements in the body of the block do not go on the same line as the {. Doing so makes the code too hard to read.

buttonPushCounter == buttonPushCounter1 + buttonPushCounter2 + buttonPushCounter3;

The == is an equality operator. A comparison is performed, not an assignment.

You are not debouncing the buttons, except for the time taken to print stuff.

There are a lot of problems with this code and it seems overly long to achieve something so simple. I'd suggest getting it to work with one servo first and debouncing your inputs. One specific thing to look at though, this code:

    val= val+ val1;
    val1= val- val1;
    val= val-val1;

Seems like a very obscure way of switching these variables between 0 and 180, and worse ,you are sharing those two val variables among all your servos, which I doubt was the intent.

Should I use a third variable? temp = a a=b b= temp

would this reduce the complexity?

Yes I have tried it with two servos and it works fine.
here is the code.

#include <Servo.h>

Servo myservo1;
Servo myservo2;
const int  buttonPin1 = 2;    // the pin that the pushbutton is attached to
const int  buttonPin2 = 4;
const int serPin1 = 9;       // the pin that the servo is attached to
const int serPin2 = 10; 
// Variables will change:
int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonPushCounter2 = 0;
int buttonState1 = 0;         // current state of the button
int buttonState2 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button
int lastButtonState2 = 0; 
int val= 180;
int val1= 0;
void setup() {
  
  myservo1.attach(9);
  myservo2.attach(10);
  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  // initialize the LED as an output:
  pinMode(serPin1, OUTPUT);
  pinMode(serPin2, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState1 == HIGH && buttonState2 == HIGH)
  {
    myservo1.write(0);
    myservo2.write(0);
  }
  else
  {
    // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {
    
    
    
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
       myservo1.write(val);
       delay(1000);
      buttonPushCounter1 ++;
      Serial.println("on");
      Serial.print("number of button pushes1:  ");
      Serial.println(buttonPushCounter1, DEC);
    } 
    else {
      myservo1.write(val);
      delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
    
  }
  
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      myservo2.write(val);
      delay(1000);
      buttonPushCounter2++;
      Serial.println("on");
      Serial.print("number of button pushes2:  ");
      Serial.println(buttonPushCounter2, DEC);
    } 
    else {
      myservo2.write(val);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
    
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;

  
}
}

it works fine with 2 servos.

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;

const int  buttonPin1 = 2;    // the pin that the pushbutton is attached to
const int  buttonPin2 = 4;
const int  buttonPin3 = 7;

const int serPin1 = 9;       // the pin that the servo is attached to
const int serPin2 = 10; 
const int serPin3 = 11;

// Variables will change:
int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonPushCounter2 = 0;
int buttonPushCounter3 = 0;
int buttonPushCounter= 0;

int buttonState1 = 0;         // current state of the button
int buttonState2 = 0;         // current state of the button
int buttonState3 = 0;

int lastButtonState1 = 0;     // previous state of the button
int lastButtonState2 = 0; 
int lastButtonState3 = 0;

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

int val= 180;
int val1= 0;
void setup() {
  
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  
  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  
  // initialize the LED as an output:
  pinMode(serPin1, OUTPUT);
  pinMode(serPin2, OUTPUT);
   pinMode(serPin3, OUTPUT);
   
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
   buttonState3 = digitalRead(buttonPin3);
  
  {
    if (buttonState1 == HIGH)
  {
    buttonPushCounter1 ++;
Serial.print("switch1:  ");
      Serial.println(val, DEC);
      myservo1.write(val);
    }
  else
  { 
    if (buttonState2 == HIGH)
  {
    buttonPushCounter2 ++;
  Serial.print("switch2:  ");
      Serial.println(val, DEC);
      myservo2.write(val);
    }
    else {
    if(buttonState3 == HIGH)
    {
      buttonPushCounter3 ++;
  Serial.print("switch3:  ");
      Serial.println(val, DEC);
    myservo3.write(val);
  }
}
  }
  
    } 
    {
  if (buttonState1 == HIGH && buttonState2 == HIGH)  // switch 1 & 2
  {
    myservo1.detach();
    myservo2.detach();
  }
  else
  {
    // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {
    myservo1.attach(9);
  myservo2.attach(10);
     // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
       myservo1.write(val);
       delay(1000);
      buttonPushCounter1 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes1:  ");
      Serial.println(buttonPushCounter1, DEC);
    } 
    else {
    myservo1.write(val);
    delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
  }
  
  if (buttonState2 != lastButtonState2) {
    myservo1.attach(9);
  myservo2.attach(10);
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      myservo2.write(val);
      delay(1000);
      buttonPushCounter2 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes2:  ");
      Serial.println(buttonPushCounter2, DEC);
    } 
    else {
      myservo2.write(val);
      delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
          }
  }
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;
  lastButtonState3 = buttonState3;
  
  } 
 {
 if (buttonState2 == HIGH && buttonState3 == HIGH)  // switch 2 & 3
  {
    myservo2.detach();
    myservo3.detach();
  }
  else
  { 
     // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {
    myservo2.attach(10);
  myservo3.attach(11);
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
       myservo1.write(val);
       delay(1000);
      buttonPushCounter2 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes2:  ");
      Serial.println(buttonPushCounter2, DEC);
    } 
    else {
    myservo2.write(val);
    delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
  }
    
  if (buttonState3 != lastButtonState3) {
   myservo2.attach(10);
  myservo3.attach(11);
        // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      myservo3.write(val);
      delay(1000);
      buttonPushCounter3 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes3:  ");
      Serial.println(buttonPushCounter3, DEC);
    } 
    else {
      myservo3.write(val);
      delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
          }
  }
  
  }
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;
  lastButtonState3 = buttonState3;
  
  
  { 
  
  if (buttonState3 == HIGH && buttonState1 == HIGH)
  {
    myservo3.detach();
    myservo1.detach();
  }
  else
  {
    // compare the buttonState to its previous state
  if (buttonState3 != lastButtonState3) {
    myservo1.attach(9);
    myservo3.attach(11);
    // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
       myservo3.write(val);
       delay(1000);
      buttonPushCounter3 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes3:  ");
      Serial.println(buttonPushCounter3, DEC);
    } 
    else {
    myservo3.write(val);
    delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
    }
  }
  
  if (buttonState1 != lastButtonState1) {
    myservo1.attach(9);
  myservo3.attach(11);
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      
      // if the current state is HIGH then the button
      // wend from off to on:
      myservo1.write(val);
      delay(1000);
      buttonPushCounter1 ++;
      Serial.println("on");
      Serial.print("S2 number of button pushes1:  ");
      Serial.println(buttonPushCounter1, DEC);
    } 
    else {
      myservo1.write(val);
      delay(1000);
      // if the current state is LOW then the button
      // wend from on to off:
      
      Serial.println("off"); 
      val= val+ val1;
      val1= val- val1;
    val= val-val1;
     Serial.print("val:  ");
      Serial.println(val, DEC);
   
      Serial.print("val1:  ");
      Serial.println(val1, DEC);
          }
  }
  }
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;
  lastButtonState3 = buttonState3;
  
  }
 
  // save the current state as the last state, 
  //for next time through the loop
  }

 }

//     }
//   {
//  if (buttonPushCounter == 3)                // when 3 switches are ON

 { 
   if (buttonState1 == HIGH && buttonState2 == HIGH && buttonState3 == HIGH)  // switch 1 & 2 & 3
  {
    myservo1.detach();
    myservo2.detach();
    myservo3.detach();
    buttonPushCounter1 ++;
    buttonPushCounter2 ++;
    buttonPushCounter3 ++;
    Serial.print("S3 val:  ");
      Serial.println(val, DEC);
  }
  else
  {
  }
  }
  }