am planning to make some rotation using servo motor. right now I am using 2 HX5010 motors. I want them to rotate 0 to 180 & back one by one.
I have modified the example code Sweep. & the code looks like this
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(9);
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos < 0; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
for(pos = 0; pos>=180; pos-=1) // goes from 180 degrees to 0 degrees
{
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
}
When I run this code the motors look super confused. the motors are randomly rotating. Am i doing it right? & time of delay fine or I need to increase it?
Yes, now the rotation is controlled but I want servo motor 1 to rotate 180 degrees & then motor 2 to rotate 180 degrees & then back to 0 degree one by one.
here is the code now
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10);
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos < 0; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
}
Haven't changed much except the delays. i want motors to rotate alternately. right now motor1 finishes rotation 0-180 & back to 0 first then motor 2 starts rotating. there is variation of speed too.
Servo servo1; // create servo object to control a servo
Servo servo2;
int pos = 0; // variable to store the servo position
void setup()
{
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10);
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); } // waits 15ms for the servo to reach the position
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); } // waits 15ms for the servo to reach the position
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); }
}
}