Hi there
I am working on a project by using 2 servo motors with different rotations as the below code
I like myservo to rotate -180 to 0 and 0 to -180 if possible.
While myservo is rotating like the question 1, myservo2 is rotating to 90 degree and stay the position for 5 seconds.
Please give me your thought.
Thanks, Jay
void loop()
{
//I like to rotate this to 0 to -180
for (pos = 0; pos <= 180; pos++)
{
myservo.write(pos);
int pos2;
if (count==3)
//if count==3 after 3 rotation of myservo
pos2=90;
else
pos2=0;
myservo2.write(pos2);
//I like myservo2 to be stayed 5 seconds while myservo is rotating
delay(10);
}
for (pos = 180; pos >= 0; pos--)
{
myservo.write(pos);
int pos2;
if (count==3)
{
//pos2 = pos + 45;
pos2=90;
count=0;
}
else
pos2=0;
myservo2.write(pos2);
delay(10);
2 servo motors are rotating well without any giggling and sudden rotation. Just my concern is myservo2 is stayed 90 degree for 5 seconds while myservo is rotating. One of my issues is the myservo2 is in a for loop, so it will be a delayed due to myservo2's stay.
Thanks, J
You would be best moving servo2 out of the if statement, and do a non-blocking delay.
Look that up please.
But basically, you would set a time measurement variable and test it every time through loop. When it reaches 5000 ms, you would command servo2 to move.
long myTime = millis(); // set this one time
if (millis()-myTime > 5000) {
myTime = millis();
//your code
}
Now, you will need to use less than 5000, because you have put a delay in your servo loop
Hi CNCNOTES
I have tried what you suggested as above in many ways. Due to my lack of knowledge, I couldn't get servo2 stayed a long time and get back to 0 till 3 rotations of servo.
What I wanted to is that myservo is rotating and then after 3 rotations of myservo, myservo2 rotates to 90 degree and stay there for 3-5 seconds and move back to 0degree. This process will be continue till a power down.
Would you give me a detail code so that I can try?
Thanks,
I have tried using this code inside of my code. One time, myservo2 was rotating to 90 and stayed with giggling sometimes, but got back to 0. This was what I did.
#include <Servo.h>
Servo myservo;
Servo myservo2;
int pos = 0;
int count=0;
long myTime=millis();
void setup()
{
myservo.attach(9);
myservo2.attach(10);
}
void loop()
{
//I like to rotate this to 0 to -180
for (pos = 0; pos <= 180; pos++)
{
myservo.write(pos);
if(millis()-myTime>3000)
{
myTime=millis();
myservo2.write(0);
}
else
{
myservo2.write(90);
}
//myservo2.write(pos2);
//I like myservo2 to be stayed 5 seconds while myservo is rotating
delay(10);
}
for (pos = 180; pos >= 0; pos--)
{
myservo.write(pos);
delay(10);
}
count++;
}
No I am still struggling and I made a miss-typing on the previous email which I made you confused. It didn't go back to 0 position. I didn't work. Any idea?