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