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!
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.
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
#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;/
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