Issues with code

We are creating a eye mechanism like one will cogley made instead of using a ada fruit we are using a bread board to control all of the servos. There are two servos that are meant to move a mechanism forward and the move back to mimic a blinking motion. I am a new at all of this and don't know what anything means any help is appreciated. :slight_smile:


#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(200);

   // 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);
         delay(500);
      }
   }
}

What does the sketch do ?
What should the sketch do ?
How are the servos powered ?

      if (buttonPressed = true);

2 mistakes in one line of code
= assigns a value
== tests a value

Delete the semicolon. It should not be there

= is not the same as ==

Loose the semi colon at the end.

the two servos will move forward and then not back no matter if you click the button again or not. I dont understand what causes the servos to move forward but then makes moving back not possible and one of the servos jitters while one doesn't. The servos are MG90s and are being powered by the 5v port using a battery pack and the USB cable.

Hello caleb_05

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code to see the values of variables, especially ones that control the servos, and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

Have you corrected the problems with = being used instead of == and removed the semicolon from the if statement ?

Nothing will work properly until you do. When you have corrected those errors then please post your current sketch in a new reply here

Where did you get the code? Have you messed with it at all?

Can you link to, or post, a schematic?

It is very likely you are having power supply issues.


buttonPressed begins life as false.

And that incorrect use of = in the if statement where you obvsly meant == is the only thing that changes buttonPressed, which would make it true once that part of the code runs.

I would expect nonsense behaviour both before and after you fix the errors that have been spotted so far.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.