Problem configuring 2 buttons for 2 different sequences

I am building a cosplay/halloween costume for my daughter of Circus Baby from FNAF and trying to mimic how the characters face opens up to reveal the endoskeleton. I have everything built and the face has hinges, there are 3 hinges on each side and I have 4 servos to open the hinges. The top 2 sections are connected so I have both top servos wired together to act in unison, leaving a middle servo and bottom servo. I also have a fan in the chest and the eyes light up, but they are not connected to the UNO.

I had everything working with one button that would open all servos to 90 when pushed and return to 0 on the 2nd push. I then had everything working with one button that would open different sections to different degrees with pauses in between, and that worked as well. My problem is that I’m not sure how to combine the code so they will work without interfering with each other. I have searched online for similar setups, but can’t seem to find any. In my original code that was working I used resistors on the buttons and in the new code I enabled the pullup resistors. Everything is the same other than that change. Please look at my code and let me know if it can be fixed. Thanks.

#include <Servo.h>


const int  abuttonPin = 1;
const int  rbuttonPin = 2;
const byte atservoPin = 3;
const byte amservoPin = 5;
const byte abservoPin = 6;
const byte rtservoPin = 9;
const byte rmservoPin = 10;
const byte rbservoPin = 11;

Servo atservo;
Servo amservo;
Servo abservo;
Servo rtservo;
Servo rmservo;
Servo rbservo;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int buttonState2 = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int lastButtonState2 = 0;     // previous state of the button

void setup()
{
  pinMode(abuttonPin, INPUT_PULLUP);
  pinMode(rbuttonPin, INPUT_PULLUP);
  atservo.write(0);
  amservo.write(0);
  abservo.write(0);
  rtservo.write(0);
  rmservo.write(0);
  rbservo.write(0);
  atservo.attach(3);
  amservo.attach(5);
  abservo.attach(6);
  rtservo.attach(9);
  rmservo.attach(10);
  rbservo.attach(11);
  Serial.begin(9600);
}

void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(abuttonPin);
  buttonState2 = digitalRead(rbuttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
        if (buttonState == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter++;      
      if(buttonPushCounter == 1)
      {
        atservo.write(0);
        amservo.write(0); 
        abservo.write(0);
      }
      if(buttonPushCounter == 2)
      {
        rtservo.write(90);
        rmservo.write(90); 
        rbservo.write(90);
        buttonPushCounter = 0;
      }
    }
    delay(500);

  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  

    // if the state has changed, increment the counter
    if (buttonState2 == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter++;      
      if(buttonPushCounter == 1)
      {
        rtservo.write(0);
        rmservo.write(0); 
        rbservo.write(0);     
      }
      if(buttonPushCounter == 2)
      {
        rtservo.write(45);
        rmservo.write(15);
        rbservo.write(75);
        delay(4000);
        rtservo.write(0);
        rmservo.write(45);
        rbservo.write(15);
        delay(6000);
        rtservo.write(75);
        rmservo.write(0);
        rbservo.write(45);
        delay(2000);
        rtservo.write(15);
        rmservo.write(75);
        rbservo.write(0);
        delay(7000);
        rtservo.write(0);
        rmservo.write(0);
        rbservo.write(0);
        delay(5000);
        buttonPushCounter = 0;
      }
    }
    delay(15);
    lastButtonState2 = buttonState2;
  }
}

Serial. Uses pins 0 and 1.
Oops

Thanks for the help. I’m trying to learn this, but it is a lot to take in with no experience. The buttons have no effect and the servos jitter continuously. New code below.

#include <Servo.h>


const int  abuttonPin = 4;
const int  rbuttonPin = 2;
const byte atservoPin = 3;
const byte amservoPin = 5;
const byte abservoPin = 6;
const byte rtservoPin = 9;
const byte rmservoPin = 10;
const byte rbservoPin = 11;

Servo atservo;
Servo amservo;
Servo abservo;
Servo rtservo;
Servo rmservo;
Servo rbservo;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int buttonState2 = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int lastButtonState2 = 0;     // previous state of the button

void setup()
{
  pinMode(abuttonPin, INPUT_PULLUP);
  pinMode(rbuttonPin, INPUT_PULLUP);
  atservo.write(0);
  amservo.write(0);
  abservo.write(0);
  rtservo.write(0);
  rmservo.write(0);
  rbservo.write(0);
  atservo.attach(3);
  amservo.attach(5);
  abservo.attach(6);
  rtservo.attach(9);
  rmservo.attach(10);
  rbservo.attach(11);
  Serial.begin(9600);
}

