Code problems with servos

We can't get our blinking servos to move back to our original position and everything we have tried has not fixed the issue. The servos move forward but not back we want the servos to move forward and back with a single click.

#include <Servo.h>

// Create servo objects for the horizontal and vertical movement of the eyes
Servo horizontalServo;
Servo verticalServo;

// Create two additional servo objects
Servo servo1;
Servo servo2;

// Joystick pins
const int xAxisPin = A0; // Connect the X-axis of the joystick to A0
const int yAxisPin = A1; // Connect the Y-axis of the joystick to A1
const int buttonPin = 7; // Connect the button to digital pin 7

// Servo angle limits
const int horizontalMinAngle = 0; // Minimum angle for left
const int horizontalMaxAngle = 130; // Maximum angle for right
const int verticalMinAngle = 0; // Minimum angle for up
const int verticalMaxAngle = 130; // Maximum angle for down

// Define the servo angle limits for servo1 and servo2
const int servo1MinAngle = 0;
const int servo1MaxAngle = -180;
const int servo2MinAngle = 0;
const int servo2MaxAngle = 180;

bool buttonPressed = false;

void setup() {
// Attach servos to their respective pins
horizontalServo.attach(9); // Connect to digital pin 9
verticalServo.attach(10); // Connect to digital pin 10

// Attach servo1 and servo2 to their respective pins
servo1.attach(11); // Connect servo1 to digital pin 11
servo2.attach(12); // Connect servo2 to digital pin 12

pinMode(buttonPin, INPUT_PULLUP); // Configure the push button pin as INPUT_PULLUP
}

void loop() {
// Read joystick values
int xAxisValue = analogRead(xAxisPin);
int yAxisValue = analogRead(yAxisPin);

// Map joystick values to servo angles for horizontal and vertical servos
int horizontalAngle = map(xAxisValue, 0, 1023, horizontalMinAngle, horizontalMaxAngle);
int verticalAngle = map(yAxisValue, 0, 1023, verticalMinAngle, verticalMaxAngle);
int servo1Angle = map(xAxisValue, 0, 1023, servo1MinAngle, servo1MaxAngle);
int servo2Angle = map(xAxisValue, 0, 1023, servo2MinAngle, servo2MaxAngle);

// Move the horizontal and vertical servos to the mapped angles
horizontalServo.write(horizontalAngle);
verticalServo.write(verticalAngle);

// Add a small delay to avoid rapid servo movement
delay(20);

// Check if the push button is pressed
if (digitalRead(buttonPin) == buttonPressed) {
if (buttonPressed = true); {
servo1.write(servo1MaxAngle); // Move servo1 to its maximum angle
servo2.write(servo2MaxAngle); // Move servo2 to its maximum angle
delay(500);
servo1.write(servo1MinAngle);
servo2.write(servo2MinAngle);
}
}
}

I moved your topic to an appropriate forum category @caleb_05.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Please edit your post to add code tags.

About 95% of servo problems reported in this forum are due to inadequate power supplies, like trying to use the Arduino 5V output to power them.

Please identify the servo type and post a wiring diagram, showing how they are powered.

bool buttonPressed = false;

      if (buttonPressed = true);

There are multiple things wrong with that if statement:

The buttonPressed variable is never changed so how can it ever be true?

And the ; at the end of the line means that all code after the if will be executed unconditionally. Lose the ;.

= is for assignment, == for comparison.

Here is your code indented and posted properly. Isn't this easier to read and follow? It sure is easier to copy to the IDE or a text editor for examination and verification.

#include <Servo.h>

// Create servo objects for the horizontal and vertical movement of the eyes
Servo horizontalServo;
Servo verticalServo;

// Create two additional servo objects
Servo servo1;
Servo servo2;

// Joystick pins
const int xAxisPin = A0; // Connect the X-axis of the joystick to A0
const int yAxisPin = A1; // Connect the Y-axis of the joystick to A1
const int buttonPin = 7; // Connect the button to digital pin 7

// Servo angle limits
const int horizontalMinAngle = 0; // Minimum angle for left
const int horizontalMaxAngle = 130; // Maximum angle for right
const int verticalMinAngle = 0; // Minimum angle for up
const int verticalMaxAngle = 130; // Maximum angle for down

// Define the servo angle limits for servo1 and servo2
const int servo1MinAngle = 0;
const int servo1MaxAngle = -180;
const int servo2MinAngle = 0;
const int servo2MaxAngle = 180;

bool buttonPressed = false;

void setup()
{
   // Attach servos to their respective pins
   horizontalServo.attach(9); // Connect to digital pin 9
   verticalServo.attach(10); // Connect to digital pin 10

   // Attach servo1 and servo2 to their respective pins
   servo1.attach(11); // Connect servo1 to digital pin 11
   servo2.attach(12); // Connect servo2 to digital pin 12

   pinMode(buttonPin, INPUT_PULLUP); // Configure the push button pin as INPUT_PULLUP
}

void loop()
{
   // Read joystick values
   int xAxisValue = analogRead(xAxisPin);
   int yAxisValue = analogRead(yAxisPin);

   // Map joystick values to servo angles for horizontal and vertical servos
   int horizontalAngle = map(xAxisValue, 0, 1023, horizontalMinAngle, horizontalMaxAngle);
   int verticalAngle = map(yAxisValue, 0, 1023, verticalMinAngle, verticalMaxAngle);
   int servo1Angle = map(xAxisValue, 0, 1023, servo1MinAngle, servo1MaxAngle);
   int servo2Angle = map(xAxisValue, 0, 1023, servo2MinAngle, servo2MaxAngle);

   // Move the horizontal and vertical servos to the mapped angles
   horizontalServo.write(horizontalAngle);
   verticalServo.write(verticalAngle);

   // Add a small delay to avoid rapid servo movement
   delay(20);

   // Check if the push button is pressed
   if (digitalRead(buttonPin) == buttonPressed)
   {
      if (buttonPressed = true);
      {
         servo1.write(servo1MaxAngle); // Move servo1 to its maximum angle
         servo2.write(servo2MaxAngle); // Move servo2 to its maximum angle
         delay(500);
         servo1.write(servo1MinAngle);
         servo2.write(servo2MinAngle);
      }
   }
}

Put a delay after these commands, just like the line before (delay(500);)

Because, immediately after servos are sent to "min" angle, the loop starts again, joystick is read (at dead/center) and the servos are told to stay where they are (because the joystick is dead/center).