issues with delaying servo from 0 to 45 degrees and vice versa

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?

Do you realize that delay(100) is for one tenth of a second?

The numbers are in milliseconds.

...R

if(pos == 0)
    {
    delay (100);
    }

Inside a loop where pos can never reach zero? Not gonna happen.

Same (similar) for the other loop.

PaulS:

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.

AWOL: if(pos == 0)    {    delay (100);    }

Inside a loop where pos can never reach zero? Not gonna happen.

Same (similar) for the other loop.

So I cant say if the position value is 0 degrees stay here for this amount of time?

Robin2: Do you realize that delay(100) is for one tenth of a second?

The numbers are in milliseconds.

...R

I do, I was just testing certain delay times, after I posted this code I tried it for 10000ms

So I cant say if the position value is 0 degrees stay here for this amount of time?

Yes, you can. But the value needs to be one that is reached during the for loop.

But, since the for loop runs from 0 to 44, and you want the delay to happen at 45, AFTER the for loop is a better place to put the delay.

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)

AWOL:

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);
    } 

  } 

}

That's totally different to what you posted earlier (Though the comments are still as wrong)

AWOL: That's totally different to what you posted earlier (Though the comments are still as wrong)

Yes, but the loops are still the same.

What don’t you understand about “totally”?

AWOL:
What don’t you understand about “totally”?

I understand you don’t have to be a jerk about it

So you don't see that the starting conditions of a loop are different to the end conditions?

AWOL: So you don't see that the starting conditions of a loop are different to the end conditions?

I do see it that's why I understood what you said, before it didn't fit the parameters but now it does.

I do see it that’s why I understood what you said, before it didn’t fit the parameters but now it does.

I see a sequence of words.

AWOL:

I do see it that's why I understood what you said, before it didn't fit the parameters but now it does.

I see a sequence of words.

Ok man this is getting nowhere