Help-need help fixing this program to move two servos at same time

This is my first time posting and am new to Arduino, I thought I had working and seems to work for one or two times then fails.
Initial time push button, the servos run separate, push button for going back they seem to work close enough together, then push button up and work good, next time do not work together at all
for MyServo and Myservo2. Myservo3 and myservo4 works fine.
#include <Servo.h>

Servo myservo; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;

int pos = 90; // variable to store the servo's starting position
int pos2 = 90;
int pos3 = 90;
int pos4 = 90;

const int maxDeg = 125; // limits the maximum range of the servo's movement to 125
const int minDeg = 35; // limits the minimum range of the servo's movement to 35
const int maxDeg2 = 125; // limits the maximum range of the servo's movement to 125
const int minDeg2 = 35; // limits the minimum range of the servo's movement to 35

const int movement = 1; // distance to move servo
const int movement2 = 1; // distance to move servo
const int movement3 = 1; // distance to move servo
const int movement4 = 1; // distance to move servo
// This basically means the servo will sweep from 45 to 135 (not 0 to 180 as expected), you can adjust this to suit your own servo motors specs

const int leftPin = 2; // tells the Arduino the location of the signal cable from the switch
const int rightPin = 3;

const int leftPin3 = 6;
const int rightPin3 = 7;

const int outputPin = 8; // tells the Arduino the location of the signal cable to the servo
const int outputPin2 = 9;
const int outputPin3 = 10;
const int outputPin4 = 11;

int leftPressed = 0; // variables we will use to keep information about the switch states
int rightPressed = 0;

int leftPressed2 = 0;
int rightPressed2 = 0;

int leftPressed3 = 0;
int rightPressed3 = 0;

void setup()
{
myservo.attach(outputPin); // attaches the servo motor's signal cable location, stored in the variable outputPin, to the servo object
myservo2.attach(outputPin2);
myservo3.attach(outputPin3); // attaches the servo motor's signal cable location, stored in the variable outputPin, to the servo object
myservo4.attach(outputPin4);

pinMode(leftPin, INPUT); // sets the state of the pins to input mode
pinMode(rightPin, INPUT);

pinMode(leftPin3, INPUT);
pinMode(rightPin3, INPUT);
}

void loop()
{
leftPressed = digitalRead(leftPin); //gives a value to the variables as the state of the switch
rightPressed = digitalRead(rightPin);
leftPressed3 = digitalRead(leftPin3);
rightPressed3 = digitalRead(rightPin3);

// The following routine handles what happens if the first set of push buttons are pressed WANT PIN 8 AND PIN 9 TO OPERATE AT SAME TIME

if (leftPressed) {
if (pos < maxDeg)
pos += movement;
(pos2 < maxDeg2);
pos2 -= movement2;
myservo.write(pos); // tells the servo to go to the position stored in the variable 'pos' pin 8
myservo2.write(pos2); // tells the servo to go to the position stored in the variable 'pos' pin 9
}

if (rightPressed) {
if (pos > minDeg)
(pos2 > minDeg2);
pos -= movement;
pos2 += movement2;
myservo.write(pos); // tells the servo to go to the position stored in the variable 'pos' pin 8
myservo2.write(pos2); // tells the servo to go to the position stored in the variable 'pos' pin 9

}

// The following routine handles what happens if the third pair of push buttons are pressed WANT PIN 10 AND PIN 11 TO OPERATE AT SAME TIME

if (leftPressed3) {
if (pos3 < maxDeg)
pos3 += movement3;
(pos4 < maxDeg2);
pos4 += movement4;
myservo3.write(pos3); // tells the servo to go to the position stored in the variable 'pos3'
myservo4.write(pos4); // tells the servo to go to the position stored in the variable 'pos4'
}

if (rightPressed3) {
if (pos3 > minDeg)
pos3 -= movement3;
(pos4 > minDeg2);
pos4 -= movement4;
myservo3.write(pos3); // tells the servo to go to the position stored in the variable 'pos3'
myservo4.write(pos4); // tells the servo to go to the position stored in the variable 'pos4'
}
else;
delay(15);
}

Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

    (pos2 < maxDeg2);

You have a few lines like this. What are they intended to do? They look like comparisons but with no if! But I may just be missing something.

Steve

When you have three or more variables that have the same name except for a number at the end of the name, it's time to switch to using arrays.

#include  <Servo.h>


Servo Servos[4];  // create servo object to control a servo
int ServoPositions[4] = {90, 90, 90, 90};
const int ServoPins[4] = {8, 9, 10, 11}; // tells the Arduino the location of the signal cable to the servo


const int maxDeg = 125; // limits the maximum range of the servo's movement to 125
const int minDeg = 35;   // limits the minimum range of the servo's movement to 35


const int MovementStep = 1; // distance to move servo


const int LeftPin = 2; // tells the Arduino the location of  the signal cable from the switch
const int RightPin = 3;


const int LeftPin3 = 6;
const int RightPin3 = 7;




void setup()
{
  for (int i = 0; i < 4; i++)
  {
    Servos[i].attach(ServoPins[i]);
    Servos[i].write(ServoPositions[i]);
  }


  pinMode(LeftPin, INPUT); // sets the state of the pins to input mode
  pinMode(RightPin, INPUT);


  pinMode(LeftPin3, INPUT);
  pinMode(RightPin3, INPUT);
}


void loop()
{
  // The following routine handles what happens if the
  // first set of push buttons are pressed
  // WANT PIN 8 AND PIN 9 TO OPERATE AT SAME TIME


  if (digitalRead(LeftPin))
  {
    if (ServoPositions[0] < maxDeg)
      ServoPositions[0] += MovementStep;
    if (ServoPositions[1] < maxDeg)
      ServoPositions[1] += MovementStep;
  }


  if (digitalRead(RightPin))
  {
    if (ServoPositions[0] > minDeg)
      ServoPositions[0] -= MovementStep;
    if (ServoPositions[1] > minDeg)
      ServoPositions[1] -= MovementStep;
  }


  // The following routine handles what happens if the
  // second set of push buttons are pressed
  // WANT PIN 10 AND PIN 11 TO OPERATE AT SAME TIME
  
  if (digitalRead(LeftPin3))
  {
    if (ServoPositions[2] < maxDeg)
      ServoPositions[2] += MovementStep;
    if (ServoPositions[3] < maxDeg)
      ServoPositions[3] += MovementStep;
  }


  if (digitalRead(RightPin3))
  {
    if (ServoPositions[2] > minDeg)
      ServoPositions[2] -= MovementStep;
    if (ServoPositions[3] > minDeg)
      ServoPositions[3] -= MovementStep;
  }


  // Updte all the servos
  for (int i = 0; i < 4; i++)
  {
    Servos[i].write(ServoPositions[i]);
  }


  delay(15);
}

Inadequate power supplies (like trying to use the Arduino 5V output) account for around 95% of the servo problems reported on this forum.

Always use a separate power supply and connect all the grounds together. 4X AA will work for one or two small servos.

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