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);
}
}
}