2 servos moving in opposite diresction ( Solved)

Hey all,
Been kicking around a project for a while and couldn't find a solution but here it is.

My problem was that I was trying to operate 2 servos from one push button and have them move in opposite directions.

If this has been posted elsewhere, my bad.

Here is my code. Tested and working..

[code]
/*
 * Code (Partly) by Greg TEdford with help from many others.
 * 19th October 2020..
 */
 
 /* pushbutton attached to pin 2 from +5V
    10K resistor attached to pin 2 from ground
    servoLeft attached to pin 9
    servoLeft moves from 0 to 50 on first push of button
    servoLeft moves back to zero on second push.
    servoRight attached to pin 10
    servoRight moves from 0 to -50 on first push
    servoRight moves from -50 to 0 on second push
    
*/

#include <Servo.h>
Servo myservoRight;
Servo myservoLeft;
int posL = 0;
int posR = 0;

byte switchPin = 2;                    // switch is connected to pin 2
byte buttonPresses = 0;                // how many times the button has been pressed
byte lastPressCount = 0;               // to keep track of last press count

void setup() {
  myservoLeft.attach(9);
  myservoLeft.write(0);
  myservoRight.attach(10);
  myservoRight.write(180);
  pinMode(switchPin, INPUT);          // Set the switch pin as input
  digitalWrite(switchPin, LOW);      // set pullup resistor
  Serial.begin(9600);                 // Set up serial communication at 9600bps
}

void loop(){

  if (digitalRead(switchPin) == HIGH)  // check if button was pressed
  {
    buttonPresses++;                  // increment buttonPresses count
    delay(500);                       // debounce switch
    if (buttonPresses > 2){
    buttonPresses = 1;
    }
    Serial.print ("Button press count = ");          // out to serial
    Serial.println(buttonPresses, DEC);

  if (lastPressCount != buttonPresses)              // only do output if the count has changed
  {
    if (buttonPresses == 1){
      for (posL = 0; posL <= 50; posL += 1)  
    myservoLeft.write(posL);             
    delay(15); 
    for (posR = 0; posR <= 80; posR +=1)
    myservoRight.write(180-posR);
    delay(15);
    }
    
    if (buttonPresses == 2){
      for (posL = 50; posL >= 0; posL -= 1) 
    myservoLeft.write(posL);             
    delay(15); 
      for (posR = 80; posR >= 0; posR -= 1)
    myservoRight.write(180-posR);
    delay(15);                     
  }     }  }
    lastPressCount = buttonPresses;    // track last press count

}

[/code]
Hope this helps others.
The 80 in the code was to get the arms to match movement. Feel free to modify this figure to match your needs.

Hasta la vista baby....

If we look at just a section of your code, and format it correctly, we see a problem.

  if (lastPressCount != buttonPresses)              // only do output if the count has changed
  {
      if (buttonPresses == 1){
          for (posL = 0; posL <= 50; posL += 1) 
              myservoLeft.write(posL);             
          delay(15);
          for (posR = 0; posR <= 80; posR +=1)
              myservoRight.write(180-posR);
          delay(15);
    }

You may as well just have written

  if (lastPressCount != buttonPresses)              // only do output if the count has changed
  {
      if (buttonPresses == 1){
          myservoLeft.write(50);
          delay(15);
          myservoRight.write(100);
          delay(15);
    }

, though I'm not sure what purpose the delays serve.

Thanks for the reply...

I tidied up my code as suggested and it works the same. ( But neater. :smiley: )
I also removed the delay as suggested.
Don't know why that was there either. I just copied the code from the original poster and it worked, so I left it alone. :kissing:

I assumed the 180- was to get the servo to run in reverse but now you have pointed it out.. 180-80=100. :cry:

In the void setup, in line 31, i stated myservoRight.write(180) so that servo would start at 180 so 100 would be reverse and 180 would be back to the original position... Doh..
Live and learn, live and learn.... :sleeping: :sleeping: :sleeping:

Just checked the original post for that delay.
I copied it from the example servo sweep on the Arduino ide.

Strangely enough... If I just state servo.write(180); and then servo.write(0); without the delay, the servo jumps around and doesn't turn smoothly.

Also if I remove the delay(15) from the original servosweep sketch the servos shudder and jump around also.

With the delay included, the movement is smooth and controlled.

Curious......

Edit...

Just played around with the original servo sweep....
If I write servo.write(0); in void setup and servo.write(180); and servo.write(0); it works smoothly but at full speed.

So the pos+=1 controls the speed. If I make that 10 it moves full speed and 20 it needs the delay of 50 or it doesn't get to 180 before it returns.

Might be handy for later...

Maybe the original code had more braces.