How to move a servo X degrees rom the current position

Hi, I am using a servo motor to control a firing pin for a gun that I am making. I have the code down and it works like a charm. The only problem is that the initial angle of the servo is wrong and you have to dry fire first to line it up properly. I would like to add a bit of code in the void setup that can move it to the right angle.
My question is how do you move a servo from the current position to an X number of degrees?
Thanks in advance.

My question is how do you move a servo from the current position to an X number of degrees?

If you know the increment, add it to the current position, to give the new position, and write the new position to the servo.

If you don't know the current position, or the increment, there is a problem with your project statement.

jreeve17:
Hi, I am using a servo motor to control a firing pin for a gun that I am making. I have the code down and it works like a charm. The only problem is that the initial angle of the servo is wrong and you have to dry fire first to line it up properly. I would like to add a bit of code in the void setup that can move it to the right angle.
My question is how do you move a servo from the current position to an X number of degrees?
Thanks in advance.

That's a little difficult to answer as the arduino has no knowledge of what the current position of the servo may be at startup, all you can do at startup is to do a servo write command to the servo position you desire it to go to with no foreknowledge of where it might be at the time.

Lefty

Hi retro lefty, your answer is exactly what I want. How would I do that?

jreeve17:
Hi retro lefty, your answer is exactly what I want. How would I do that?

You mean like this?

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created  
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(0);  // command servo to it's 'zero position' or any other
                     // starting position you desire in the 0-180 range
} 
 
 
void loop() 
{ 
  // your other code goes here
}

Lefty

Yes, that worked perfectly. Thanks a ton!