Hello I am new to arduino and I have a question regarding a servo delay issue that I am having. With the code I have I can go from 0 to 45 degrees and from 45 to 0 degrees. The problem that I am having is that I want it to once it goes from 0 to 45 degrees stop for a period of time and when it goes 45 to 0 degrees stop for a period of time. Code that I have worked on is below.
#include <Servo.h>
Servo myservo; // 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()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 45; pos += 1) // goes from 0 degrees to 45 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
if(pos == 45)
{
delay(100);
}
}
for(pos = 45; pos>=1; pos-=1) // goes from 45 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
if(pos == 0)
{
delay (100);
}
}
}
The problem that I am having is that I want it to once it goes from 0 to 45 degrees stop for a period of time and when it goes 45 to 0 degrees stop for a period of time.
Can you recognize the portion that makes the servo move from 0 to 45? Can you recognize the portion that makes the servo go from 45 to 0? Can you see where to put a delay(someAmountOfTime); between them?
When I commented out one portion and tested it there wasn't much difference on how the program was executed. I could see the delay difference though. I think I meant to say that I want to do -45 to 45 degrees instead of 0 to 45 degrees.
I think I meant to say that I want to do -45 to 45 degrees instead of 0 to 45 degrees.
Really? Is your servo capable of moving to -45 degrees? Most aren't. And those that can are rarely smart enough to stop there.
Have you tried a delay between the two for loops?
I wanted it to go from -45 to 45 degrees but for the servo that I am using that is not the case. It is more so in the 90 to 0 degree range to make the sweep motion that I want. I got it to delay like I want but now I want it to go from 90 degrees to the middle then from the middle to 0 degrees and vice versa.
So I cant say if the position value is 0 degrees stay here for this amount of time?
Yes, of course you can.
Just not in those loops.
Why bother with a test?
Just put the delay outside the loop.
(You shouldn't always believe what a comment tells you)
I'm trying to picture what your telling me because I go it to work with those loops. I'll post what I coded up, could you show me what you are referring to?
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 52; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 52; pos < 90; pos += 1) // goes from 52 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 5ms for the servo to reach the position
if(pos == 52) //At the middle wait for 1000ms
{
delay(1000);
}
}
for(pos = 52; pos > 0; pos-=1) // goes from 52 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 5ms for the servo to reach the position
if(pos == 52) //At the middle wait for 1000ms
{
delay(1000);
}
}
}