void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(abuttonPin);
  buttonState2 = digitalRead(rbuttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
        if (buttonState == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter++;      
      if(buttonPushCounter == 1)
      {
        atservo.write(0);
        amservo.write(0); 
        abservo.write(0);
      }
      if(buttonPushCounter == 2)
      {
        rtservo.write(90);
        rmservo.write(90); 
        rbservo.write(90);
        buttonPushCounter = 0;
      }
    }
    delay(500);

  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  

    // if the state has changed, increment the counter
    if (buttonState2 == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter++;      
      if(buttonPushCounter == 1)
      {
        rtservo.write(0);
        rmservo.write(0); 
        rbservo.write(0);     
      }
      if(buttonPushCounter == 2)
      {
        rtservo.write(45);
        rmservo.write(15);
        rbservo.write(75);
        delay(4000);
        rtservo.write(0);
        rmservo.write(45);
        rbservo.write(15);
        delay(6000);
        rtservo.write(75);
        rmservo.write(0);
        rbservo.write(45);
        delay(2000);
        rtservo.write(15);
        rmservo.write(75);
        rbservo.write(0);
        delay(7000);
        rtservo.write(0);
        rmservo.write(0);
        rbservo.write(0);
        delay(5000);
        buttonPushCounter = 0;
      }
    }
    delay(15);
    lastButtonState2 = buttonState2;
  }
}
    // if the state has changed, increment the counter
    if (buttonState2 == LOW)
    {

How do you know that buttonState2 has changed ?

int buttonState = 0;         // current state of the button
int buttonState2 = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int lastButtonState2 = 0;     // previous state of the button

It looks pretty silly to hang a 2 on the end of one variable when you don't hang a 1 on the end of the other one.

It looks pretty silly to use meaningless names. Why does the switch even exist? The current state and previous state variables should reflect that purpose, NOT the fact that the variable holds a pin state (NOT a "button" state, especially since buttons only have "sewn on" or "fell off" as valid states).

      if(buttonPushCounter == 1)
      if(buttonPushCounter == 2)

Under what conditions might both of these be true? Mutually exclusive conditions should use else if statements for all but the first condition.

The buttons have no effect

What do your Serial.print() statements tell you is happening?

Why not?

and the servos jitter continuously

Standard question when this happens: How are the servos powered?

As I said earlier, I know nothing about coding as is evident in the code. I read more today and now have the code working, but it only works if I have the UNO connected to USB and there is a constant hum whenever it is connected. The unit is powered by 4 AA batteries and I think that they may be close to drained, but don’t have my volt meter to check.

I am using Self-reset SPST Latching Push Button Switches, and they currently have to be cycled every other time to work. Is there any way to change the code so that they work with every push or is that not possible with this type of switch?

#include <Servo.h>

const int  abuttonPin = 2;
const int  rbuttonPin = 4;
const byte tservoPin = 9;
const byte mservoPin = 10;
const byte bservoPin = 11;

Servo tservo;
Servo mservo;
Servo bservo;

int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button
int buttonPushCounter2 = 0;   // counter for the number of button presses
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button

void setup()
{
  pinMode(abuttonPin, INPUT_PULLUP);
  pinMode(rbuttonPin, INPUT_PULLUP);
  tservo.write(0);
  mservo.write(0);
  bservo.write(0);
  tservo.attach(9);
  mservo.attach(10);
  bservo.attach(11);
}

void loop()
{
  // read the pushbutton input pin:
  buttonState1 = digitalRead(abuttonPin);
  // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1)
  {
    // if the state has changed, increment the counter
    if (buttonState1 == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter1++;      
      if(buttonPushCounter1 == 1)
      {
        tservo.write(0);
        mservo.write(0); 
        bservo.write(0);
      }
      if(buttonPushCounter1 == 2)
      {
        tservo.write(90);
        mservo.write(90); 
        bservo.write(90);
        buttonPushCounter1 = 0;
      }
    }
    delay(15);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState1 = buttonState1;

    // read the pushbutton input pin:
  buttonState2 = digitalRead(rbuttonPin);
  // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2)
  {
    // if the state has changed, increment the counter
    if (buttonState2 == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      buttonPushCounter2++;      
      if(buttonPushCounter2 == 1)
      {
        tservo.write(0);
        mservo.write(0); 
        bservo.write(0);
      }
      if(buttonPushCounter2 == 2)
      {
        tservo.write(45);
        mservo.write(15);
        bservo.write(75);
        delay(2000);
        tservo.write(0);
        mservo.write(45);
        bservo.write(15);
        delay(2000);
        tservo.write(75);
        mservo.write(0);
        bservo.write(45);
        delay(2000);
        tservo.write(15);
        mservo.write(75);
        bservo.write(0);
        delay(2000);
        tservo.write(0);
        mservo.write(0);
        bservo.write(0);
        delay(2000);
        buttonPushCounter2 = 0;
      }
    }
    delay(15);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState2 = buttonState2;
}

As the switches are latching they will stay in the same state when pushed until pushed again.

Your current code only acts when an input becomes LOW, ie every other button push. Change it so that the current state is irrelevant and only the change of state, whatever it is, is acted upon.