Moving 2 servo right, left, up, down

I own 2 servos. And I would like to move it up, down and right, left.

So far, with the help of the above code, the servo moves right and left and returns to its position. (However, it returns too rapidly to position 0).

The problem is that one servo does not move at all (up, down)

1)So what is wrong with the code that the other servo is not moving?
2) How can I make the first servo return to position not rapidly to position 0 but, as requested, a few degrees?

code;

#include <Servo.h> 
 
Servo servo1;  
Servo servo2; 
int pos= 0; 
int swi= 8; 
 
void setup() 
{ 
  servo1.attach(9);  
  servo2.attach(10);  

} 
 
void loop() 
{  
  if (pos< 180) { 
    servo1.write(pos); 
    servo2.write(pos); 

  } else { 
  pos= 0;
  }    
  
  pos= pos+ swi; 
  delay(200);                 
}

Thanks for your answer!

What is wrong with the code you haven't posted?
Seriously?

What code would that be ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You need to post your code and circuit otherwise its going to be guesswork.

Did you test each servo indepedently first? Proceed in steps verifying the basic hardware elements separately first, only then go on to program several devices together.

Sorry, it didn't upload my code right away. I am correcting the question.

 pos= 0;

Considering this line of code is that any surprise ?

The Servo Sweep example will show you one way to do what you describe, but there are neater ways

I tried something like this pos= pos- swi;, but the servo goes to 180 and doesn't come back, it just stops there, and I would like it to come back at the same rate as it moved at the beginning

Have you looked at the Sweep example ?

Yes, the Sweep servo works as expected, but how can I add a 2nd servo to it to make it move the same way just up, down?

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    myservo2.write(pos);
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos); 
    myservo2.write(pos);// tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

I seem to have added a 2nd servo to the code but it doesn't work;/

Are you shure your second servo is connected properly and the hardware is working?

Swap the physical connections to the servo signal wires. Does the same actual servo still not move or is it the one connected to the same pin that does not move ?

Ok, it's working, but how can I limit servo 2 to swing only from 0 to 30 degrees, I do not want it to rotate 180, do I have to write a new loop? because currently they draw information from one and the same loop

Give each servo its own pos variable and pair of for loops to manipulate it.

Do you want the 2 servos to move at the same time and do you want to synchronise their movements ?

Yes

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